本文整理汇总了Scala中com.lightbend.lagom.scaladsl.persistence.PersistentEntityRegistry类的典型用法代码示例。如果您正苦于以下问题:Scala PersistentEntityRegistry类的具体用法?Scala PersistentEntityRegistry怎么用?Scala PersistentEntityRegistry使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PersistentEntityRegistry类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。
示例1: PlayerServiceImpl
//设置package包名称以及导入依赖的类
package com.chriswk.gameranker.player.impl
import java.util.UUID
import akka.actor.ActorSystem
import akka.persistence.cassandra.query.scaladsl.CassandraReadJournal
import akka.persistence.query.PersistenceQuery
import akka.stream.Materializer
import akka.stream.scaladsl.Sink
import com.chriswk.gameranker.player.api
import com.chriswk.gameranker.player.api.PlayerService
import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.api.transport.NotFound
import com.lightbend.lagom.scaladsl.persistence.PersistentEntityRegistry
import scala.concurrent.ExecutionContext
class PlayerServiceImpl(registry: PersistentEntityRegistry, system: ActorSystem)(implicit ec: ExecutionContext, mat: Materializer) extends PlayerService {
private val currentIdsQuery = PersistenceQuery(system).readJournalFor[CassandraReadJournal](CassandraReadJournal.Identifier)
override def createPlayer = ServiceCall { createPlayer =>
val playerId = UUID.randomUUID()
refFor(playerId).ask(CreatePlayer(createPlayer.name)).map { _ =>
api.Player(playerId, createPlayer.name)
}
}
override def getPlayer(playerId: UUID) = ServiceCall { _ =>
refFor(playerId).ask(GetPlayer).map {
case Some(player) => api.Player(playerId, player.name)
case None => throw NotFound(s"Player with id $playerId")
}
}
private def refFor(playerId: UUID) = registry.refFor[PlayerEntity](playerId.toString)
override def getPlayers = ServiceCall { _ =>
currentIdsQuery.currentPersistenceIds()
.filter(_.startsWith("PlayerEntity|"))
.mapAsync(4) { id =>
val entityId = id.split("\\|", 2).last
registry.refFor[PlayerEntity](entityId)
.ask(GetPlayer)
.map(_.map(player => api.Player(UUID.fromString(entityId), player.name)))
}
.collect {
case Some(p) => p
}
.runWith(Sink.seq)
}
}
开发者ID:chriswk,项目名称:gameranker,代码行数:53,代码来源:PlayerServiceImpl.scala
示例2: HelloServiceImpl
//设置package包名称以及导入依赖的类
package sample.helloworld.impl
import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.api.broker.Topic
import com.lightbend.lagom.scaladsl.broker.TopicProducer
import com.lightbend.lagom.scaladsl.persistence.{EventStreamElement, PersistentEntityRegistry}
import sample.helloworld.api.model.GreetingMessage
import sample.helloworld.api.HelloService
class HelloServiceImpl(persistentEntityRegistry: PersistentEntityRegistry) extends HelloService {
override def hello(id: String) = ServiceCall { _ =>
// Look up the Hello entity for the given ID.
val ref = persistentEntityRegistry.refFor[HelloEntity](id)
// Ask the entity the Hello command.
ref.ask(Hello(id, None))
}
override def useGreeting(id: String) = ServiceCall { request =>
// Look up the Hello entity for the given ID.
val ref = persistentEntityRegistry.refFor[HelloEntity](id)
// Tell the entity to use the greeting message specified.
ref.ask(UseGreetingMessage(request.message))
}
override def greetingsTopic(): Topic[GreetingMessage] = {
TopicProducer.singleStreamWithOffset {
offset =>
persistentEntityRegistry.eventStream(HelloEventTag.instance, offset)
.map(ev => (convertEvent(ev), offset))
}
}
private def convertEvent(helloEvent: EventStreamElement[HelloEvent]): GreetingMessage = {
helloEvent.event match {
case GreetingMessageChanged(msg) => GreetingMessage(msg)
}
}
}
开发者ID:knoldus,项目名称:lagom-scala-wordcount.g8,代码行数:44,代码来源:HelloServiceImpl.scala
示例3: HelloConsumerServiceImpl
//设置package包名称以及导入依赖的类
package sample.helloworldconsumer.impl
import akka.stream.scaladsl.Flow
import akka.{Done, NotUsed}
import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.persistence.PersistentEntityRegistry
import sample.helloworld.api.HelloService
import sample.helloworld.api.model.GreetingMessage
import sample.helloworldconsumer.api.HelloConsumerService
import sample.helloworldconsumer.impl.repositories.MessageRepository
class HelloConsumerServiceImpl (registery: PersistentEntityRegistry ,helloService: HelloService ,msgRepository:MessageRepository) extends HelloConsumerService {
helloService.greetingsTopic
.subscribe
.atLeastOnce(
Flow[GreetingMessage].map{ msg =>
putGreetingMessage(msg)
Done
}
)
var lastObservedMessage: String = _
private def putGreetingMessage(greetingMessage: GreetingMessage) = {
println(s"obersrve new message ${greetingMessage.message}")
entityRef(greetingMessage.message.toString).ask(SaveNewMessage(greetingMessage.message))
lastObservedMessage = greetingMessage.message
}
override def findTopHundredWordCounts(): ServiceCall[NotUsed, Map[String, Int]] = ServiceCall {
//fetch top 100 message and perform word count
req => msgRepository.fetchAndCountWordsFromMessages(100)
}
override def foo():ServiceCall[NotUsed, String] = ServiceCall{
req => scala.concurrent.Future.successful(lastObservedMessage)
}
private def entityRef(id: String) = registery.refFor[MessageEntity](id)
}
开发者ID:knoldus,项目名称:lagom-scala-wordcount.g8,代码行数:42,代码来源:HelloConsumerServiceImpl.scala
示例4: LagomhandsondevelopmentServiceImpl
//设置package包名称以及导入依赖的类
package com.example.lagomhandsondevelopment.impl
import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.persistence.PersistentEntityRegistry
import com.example.lagomhandsondevelopment.api.LagomhandsondevelopmentService
class LagomhandsondevelopmentServiceImpl(persistentEntityRegistry: PersistentEntityRegistry) extends LagomhandsondevelopmentService {
override def hello(id: String) = ServiceCall { _ =>
// Look up the lagom-hands-on-development entity for the given ID.
val ref = persistentEntityRegistry.refFor[LagomhandsondevelopmentEntity](id)
// Ask the entity the Hello command.
ref.ask(Hello(id, None))
}
override def useGreeting(id: String) = ServiceCall { request =>
// Look up the lagom-hands-on-development entity for the given ID.
val ref = persistentEntityRegistry.refFor[LagomhandsondevelopmentEntity](id)
// Tell the entity to use the greeting message specified.
ref.ask(UseGreetingMessage(request.message))
}
}
开发者ID:negokaz,项目名称:lagom-hands-on-development.scala,代码行数:26,代码来源:LagomhandsondevelopmentServiceImpl.scala
示例5: AccountServiceImpl
//设置package包名称以及导入依赖的类
package org.ioreskovic.greatmaterialcontinuum.impl
import akka.{Done, NotUsed}
import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.persistence.PersistentEntityRegistry
import org.ioreskovic.greatmaterialcontinuum.api.{Account, AccountService}
import org.ioreskovic.greatmaterialcontinuum.impl.cmd.acc._
import org.ioreskovic.greatmaterialcontinuum.impl.ent.acc.AccountEntity
class AccountServiceImpl(persistentEntityRegistry: PersistentEntityRegistry) extends AccountService {
override def createAccount(): ServiceCall[Account, Done] = ServiceCall { account =>
persistentEntityRegistry.refFor[AccountEntity](account.username).ask(CreateAccount(account))
}
override def deleteAccount(username: String): ServiceCall[NotUsed, Done] = ServiceCall { _ =>
persistentEntityRegistry.refFor[AccountEntity](username).ask(DeleteAccount(username))
}
override def activateAccount(username: String): ServiceCall[NotUsed, Done] = ServiceCall { _ =>
persistentEntityRegistry.refFor[AccountEntity](username).ask(ActivateAccount(username))
}
override def deactivateAccount(username: String): ServiceCall[NotUsed, Done] = ServiceCall { _ =>
persistentEntityRegistry.refFor[AccountEntity](username).ask(DeactivateAccount(username))
}
override def getAccount(username: String): ServiceCall[NotUsed, Account] = ServiceCall { _ =>
persistentEntityRegistry.refFor[AccountEntity](username).ask(RetrieveAccount(username))
}
}
开发者ID:ioreskovic,项目名称:great-material-continuum,代码行数:31,代码来源:AccountServiceImpl.scala
示例6: HelloServiceImpl
//设置package包名称以及导入依赖的类
package se.hultner.hello.impl
import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.persistence.PersistentEntityRegistry
import se.hultner.hello.api.HelloService
class HelloServiceImpl(persistentEntityRegistry: PersistentEntityRegistry) extends HelloService {
override def hello(id: String) = ServiceCall { _ =>
// Look up the Hello entity for the given ID.
val ref = persistentEntityRegistry.refFor[HelloEntity](id)
// Ask the entity the Hello command.
ref.ask(Hello(id, None))
}
override def useGreeting(id: String) = ServiceCall { request =>
// Look up the Hello entity for the given ID.
val ref = persistentEntityRegistry.refFor[HelloEntity](id)
// Tell the entity to use the greeting message specified.
ref.ask(UseGreetingMessage(request.message))
}
}
开发者ID:Hultner,项目名称:hello_scala_microservices,代码行数:26,代码来源:HelloServiceImpl.scala
示例7: BasketServiceImpl
//设置package包名称以及导入依赖的类
package demo.impl.basket
import akka.NotUsed
import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.api.broker.Topic
import com.lightbend.lagom.scaladsl.broker.TopicProducer
import com.lightbend.lagom.scaladsl.persistence.PersistentEntity.InvalidCommandException
import com.lightbend.lagom.scaladsl.persistence.{EventStreamElement, PersistentEntityRegistry}
import demo.api.basket.{Basket, BasketService, ExtraTransportExceptions, Item}
import scala.collection.immutable
import scala.concurrent.ExecutionContext
class BasketServiceImpl(persistentEntities: PersistentEntityRegistry)(implicit ec: ExecutionContext)
extends BasketService with ExtraTransportExceptions{
override def getBasket(basketId: String): ServiceCall[NotUsed, Basket] = ServiceCall { req =>
persistentEntities.refFor[BasketEntity](basketId).ask(GetBasket)
}
override def addItem(basketId: String): ServiceCall[Item, NotUsed] = ServiceCall { item =>
persistentEntities.refFor[BasketEntity](basketId).ask(AddItem(item))
.map(_ => NotUsed)
.recoverWith {
case e: InvalidCommandException => throw BadRequest(e.message)
}
}
override def getTotal(basketId: String): ServiceCall[NotUsed, Int] = ServiceCall { req =>
persistentEntities.refFor[BasketEntity](basketId).ask(GetTotal)
}
override def clearAll(basketId: String): ServiceCall[NotUsed, NotUsed] = ServiceCall { req =>
persistentEntities.refFor[BasketEntity](basketId).ask(ClearAll)
.map(x => NotUsed)
.recoverWith {
case e: InvalidCommandException => throw BadRequest(e.message)
}
}
override def placeOrder(basketId: String): ServiceCall[NotUsed, NotUsed] = ServiceCall { req =>
persistentEntities.refFor[BasketEntity](basketId).ask(PlaceOrder)
.map(x => NotUsed)
.recoverWith {
case e: InvalidCommandException => throw BadRequest(e.message)
}
}
override def placedOrders: Topic[demo.api.basket.OrderPlaced] =
TopicProducer.taggedStreamWithOffset(BasketEntityEvent.Tag.allTags.to[immutable.Seq]) { (tag, offset) =>
persistentEntities.eventStream(tag, offset).collect {
case EventStreamElement(t, OrderPlaced(id, basket), o) => {
demo.api.basket.OrderPlaced(id, basket) -> o
}
}
}
}
开发者ID:tommpy,项目名称:demo-lagom-checkout,代码行数:57,代码来源:BasketServiceImpl.scala
示例8: BasketServiceImpl
//设置package包名称以及导入依赖的类
package demo.impl.basket
import akka.NotUsed
import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.api.broker.Topic
import com.lightbend.lagom.scaladsl.broker.TopicProducer
import com.lightbend.lagom.scaladsl.persistence.PersistentEntity.InvalidCommandException
import com.lightbend.lagom.scaladsl.persistence.{EventStreamElement, PersistentEntityRegistry}
import demo.api.basket.{Basket, BasketService, ExtraTransportExceptions, Item}
import scala.collection.immutable
import scala.concurrent.ExecutionContext
class BasketServiceImpl(persistentEntities: PersistentEntityRegistry)(implicit ec: ExecutionContext)
extends BasketService with ExtraTransportExceptions{
override def getBasket(basketId: String): ServiceCall[NotUsed, Basket] = ServiceCall { req =>
persistentEntities.refFor[BasketEntity](basketId).ask(GetBasket)
}
override def addItem(basketId: String): ServiceCall[Item, NotUsed] = ServiceCall { item =>
persistentEntities.refFor[BasketEntity](basketId).ask(AddItem(item))
.map(_ => NotUsed)
.recoverWith {
case e: InvalidCommandException => throw BadRequest(e.message)
}
}
override def getTotal(basketId: String): ServiceCall[NotUsed, Int] = ServiceCall { req =>
persistentEntities.refFor[BasketEntity](basketId).ask(GetTotal)
}
override def clearAll(basketId: String): ServiceCall[NotUsed, NotUsed] = ServiceCall { req =>
persistentEntities.refFor[BasketEntity](basketId).ask(ClearAll)
.map(x => NotUsed)
.recoverWith {
case e: InvalidCommandException => throw BadRequest(e.message)
}
}
override def placeOrder(basketId: String): ServiceCall[NotUsed, NotUsed] = ServiceCall { req =>
persistentEntities.refFor[BasketEntity](basketId).ask(PlaceOrder).map(_ => NotUsed)
}
override def placedOrders: Topic[demo.api.basket.OrderPlaced] =
TopicProducer.taggedStreamWithOffset(BasketEntityEvent.Tag.allTags.to[immutable.Seq]) { (tag, offset) =>
persistentEntities.eventStream(tag, offset).collect {
case EventStreamElement(t, OrderPlaced(id, basket), o) => {
demo.api.basket.OrderPlaced(id, basket) -> o
}
}
}
}
开发者ID:tommpy,项目名称:demo-lagom-checkout,代码行数:53,代码来源:BasketServiceImpl.scala
示例9: BasketServiceImpl
//设置package包名称以及导入依赖的类
package demo.impl.basket
import akka.NotUsed
import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.api.broker.Topic
import com.lightbend.lagom.scaladsl.broker.TopicProducer
import com.lightbend.lagom.scaladsl.persistence.{EventStreamElement, PersistentEntityRegistry}
import demo.api.basket.{Basket, BasketService, Item}
import scala.collection.immutable
import scala.concurrent.ExecutionContext
class BasketServiceImpl(persistentEntities: PersistentEntityRegistry)(implicit ec: ExecutionContext)
extends BasketService {
override def getBasket(basketId: String): ServiceCall[NotUsed, Basket] = ServiceCall { req =>
persistentEntities.refFor[BasketEntity](basketId).ask(GetBasket)
}
override def addItem(basketId: String): ServiceCall[Item, NotUsed] = ServiceCall { item =>
persistentEntities.refFor[BasketEntity](basketId).ask(AddItem(item)).map(_ => NotUsed)
}
override def getTotal(basketId: String): ServiceCall[NotUsed, Int] = ServiceCall { req =>
persistentEntities.refFor[BasketEntity](basketId).ask(GetTotal)
}
override def clearAll(basketId: String): ServiceCall[NotUsed, NotUsed] = ServiceCall { req =>
persistentEntities.refFor[BasketEntity](basketId).ask(ClearAll).map(x => NotUsed)
}
override def placeOrder(basketId: String): ServiceCall[NotUsed, NotUsed] = ServiceCall { req =>
persistentEntities.refFor[BasketEntity](basketId).ask(PlaceOrder).map(_ => NotUsed)
}
override def placedOrders: Topic[demo.api.basket.OrderPlaced] =
TopicProducer.taggedStreamWithOffset(BasketEntityEvent.Tag.allTags.to[immutable.Seq]) { (tag, offset) =>
persistentEntities.eventStream(tag, offset).collect {
case EventStreamElement(t, OrderPlaced(id, basket), o) => {
demo.api.basket.OrderPlaced(id, basket) -> o
}
}
}
}
开发者ID:tommpy,项目名称:demo-lagom-checkout,代码行数:44,代码来源:BasketServiceImpl.scala
示例10: BasketServiceImpl
//设置package包名称以及导入依赖的类
package demo.impl.basket
import akka.NotUsed
import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.persistence.PersistentEntityRegistry
import demo.api.basket.{Basket, BasketService, Item}
import scala.concurrent.ExecutionContext
class BasketServiceImpl(persistentEntities: PersistentEntityRegistry)(implicit ec: ExecutionContext)
extends BasketService {
override def getBasket(basketId: String): ServiceCall[NotUsed, Basket] = ServiceCall { req =>
persistentEntities.refFor[BasketEntity](basketId).ask(GetBasket)
}
override def addItem(basketId: String): ServiceCall[Item, NotUsed] = ServiceCall { item =>
persistentEntities.refFor[BasketEntity](basketId).ask(AddItem(item)).map(_ => NotUsed)
}
override def getTotal(basketId: String): ServiceCall[NotUsed, Int] = ServiceCall { req =>
persistentEntities.refFor[BasketEntity](basketId).ask(GetTotal)
}
}
开发者ID:tommpy,项目名称:demo-lagom-checkout,代码行数:24,代码来源:BasketServiceImpl.scala
示例11: BasketServiceImpl
//设置package包名称以及导入依赖的类
package demo.impl.basket
import akka.NotUsed
import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.api.transport.NotFound
import com.lightbend.lagom.scaladsl.persistence.PersistentEntityRegistry
import demo.api.basket.{Basket, BasketService, Item}
import scala.concurrent.{ExecutionContext, Future}
class BasketServiceImpl(persistentEntities: PersistentEntityRegistry)(implicit ec: ExecutionContext)
extends BasketService {
private var baskets = Map[String, Basket]()
override def getBasket(basketId: String): ServiceCall[NotUsed, Basket] = ServiceCall { req =>
baskets.synchronized {
Future.successful(baskets.getOrElse(basketId, Basket(Seq(), 0)))
}
}
override def addItem(basketId: String): ServiceCall[Item, NotUsed] = ServiceCall { item =>
baskets.synchronized {
val newItems = baskets.get(basketId).toSeq.flatMap(_.items) :+ item
baskets = baskets.+(basketId -> Basket(newItems, newItems.map(_.price).sum))
}
Future.successful(NotUsed)
}
}
开发者ID:tommpy,项目名称:demo-lagom-checkout,代码行数:29,代码来源:BasketServiceImpl.scala
示例12: BasketServiceImpl
//设置package包名称以及导入依赖的类
package demo.impl.basket
import akka.NotUsed
import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.persistence.PersistentEntityRegistry
import demo.api.basket.{Basket, BasketService, Item}
import scala.concurrent.ExecutionContext
class BasketServiceImpl(persistentEntities: PersistentEntityRegistry)(implicit ec: ExecutionContext)
extends BasketService {
override def getBasket(basketId: String): ServiceCall[NotUsed, Basket] = ServiceCall { req =>
persistentEntities.refFor[BasketEntity](basketId).ask(GetBasket)
}
override def addItem(basketId: String): ServiceCall[Item, NotUsed] = ServiceCall { item =>
persistentEntities.refFor[BasketEntity](basketId).ask(AddItem(item)).map(_ => NotUsed)
}
override def getTotal(basketId: String): ServiceCall[NotUsed, Int] = ServiceCall { req =>
persistentEntities.refFor[BasketEntity](basketId).ask(GetTotal)
}
override def clearAll(basketId: String): ServiceCall[NotUsed, NotUsed] = ServiceCall { req =>
persistentEntities.refFor[BasketEntity](basketId).ask(ClearAll).map(x => NotUsed)
}
}
开发者ID:tommpy,项目名称:demo-lagom-checkout,代码行数:28,代码来源:BasketServiceImpl.scala
示例13: SsServiceImpl
//设置package包名称以及导入依赖的类
package com.ss.ss.impl
import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.persistence.PersistentEntityRegistry
import com.ss.ss.api.SsService
class SsServiceImpl(persistentEntityRegistry: PersistentEntityRegistry) extends SsService {
override def hello(id: String) = ServiceCall { _ =>
// Look up the ss entity for the given ID.
val ref = persistentEntityRegistry.refFor[SsEntity](id)
// Ask the entity the Hello command.
ref.ask(Hello(id, None))
}
override def useGreeting(id: String) = ServiceCall { request =>
// Look up the ss entity for the given ID.
val ref = persistentEntityRegistry.refFor[SsEntity](id)
// Tell the entity to use the greeting message specified.
ref.ask(UseGreetingMessage(request.message))
}
}
开发者ID:ytaras,项目名称:iot_lagom_poc,代码行数:26,代码来源:SsServiceImpl.scala
示例14: MappingServiceImpl
//设置package包名称以及导入依赖的类
package com.ss.mapping.impl
import akka.NotUsed
import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.persistence.{PersistentEntityRegistry, ReadSide}
import com.ss.mapping.api.{MappingService, SensorMapping}
import scala.concurrent.ExecutionContext
class MappingServiceImpl(persistentEntityRegistry: PersistentEntityRegistry)(implicit ec: ExecutionContext) extends MappingService {
persistentEntityRegistry.register(new SensorMappingPersistentEntity)
override def sensorMapping(nodeId: Long, sensorId: Int): ServiceCall[NotUsed, SensorMapping] = ServiceCall { _ =>
mappingRef(nodeId, sensorId)
.ask(LoadMapping)
}
override def unregisterMapping(nodeId: Long, sensorId: Int): ServiceCall[NotUsed, NotUsed] = ServiceCall { _ =>
mappingRef(nodeId, sensorId).ask(UnregisterMapping).map(_ => NotUsed)
}
private def entityId(nodeId: Long, sensorId: Int) = s"$nodeId:$sensorId"
private def mappingRef(nodeId: Long, sensorId: Int) =
persistentEntityRegistry.refFor[SensorMappingPersistentEntity](entityId(nodeId, sensorId))
override def registerMapping(nodeId: Long, sensorId: Int): ServiceCall[SensorMapping, NotUsed] = ServiceCall { sm =>
mappingRef(nodeId, sensorId)
.ask(RegisterMapping(sm)).map(_ => NotUsed)
}
}
开发者ID:ytaras,项目名称:iot_lagom_poc,代码行数:33,代码来源:MappingServiceImpl.scala
示例15: ItemServiceSubscriber
//设置package包名称以及导入依赖的类
package com.example.auction.bidding.impl
import java.util.UUID
import akka.Done
import akka.stream.scaladsl.Flow
import com.example.auction.item.api.{ItemEvent, ItemService}
import com.example.auction.item.api
import com.lightbend.lagom.scaladsl.persistence.PersistentEntityRegistry
import scala.concurrent.Future
class ItemServiceSubscriber(persistentEntityRegistry: PersistentEntityRegistry, itemService: ItemService) {
itemService.itemEvents.subscribe.atLeastOnce(Flow[ItemEvent].mapAsync(1) {
case as: api.AuctionStarted =>
val auction = Auction(
itemId = as.itemId,
creator = as.creator,
reservePrice = as.reservePrice,
increment = as.increment,
startTime = as.startDate,
endTime = as.endDate
)
entityRef(as.itemId).ask(StartAuction(auction));
case api.AuctionCancelled(itemId) =>
entityRef(itemId).ask(CancelAuction)
case other =>
Future.successful(Done)
})
private def entityRef(itemId: UUID) = persistentEntityRegistry.refFor[AuctionEntity](itemId.toString)
}
开发者ID:lagom,项目名称:online-auction-scala,代码行数:39,代码来源:ItemServiceSubscriber.scala
示例16: UserServiceImpl
//设置package包名称以及导入依赖的类
package com.example.auction.user.impl
import java.util.UUID
import akka.actor.ActorSystem
import akka.persistence.cassandra.query.scaladsl.CassandraReadJournal
import akka.persistence.query.PersistenceQuery
import akka.stream.Materializer
import akka.stream.scaladsl.Sink
import com.example.auction.user.api
import com.example.auction.user.api.UserService
import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.api.transport.NotFound
import com.lightbend.lagom.scaladsl.persistence.PersistentEntityRegistry
import scala.concurrent.ExecutionContext
class UserServiceImpl(registry: PersistentEntityRegistry, system: ActorSystem)(implicit ec: ExecutionContext, mat: Materializer) extends UserService {
private val currentIdsQuery = PersistenceQuery(system).readJournalFor[CassandraReadJournal](CassandraReadJournal.Identifier)
override def createUser = ServiceCall { createUser =>
val userId = UUID.randomUUID()
refFor(userId).ask(CreateUser(createUser.name)).map { _ =>
api.User(userId, createUser.name)
}
}
override def getUser(userId: UUID) = ServiceCall { _ =>
refFor(userId).ask(GetUser).map {
case Some(user) =>
api.User(userId, user.name)
case None =>
throw NotFound(s"User with id $userId")
}
}
override def getUsers = ServiceCall { _ =>
// Note this should never make production....
currentIdsQuery.currentPersistenceIds()
.filter(_.startsWith("UserEntity|"))
.mapAsync(4) { id =>
val entityId = id.split("\\|", 2).last
registry.refFor[UserEntity](entityId)
.ask(GetUser)
.map(_.map(user => api.User(UUID.fromString(entityId), user.name)))
}.collect {
case Some(user) => user
}
.runWith(Sink.seq)
}
private def refFor(userId: UUID) = registry.refFor[UserEntity](userId.toString)
}
开发者ID:lagom,项目名称:online-auction-scala,代码行数:55,代码来源:UserServiceImpl.scala
示例17: MonitorServiceImpl
//设置package包名称以及导入依赖的类
package org.wex.cmsfs.monitor.impl
import java.util.UUID
import akka.persistence.query.Offset
import com.datastax.driver.core.utils.UUIDs
import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.broker.TopicProducer
import com.lightbend.lagom.scaladsl.persistence.{EventStreamElement, PersistentEntityRegistry}
import org.wex.cmsfs.monitor.api
import org.wex.cmsfs.monitor.api._
import scala.concurrent.{ExecutionContext, Future}
class MonitorServiceImpl(registry: PersistentEntityRegistry)(implicit ec: ExecutionContext) extends MonitorService {
override def createMonitor: ServiceCall[Monitor, Monitor] = ServiceCall { m =>
val id = UUIDs.timeBased()
val monitor = Monitor(Some(id), m.cron, m.mode, m.monitorId, m.connectorId, MonitorStatus.Created)
val command = CreateMonitor(monitor)
entityRef(id).ask(command).map(_ => monitor)
}
override def monitorCollectEvents = TopicProducer.taggedStreamWithOffset(MonitorEvent.Tag.allTags.toList) { (tag, offset) =>
println(11)
registry.eventStream(tag, offset).mapAsync(1)(p => Future{
println(offset);
(api.MonitorCreated(UUIDs.random(), 1, 1), offset)
})
}
private def convertEvent(eventStreamElement: EventStreamElement[MonitorEvent]): Future[(api.MonitorEvent, Offset)] = {
eventStreamElement match {
case EventStreamElement(_, MonitorCreated(m), offset) =>
Future((api.MonitorCreated(m.id.get, m.monitorId, m.connectorId), offset))
}
}
private def entityRef(id: UUID) = entityRefString(id.toString)
private def entityRefString(id: String) = registry.refFor[MonitorEntity](id)
}
开发者ID:shinhwazx160,项目名称:test,代码行数:43,代码来源:MonitorServiceImpl.scala
示例18: LagompracticeServiceImpl
//设置package包名称以及导入依赖的类
package com.example.lagompractice.impl
import java.util.UUID
import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.persistence.PersistentEntityRegistry
import com.example.lagompractice.api.{LagompracticeService, Message => ApiMessage}
import com.fasterxml.uuid.Generators
import com.lightbend.lagom.scaladsl.api.transport.NotFound
import scala.concurrent.ExecutionContext
class LagompracticeServiceImpl(persistentEntityRegistry: PersistentEntityRegistry)(implicit ec: ExecutionContext) extends LagompracticeService {
override def hello(id: String) = ServiceCall { _ =>
// Look up the lagom-practice entity for the given ID.
val ref = persistentEntityRegistry.refFor[LagompracticeEntity](id)
// Ask the entity the Hello command.
ref.ask(Hello(id, None))
}
override def useGreeting(id: String) = ServiceCall { request =>
// Look up the lagom-practice entity for the given ID.
val ref = persistentEntityRegistry.refFor[LagompracticeEntity](id)
// Tell the entity to use the greeting message specified.
ref.ask(UseGreetingMessage(request.message))
}
override def getMessage(id: UUID) = ServiceCall { _ =>
val ref = persistentEntityRegistry.refFor[MessageEntity](id.toString)
ref.ask(GetMessage).map {
case Some(message) => ApiMessage(Some(message.id), message.message)
case None => throw NotFound(s"A message '$id' is not found.")
}
}
override def postMessage = ServiceCall { message =>
val uuid = Generators.timeBasedGenerator().generate()
val newMessage = Message(uuid, message.message)
val ref = persistentEntityRegistry.refFor[MessageEntity](uuid.toString)
ref.ask(CreateMessage(newMessage)).map { _ =>
ApiMessage(Some(newMessage.id), newMessage.message)
}
}
}
开发者ID:Saneyan,项目名称:lagom-practice,代码行数:48,代码来源:LagompracticeServiceImpl.scala
示例19: LagomdemoServiceImpl
//设置package包名称以及导入依赖的类
package com.dannyrobert.lagomdemo.impl
import java.util.concurrent.atomic.AtomicInteger
import akka.NotUsed
import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.persistence.PersistentEntityRegistry
import com.dannyrobert.lagomdemo.api.{FooResponse, LagomdemoService}
import scala.concurrent.Future
/**
* Implementation of the LagomdemoService.
*/
class LagomdemoServiceImpl(persistentEntityRegistry: PersistentEntityRegistry) extends LagomdemoService {
private final val counter = new AtomicInteger(0)
override def hello(id: String) = ServiceCall { _ =>
// Look up the lagom-demo entity for the given ID.
val ref = persistentEntityRegistry.refFor[LagomdemoEntity](id)
// Ask the entity the Hello command.
ref.ask(Hello(id, None))
}
override def useGreeting(id: String) = ServiceCall { request =>
// Look up the lagom-demo entity for the given ID.
val ref = persistentEntityRegistry.refFor[LagomdemoEntity](id)
// Tell the entity to use the greeting message specified.
ref.ask(UseGreetingMessage(request.message))
}
override def other(): ServiceCall[NotUsed, FooResponse] = ServiceCall(_ => Future.successful(FooResponse(counter.incrementAndGet())))
}
开发者ID:drobert,项目名称:lagom-prototype,代码行数:36,代码来源:LagomdemoServiceImpl.scala
示例20: AddServiceImpl
//设置package包名称以及导入依赖的类
package com.orion.add.impl
import akka.Done
import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.api.broker.Topic
import com.lightbend.lagom.scaladsl.broker.TopicProducer
import com.lightbend.lagom.scaladsl.persistence.{EventStreamElement, PersistentEntityRegistry}
import com.orion.add.api
import com.orion.add.api.AddService
class AddServiceImpl(persistentEntityRegistry: PersistentEntityRegistry) extends AddService {
override def add(): ServiceCall[api.AddNumber, String] = ServiceCall { request =>
val ref = persistentEntityRegistry.refFor[NumberEntity]("constant")
ref.ask(AddNumber(request.number))
}
override def curry(): ServiceCall[api.CurryWith, Done] = ServiceCall { request =>
val ref = persistentEntityRegistry.refFor[NumberEntity]("constant")
ref.ask(CurryWith(request.number))
}
override def additionTopic(): Topic[api.CurriedWith] =
TopicProducer.singleStreamWithOffset {
fromOffset =>
persistentEntityRegistry.eventStream(NumberEvent.Tags, fromOffset)
.map(ev => (convertEvent(ev), ev.offset))
}
private def convertEvent(numberEvent: EventStreamElement[NumberEvent]): api.CurriedWith = {
numberEvent.event match {
case CurriedWith(num) => api.CurriedWith(num)
}
}
}
开发者ID:utsavgupta,项目名称:add-lagom,代码行数:38,代码来源:AddServiceImpl.scala
注:本文中的com.lightbend.lagom.scaladsl.persistence.PersistentEntityRegistry类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论