本文整理汇总了Scala中com.lightbend.lagom.scaladsl.persistence.PersistentEntityRegistry类的典型用法代码示例。如果您正苦于以下问题:Scala PersistentEntityRegistry类的具体用法?Scala PersistentEntityRegistry怎么用?Scala PersistentEntityRegistry使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PersistentEntityRegistry类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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)
}
}
示例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)
}
}
}
示例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)
}
示例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))
}
}
示例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))
}
}
示例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
}
}
}
}
示例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
}
}
}
}
示例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
}
}
}
}
示例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)
}
}
示例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)
}
}
示例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)
}
}
示例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))
}
}
示例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)
}
}
示例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)
}