本文整理汇总了Scala中com.lightbend.lagom.scaladsl.persistence.PersistentEntity类的典型用法代码示例。如果您正苦于以下问题:Scala PersistentEntity类的具体用法?Scala PersistentEntity怎么用?Scala PersistentEntity使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PersistentEntity类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: MessageEntity
//设置package包名称以及导入依赖的类
package sample.helloworldconsumer.impl
import java.time.LocalDateTime
import akka.Done
import com.lightbend.lagom.scaladsl.persistence.PersistentEntity
class MessageEntity extends PersistentEntity {
override type Command = MessageCommand[_]
override type Event = MessageEvent
override type State = MessageState
override def initialState = MessageState("", LocalDateTime.now().toString)
override def behavior: Behavior = {
case MessageState(msg, _) => Actions().onCommand[SaveNewMessage, Done]{
case (SaveNewMessage(msg), ctx, state) =>
println(s"observe new message from kafka and save ${msg}")
ctx.thenPersist( MessageSaved(msg)) { msgSaved: MessageSaved =>
ctx.reply(Done)
}
}.onEvent {
case (MessageSaved(message), state) =>
println(s"MessgaeSaved event fire ...")
MessageState(message, LocalDateTime.now().toString)
}
}
}
示例2: OrderEntityFormats
//设置package包名称以及导入依赖的类
package demo.impl.order
import akka.Done
import com.lightbend.lagom.scaladsl.persistence.PersistentEntity
import com.lightbend.lagom.scaladsl.persistence.PersistentEntity.ReplyType
import com.lightbend.lagom.scaladsl.playjson.JsonSerializer
import play.api.libs.json.{Format, Json}
object OrderEntityFormats {
implicit val orderItemsFormat: Format[OrderItems] = Json.format
implicit val createOrderFormat: Format[CreateOrder] = Json.format
implicit val getOrderFormat = JsonSerializer.emptySingletonFormat(GetOrder)
implicit val orderInitialisedFormat: Format[OrderInitialized] = Json.format
implicit val orderStateFormat: Format[OrderState] = Json.format
}
case class OrderItems (items: Seq[String])
trait OrderCommand
case class CreateOrder(items: Seq[String]) extends OrderCommand with ReplyType[Done]
case object GetOrder extends OrderCommand with ReplyType[OrderItems]
trait OrderEvent
case class OrderInitialized(items: Seq[String]) extends OrderEvent
case class OrderState(items: Seq[String])
object OrderState {
def empty = OrderState(Seq())
}
class Order extends PersistentEntity {
override type Command = OrderCommand
override type Event = OrderEvent
override type State = OrderState
override def initialState = OrderState.empty
override def behavior: Behavior = Actions()
.onCommand[CreateOrder, Done] {
case (CreateOrder(items), ctx, state) => {
ctx.thenPersist(OrderInitialized(items)){
evt => ctx.reply(Done)
}
}
}
.onReadOnlyCommand[GetOrder.type, OrderItems] {
case (GetOrder, ctx, state) => ctx.reply(OrderItems(state.items))
}
.onEvent{
case (OrderInitialized(items), state) => OrderState(items)
}
}
示例3: BasketEntityFormats
//设置package包名称以及导入依赖的类
package demo.impl.basket
import akka.{Done, NotUsed}
import com.lightbend.lagom.scaladsl.persistence.PersistentEntity.ReplyType
import com.lightbend.lagom.scaladsl.persistence.{AggregateEvent, AggregateEventTag, PersistentEntity}
import com.lightbend.lagom.scaladsl.playjson.JsonSerializer
import demo.api.basket.{Basket, Item}
import play.api.libs.json.{Format, Json}
object BasketEntityFormats {
implicit val addItemFormat: Format[AddItem] = Json.format
implicit val getBasketFormat: Format[GetBasket.type] = JsonSerializer.emptySingletonFormat(GetBasket)
implicit val itemAddedFormat: Format[ItemAdded] = Json.format
implicit val basketEntityStateFormat: Format[BasketEntityState] = Json.format
}
trait BasketEntityCommand
case class AddItem(item: Item) extends BasketEntityCommand with ReplyType[Done]
case object GetBasket extends BasketEntityCommand with ReplyType[Basket]
case class BasketEntityState(currentBasket: Basket)
object BasketEntityState{
val initial = BasketEntityState(Basket(Seq(), 0))
}
sealed trait BasketEntityEvent extends AggregateEvent[BasketEntityEvent] {
override def aggregateTag = BasketEntityEvent.Tag
}
object BasketEntityEvent {
val Tag = AggregateEventTag.sharded[BasketEntityEvent](4)
}
case class ItemAdded(item: Item) extends BasketEntityEvent
final class BasketEntity extends PersistentEntity {
override type Command = BasketEntityCommand
override type Event = BasketEntityEvent
override type State = BasketEntityState
override def initialState = BasketEntityState.initial
override def behavior: Behavior = {
Actions()
.onCommand[AddItem, Done] {
case (AddItem(item), ctx, state) =>
ctx.thenPersist(ItemAdded(item))(_ => ctx.reply(Done))
}
.onReadOnlyCommand[GetBasket.type, Basket] {
case (GetBasket, ctx, state) => ctx.reply(state.currentBasket)
}
.onEvent {
case (ItemAdded(item), state) => {
val newItems = state.currentBasket.items :+ item
state.copy(Basket(newItems, newItems.map(_.price).sum))
}
}
}
}
示例4: MonitorEntity
//设置package包名称以及导入依赖的类
package org.wex.cmsfs.monitor.impl
import akka.Done
import com.lightbend.lagom.scaladsl.persistence.PersistentEntity
import org.wex.cmsfs.monitor.api.Monitor
class MonitorEntity extends PersistentEntity {
override type Command = MonitorCommand
override type Event = MonitorEvent
override type State = Option[Monitor]
override def initialState = None
override def behavior: Behavior = {
case None => notCreated
}
private val notCreated = {
Actions().onCommand[CreateMonitor, Done] {
case (CreateMonitor(monitor), ctx, _) =>
ctx.thenPersist(MonitorCreated(monitor))(_ => ctx.reply(Done))
}.onEvent {
case (MonitorCreated(monitor), _) => Some(monitor)
}
}
}
示例5: ClientEntity
//设置package包名称以及导入依赖的类
package com.packt.chapter11.trip.impl
import java.util.Date
import akka.Done
import com.lightbend.lagom.scaladsl.persistence.PersistentEntity
class ClientEntity extends PersistentEntity {
override type Command = ClientCommand[_]
override type Event = ClientEvent
override type State = ClientState
override def initialState = ClientState(false, Nil)
override def behavior: Behavior =
Actions()
.onCommand[StartTrip.type, Done] {
case (_, ctx, state) if !state.tripInProgress =>
ctx.thenPersist(TripStarted(new Date().getTime)) { _ => ctx.reply(Done) }
case (_, ctx, _) =>
ctx.invalidCommand("The trip has started already.")
ctx.done
}
.onCommand[EndTrip.type, Done] {
case (_, ctx, state) if state.tripInProgress =>
ctx.thenPersist(TripEnded(new Date().getTime)) { _ => ctx.reply(Done) }
case (_, ctx, _) =>
ctx.invalidCommand("The trip has not started.")
ctx.done
}
.onCommand[AddLocation, Done] {
case (AddLocation(req), ctx, state) if state.tripInProgress =>
ctx.thenPersist(LocationAdded(Location(req.latitude, req.longitude))) { _ => ctx.reply(Done) }
case (_, ctx, _) =>
ctx.invalidCommand("The trip has not started.")
ctx.done
}
.onEvent {
case (TripStarted(_), _) => ClientState(true, Nil)
case (TripEnded(_), _) => ClientState(false, Nil)
case (LocationAdded(loc), state) => state.copy(locations = state.locations :+ loc)
}
}
示例6: CounterEntity
//设置package包名称以及导入依赖的类
package com.example.counter.impl
import akka.Done
import com.lightbend.lagom.scaladsl.persistence.PersistentEntity.ReplyType
import com.lightbend.lagom.scaladsl.persistence.{AggregateEvent, AggregateEventTag, PersistentEntity}
import com.lightbend.lagom.scaladsl.playjson.{JsonSerializer, JsonSerializerRegistry}
import play.api.libs.json.Json
import scala.collection.immutable.Seq
class CounterEntity extends PersistentEntity {
type Command = CounterCommand[_]
type Event = CounterEvent
type State = CounterState
def initialState = CounterState(0)
def behavior = {
case CounterState(_) => Actions()
.onCommand[Add, Done] { case (Add(x), ctx, _) => ctx.thenPersist(Added(x)) { _ => ctx.reply(Done) } }
.onReadOnlyCommand[GetState.type, Int] { case (GetState, ctx, CounterState(n)) => ctx.reply(n) }
.onEvent { case (Added(x), CounterState(n)) => CounterState(n + x) }
}
}
case class CounterState(n: Int)
object CounterState { implicit val format = Json.format[CounterState] }
sealed trait CounterEvent extends AggregateEvent[CounterEvent] { def aggregateTag = CounterEvent.Tag }
object CounterEvent { val Tag = AggregateEventTag[CounterEvent] }
case class Added(n: Int) extends CounterEvent
object Added { implicit val format = Json.format[Added] }
sealed trait CounterCommand[R] extends ReplyType[R]
case class Add(n: Int) extends CounterCommand[Done]
object Add { implicit val format = Json.format[Add] }
case object GetState extends CounterCommand[Int] { implicit val format = JsonSerializer.emptySingletonFormat(GetState) }
object CounterSerializerRegistry extends JsonSerializerRegistry {
override def serializers: Seq[JsonSerializer[_]] = Seq(
JsonSerializer[Add],
JsonSerializer[Added],
JsonSerializer[CounterState],
JsonSerializer[GetState.type]
)
}
示例7: RegisterClient
//设置package包名称以及导入依赖的类
package io.digitalcat.publictransportation.services.identity.impl
import com.lightbend.lagom.scaladsl.persistence.PersistentEntity
import io.digitalcat.publictransportation.services.common.response.GeneratedIdDone
import io.digitalcat.publictransportation.services.identity.api.response.IdentityStateDone
import play.api.libs.json.{Format, Json}
sealed trait IdentityCommand
case class RegisterClient(
company: String,
firstName: String,
lastName: String,
email: String,
username: String,
password: String
) extends PersistentEntity.ReplyType[GeneratedIdDone] with IdentityCommand
object RegisterClient {
implicit val format: Format[RegisterClient] = Json.format
}
case class CreateUser(
firstName: String,
lastName: String,
email: String,
username: String,
password: String
) extends PersistentEntity.ReplyType[GeneratedIdDone] with IdentityCommand
object CreateUser {
implicit val format: Format[CreateUser] = Json.format
}
case class GetIdentityState() extends PersistentEntity.ReplyType[IdentityStateDone] with IdentityCommand
示例8: MessageEntity
//设置package包名称以及导入依赖的类
package sample.helloworldconsumer.impl
import java.time.LocalDateTime
import akka.Done
import com.lightbend.lagom.scaladsl.persistence.PersistentEntity
class MessageEntity extends PersistentEntity {
override type Command = MessageCommand[_]
override type Event = MessageEvent
override type State = MessageState
override def initialState = MessageState("", LocalDateTime.now().toString)
override def behavior: Behavior = {
case MessageState(msg, _) => Actions().onCommand[SaveNewMessage, Done] {
case (SaveNewMessage(msg), ctx, state) =>
println(s"observe new message from kafka and save ${msg}")
ctx.thenPersist(MessageSaved(msg)) { msgSaved: MessageSaved =>
ctx.reply(Done)
}
}.onEvent {
case (MessageSaved(message), state) =>
println(s"MessageSaved event fire ...")
MessageState(message, LocalDateTime.now().toString)
}
}
}
示例9: Post3
//设置package包名称以及导入依赖的类
package docs.home.scaladsl.persistence
import com.lightbend.lagom.scaladsl.persistence.PersistentEntity
final class Post3 extends PersistentEntity {
override type Command = BlogCommand
override type Event = BlogEvent
override type State = BlogState
override def initialState: BlogState = BlogState.empty
//#same-behavior
override def behavior: Behavior =
Actions()
.onCommand[AddPost, AddPostDone] {
case (AddPost(content), ctx, state) if state.isEmpty =>
ctx.thenPersist(PostAdded(entityId, content)) { evt =>
ctx.reply(AddPostDone(entityId))
}
}
.onEvent {
case (PostAdded(postId, content), state) =>
BlogState(Some(content), published = false)
}
.onReadOnlyCommand[GetPost.type, PostContent] {
case (GetPost, ctx, state) if !state.isEmpty =>
ctx.reply(state.content.get)
}
//#same-behavior
}
示例10: JdbcPersistentEntityRegistry
//设置package包名称以及导入依赖的类
package com.lightbend.lagom.internal.scaladsl.persistence.jdbc
import akka.actor.ActorSystem
import akka.persistence.jdbc.query.scaladsl.JdbcReadJournal
import akka.persistence.query.scaladsl.EventsByTagQuery
import akka.persistence.query.{ NoOffset, Offset, PersistenceQuery, Sequence }
import com.lightbend.lagom.internal.persistence.jdbc.SlickProvider
import com.lightbend.lagom.internal.scaladsl.persistence.AbstractPersistentEntityRegistry
import com.lightbend.lagom.scaladsl.persistence.PersistentEntity
private[lagom] final class JdbcPersistentEntityRegistry(system: ActorSystem, slickProvider: SlickProvider)
extends AbstractPersistentEntityRegistry(system) {
private lazy val ensureTablesCreated = slickProvider.ensureTablesCreated()
override def register(entityFactory: => PersistentEntity): Unit = {
ensureTablesCreated
super.register(entityFactory)
}
override protected val journalId: String = JdbcReadJournal.Identifier
private val jdbcReadJournal = PersistenceQuery(system).readJournalFor[JdbcReadJournal](journalId)
override protected val eventsByTagQuery: Option[EventsByTagQuery] = Some(jdbcReadJournal)
override protected def mapStartingOffset(storedOffset: Offset): Offset = storedOffset match {
case NoOffset => NoOffset
case Sequence(value) => Sequence(value + 1)
case other =>
throw new IllegalArgumentException(s"JDBC does not support ${other.getClass.getSimpleName} offsets")
}
}
示例11: JournalEntity
//设置package包名称以及导入依赖的类
package io.namelos.journal.impl
import akka.Done
import com.lightbend.lagom.scaladsl.persistence.PersistentEntity.ReplyType
import com.lightbend.lagom.scaladsl.persistence.{AggregateEvent, AggregateEventTag, PersistentEntity}
import com.lightbend.lagom.scaladsl.playjson.{JsonSerializer, JsonSerializerRegistry}
import play.api.libs.json.Json
import scala.collection.immutable.Seq
class JournalEntity extends PersistentEntity {
type Command = JournalCommand[_]
type Event = JournalEvent
type State = JournalState
def initialState = JournalState(0)
def behavior = {
case JournalState(_) => Actions()
.onCommand[Add, Done] { case (Add(x), ctx, _) => ctx.thenPersist(Added(x)) { _ => ctx.reply(Done) } }
.onReadOnlyCommand[GetState.type, Int] { case (GetState, ctx, JournalState(n)) => ctx.reply(n) }
.onEvent { case (Added(x), JournalState(n)) => JournalState(n + x) }
}
}
case class JournalState(n: Int)
object JournalState { implicit val format = Json.format[JournalState] }
sealed trait JournalEvent extends AggregateEvent[JournalEvent] { def aggregateTag = JournalEvent.Tag }
object JournalEvent { val Tag = AggregateEventTag[JournalEvent] }
case class Added(n: Int) extends JournalEvent
object Added { implicit val format = Json.format[Added] }
sealed trait JournalCommand[R] extends ReplyType[R]
case class Add(n: Int) extends JournalCommand[Done]
object Add { implicit val format = Json.format[Add] }
case object GetState extends JournalCommand[Int] { implicit val format = JsonSerializer.emptySingletonFormat(GetState) }
object JournalSerializerRegistry extends JsonSerializerRegistry {
override def serializers: Seq[JsonSerializer[_]] = Seq(
JsonSerializer[Add],
JsonSerializer[Added],
JsonSerializer[JournalState],
JsonSerializer[GetState.type]
)
}