1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
| package io.binglau.scala.akka.demo.scatter_gather
import java.util.concurrent.TimeUnit import scala.concurrent._ import scala.concurrent.duration._ import ExecutionContext.Implicits.global import akka.actor._
case class RequestForQuotation(rfqId: String, retailItems: Seq[RetailItem]) { val totalRetailPrice: Double = retailItems.map(retailItem => retailItem.retailPrice).sum }
case class RetailItem(itemId: String, retailPrice: Double)
case class RequestPriceQuote(rfqId: String, itemId: String, retailPrice: Double, orderTotalRetailPrice: Double)
case class PriceQuote(quoterId: String, rfqId: String, itemId: String, retailPrice: Double, discountPrice: Double)
case class PriceQuoteFulfilled(priceQuote: PriceQuote)
case class PriceQuoteTimedOut(rfqId: String)
case class RequiredPriceQuotesForFulfillment(rfqId: String, quotesRequested: Int)
case class QuotationFulfillment(rfqId: String, quotesRequested: Int, priceQuotes: Seq[PriceQuote], requester: ActorRef)
case class BestPriceQuotation(rfqId: String, priceQuotes: Seq[PriceQuote])
case class SubscribeToPriceQuoteRequests(quoterId: String, quoteProcessor: ActorRef)
object ScatterGather extends App { val system = ActorSystem("demo") val priceQuoteAggregator = system.actorOf(Props[PriceQuoteAggregator], "priceQuoteAggregator")
val orderProcessor = system.actorOf(Props(classOf[MountaineeringSuppliesOrderProcessor], priceQuoteAggregator), "orderProcessor")
system.actorOf(Props(classOf[BudgetHikersPriceQuotes], orderProcessor), "budgetHikers") system.actorOf(Props(classOf[HighSierraPriceQuotes], orderProcessor), "highSierra") system.actorOf(Props(classOf[MountainAscentPriceQuotes], orderProcessor), "mountainAscent") system.actorOf(Props(classOf[PinnacleGearPriceQuotes], orderProcessor), "pinnacleGear") system.actorOf(Props(classOf[RockBottomOuterwearPriceQuotes], orderProcessor), "rockBottomOuterwear")
orderProcessor ! RequestForQuotation("123", Vector(RetailItem("1", 29.95), RetailItem("2", 99.95), RetailItem("3", 14.95)))
orderProcessor ! RequestForQuotation("125", Vector(RetailItem("4", 39.99), RetailItem("5", 199.95), RetailItem("6", 149.95), RetailItem("7", 724.99)))
orderProcessor ! RequestForQuotation("129", Vector(RetailItem("8", 119.99), RetailItem("9", 499.95), RetailItem("10", 519.00), RetailItem("11", 209.50)))
orderProcessor ! RequestForQuotation("135", Vector(RetailItem("12", 0.97), RetailItem("13", 9.50), RetailItem("14", 1.99)))
orderProcessor ! RequestForQuotation("140", Vector(RetailItem("15", 1295.50), RetailItem("16", 9.50), RetailItem("17", 599.99), RetailItem("18", 249.95), RetailItem("19", 789.99)))
println("Scatter-Gather: is completed.")
Await.ready(system.whenTerminated, 5.seconds) }
class MountaineeringSuppliesOrderProcessor(priceQuoteAggregator: ActorRef) extends Actor { val subscribers: scala.collection.mutable.Map[String, SubscribeToPriceQuoteRequests] = scala.collection.mutable.Map[String, SubscribeToPriceQuoteRequests]()
def dispatch(rfq: RequestForQuotation): Unit = { subscribers.values.foreach { subscriber => val quoteProcessor = subscriber.quoteProcessor rfq.retailItems.foreach { retailItem => println("OrderProcessor: " + rfq.rfqId + " item: " + retailItem.itemId + " to: " + subscriber.quoterId) quoteProcessor ! RequestPriceQuote(rfq.rfqId, retailItem.itemId, retailItem.retailPrice, rfq.totalRetailPrice) } } }
override def receive: Receive = { case subscriber: SubscribeToPriceQuoteRequests => subscribers(subscriber.quoterId) = subscriber case priceQuote: PriceQuote => priceQuoteAggregator ! PriceQuoteFulfilled(priceQuote) println(s"OrderProcessor: received: $priceQuote") case rfq: RequestForQuotation => priceQuoteAggregator ! RequiredPriceQuotesForFulfillment(rfq.rfqId, subscribers.size * rfq.retailItems.size) dispatch(rfq) case bestPriceQuotation: BestPriceQuotation => println(s"OrderProcessor: received: $bestPriceQuotation") context.system.terminate() case message: Any => println(s"OrderProcessor: received unexpected message: $message") } }
class PriceQuoteAggregator extends Actor { val fulfilledPriceQuotes: scala.collection.mutable.Map[String, QuotationFulfillment] = scala.collection.mutable.Map[String, QuotationFulfillment]()
def bestPriceQuotationFrom(quotationFulfillment: QuotationFulfillment): BestPriceQuotation = { val bestPrices = scala.collection.mutable.Map[String, PriceQuote]()
quotationFulfillment.priceQuotes.foreach { priceQuote => if (bestPrices.contains(priceQuote.itemId)) { if (bestPrices(priceQuote.itemId).discountPrice > priceQuote.discountPrice) { bestPrices(priceQuote.itemId) = priceQuote } } else { bestPrices(priceQuote.itemId) = priceQuote } }
BestPriceQuotation(quotationFulfillment.rfqId, bestPrices.values.toVector) }
override def receive: Receive = { case required: RequiredPriceQuotesForFulfillment => fulfilledPriceQuotes(required.rfqId) = QuotationFulfillment(required.rfqId, required.quotesRequested, Vector(), sender) val duration = Duration.create(2, TimeUnit.SECONDS) context.system.scheduler.scheduleOnce(duration, self, PriceQuoteTimedOut(required.rfqId)) case priceQuoteFulfilled: PriceQuoteFulfilled => priceQuoteRequestFulfilled(priceQuoteFulfilled) println(s"PriceQuoteAggregator: fulfilled price quote: $PriceQuoteFulfilled") case priceQuoteTimedOut: PriceQuoteTimedOut => priceQuoteRequestTimedOut(priceQuoteTimedOut.rfqId) case message: Any => println(s"PriceQuoteAggregator: received unexpected message: $message") }
def priceQuoteRequestFulfilled(priceQuoteFulfilled: PriceQuoteFulfilled): Unit = { if (fulfilledPriceQuotes.contains(priceQuoteFulfilled.priceQuote.rfqId)) { val previousFulfillment = fulfilledPriceQuotes(priceQuoteFulfilled.priceQuote.rfqId) val currentPriceQuotes = previousFulfillment.priceQuotes :+ priceQuoteFulfilled.priceQuote val currentFulfillment = QuotationFulfillment( previousFulfillment.rfqId, previousFulfillment.quotesRequested, currentPriceQuotes, previousFulfillment.requester)
if (currentPriceQuotes.size >= currentFulfillment.quotesRequested) { quoteBestPrice(currentFulfillment) } else { fulfilledPriceQuotes(priceQuoteFulfilled.priceQuote.rfqId) = currentFulfillment } } }
def priceQuoteRequestTimedOut(rfqId: String): Unit = { if (fulfilledPriceQuotes.contains(rfqId)) { quoteBestPrice(fulfilledPriceQuotes(rfqId)) } }
def quoteBestPrice(quotationFulfillment: QuotationFulfillment): Unit = { if (fulfilledPriceQuotes.contains(quotationFulfillment.rfqId)) { quotationFulfillment.requester ! bestPriceQuotationFrom(quotationFulfillment) fulfilledPriceQuotes.remove(quotationFulfillment.rfqId) } } }
class BudgetHikersPriceQuotes(priceQuoteRequestPublisher: ActorRef) extends Actor { val quoterId: String = self.path.name priceQuoteRequestPublisher ! SubscribeToPriceQuoteRequests(quoterId, self)
override def receive: Receive = { case rpq: RequestPriceQuote => if (rpq.orderTotalRetailPrice < 1000.00) { val discount = discountPercentage(rpq.orderTotalRetailPrice) * rpq.retailPrice sender ! PriceQuote(quoterId, rpq.rfqId, rpq.itemId, rpq.retailPrice, rpq.retailPrice - discount) } else { println(s"BudgetHikersPriceQuotes: ignoring: $rpq") }
case message: Any => println(s"BudgetHikersPriceQuotes: received unexpected message: $message") }
def discountPercentage(orderTotalRetailPrice: Double): Double = { if (orderTotalRetailPrice <= 100.00) 0.02 else if (orderTotalRetailPrice <= 399.99) 0.03 else if (orderTotalRetailPrice <= 499.99) 0.05 else if (orderTotalRetailPrice <= 799.99) 0.07 else 0.075 } }
class HighSierraPriceQuotes(priceQuoteRequestPublisher: ActorRef) extends Actor { val quoterId: String = self.path.name priceQuoteRequestPublisher ! SubscribeToPriceQuoteRequests(quoterId, self)
override def receive: Receive = { case rpq: RequestPriceQuote => val discount = discountPercentage(rpq.orderTotalRetailPrice) * rpq.retailPrice sender ! PriceQuote(quoterId, rpq.rfqId, rpq.itemId, rpq.retailPrice, rpq.retailPrice - discount)
case message: Any => println(s"HighSierraPriceQuotes: received unexpected message: $message") }
def discountPercentage(orderTotalRetailPrice: Double): Double = { if (orderTotalRetailPrice <= 150.00) 0.015 else if (orderTotalRetailPrice <= 499.99) 0.02 else if (orderTotalRetailPrice <= 999.99) 0.03 else if (orderTotalRetailPrice <= 4999.99) 0.04 else 0.05 } }
class MountainAscentPriceQuotes(priceQuoteRequestPublisher: ActorRef) extends Actor { val quoterId: String = self.path.name priceQuoteRequestPublisher ! SubscribeToPriceQuoteRequests(quoterId, self)
override def receive: Receive = { case rpq: RequestPriceQuote => val discount = discountPercentage(rpq.orderTotalRetailPrice) * rpq.retailPrice sender ! PriceQuote(quoterId, rpq.rfqId, rpq.itemId, rpq.retailPrice, rpq.retailPrice - discount)
case message: Any => println(s"MountainAscentPriceQuotes: received unexpected message: $message") }
def discountPercentage(orderTotalRetailPrice: Double): Double = { if (orderTotalRetailPrice <= 99.99) 0.01 else if (orderTotalRetailPrice <= 199.99) 0.02 else if (orderTotalRetailPrice <= 499.99) 0.03 else if (orderTotalRetailPrice <= 799.99) 0.04 else if (orderTotalRetailPrice <= 999.99) 0.045 else if (orderTotalRetailPrice <= 2999.99) 0.0475 else 0.05 } }
class PinnacleGearPriceQuotes(priceQuoteRequestPublisher: ActorRef) extends Actor { val quoterId: String = self.path.name priceQuoteRequestPublisher ! SubscribeToPriceQuoteRequests(quoterId, self)
override def receive: Receive = { case rpq: RequestPriceQuote => val discount = discountPercentage(rpq.orderTotalRetailPrice) * rpq.retailPrice sender ! PriceQuote(quoterId, rpq.rfqId, rpq.itemId, rpq.retailPrice, rpq.retailPrice - discount)
case message: Any => println(s"PinnacleGearPriceQuotes: received unexpected message: $message") }
def discountPercentage(orderTotalRetailPrice: Double): Double = { if (orderTotalRetailPrice <= 299.99) 0.015 else if (orderTotalRetailPrice <= 399.99) 0.0175 else if (orderTotalRetailPrice <= 499.99) 0.02 else if (orderTotalRetailPrice <= 999.99) 0.03 else if (orderTotalRetailPrice <= 1199.99) 0.035 else if (orderTotalRetailPrice <= 4999.99) 0.04 else if (orderTotalRetailPrice <= 7999.99) 0.05 else 0.06 } }
class RockBottomOuterwearPriceQuotes(priceQuoteRequestPublisher: ActorRef) extends Actor { val quoterId: String = self.path.name priceQuoteRequestPublisher ! SubscribeToPriceQuoteRequests(quoterId, self)
override def receive: Receive = { case rpq: RequestPriceQuote => if (rpq.orderTotalRetailPrice < 2000.00) { val discount = discountPercentage(rpq.orderTotalRetailPrice) * rpq.retailPrice sender ! PriceQuote(quoterId, rpq.rfqId, rpq.itemId, rpq.retailPrice, rpq.retailPrice - discount) } else { println(s"RockBottomOuterwearPriceQuotes: ignoring: $rpq") }
case message: Any => println(s"RockBottomOuterwearPriceQuotes: received unexpected message: $message") }
def discountPercentage(orderTotalRetailPrice: Double): Double = { if (orderTotalRetailPrice <= 100.00) 0.015 else if (orderTotalRetailPrice <= 399.99) 0.02 else if (orderTotalRetailPrice <= 499.99) 0.03 else if (orderTotalRetailPrice <= 799.99) 0.04 else if (orderTotalRetailPrice <= 999.99) 0.05 else if (orderTotalRetailPrice <= 2999.99) 0.06 else if (orderTotalRetailPrice <= 4999.99) 0.07 else if (orderTotalRetailPrice <= 5999.99) 0.075 else 0.08 } }
|