本文整理汇总了Scala中com.lightbend.lagom.scaladsl.persistence.AggregateEvent类的典型用法代码示例。如果您正苦于以下问题:Scala AggregateEvent类的具体用法?Scala AggregateEvent怎么用?Scala AggregateEvent使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AggregateEvent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ReadSideTestDriver
//设置package包名称以及导入依赖的类
package sample.helloworldconsumer.impl
import akka.Done
import akka.persistence.query.Offset
import akka.stream.Materializer
import akka.stream.scaladsl.{Sink, Source}
import com.lightbend.lagom.scaladsl.persistence.ReadSideProcessor.ReadSideHandler
import com.lightbend.lagom.scaladsl.persistence.{AggregateEvent, EventStreamElement, ReadSide, ReadSideProcessor}
import scala.concurrent.{ExecutionContext, Future}
class ReadSideTestDriver(implicit val materializer: Materializer, ec: ExecutionContext) extends ReadSide {
private var processors = Map.empty[Class[_], Seq[Future[(ReadSideHandler[_], Offset)]]]
override def register[Event <: AggregateEvent[Event]](processorFactory: => ReadSideProcessor[Event]): Unit = {
val processor = processorFactory
val eventTags = processor.aggregateTags
val handler = processor.buildHandler()
val future = for {
_ <- handler.globalPrepare()
offset <- handler.prepare(eventTags.head)
} yield {
handler -> offset
}
synchronized {
val handlers = processors.getOrElse(eventTags.head.eventType, Nil)
processors += (eventTags.head.eventType -> (handlers :+ future))
}
}
def feed[Event <: AggregateEvent[Event]](entityId: String, event: Event, offset: Offset): Future[Done] = {
processors.get(event.aggregateTag.eventType) match {
case None => sys.error(s"No processor registered for Event ${event.aggregateTag.eventType.getCanonicalName}")
case Some(handlerFutures) =>
for {
handlers <- Future.sequence(handlerFutures)
_ <- Future.sequence(handlers.map {
case (handler: ReadSideHandler[Event], _) =>
Source.single(new EventStreamElement(entityId, event, offset))
.via(handler.handle())
.runWith(Sink.ignore)
})
} yield {
Done
}
}
}
}
示例2: 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))
}
}
}
}
示例3: aggregateTag
//设置package包名称以及导入依赖的类
package com.grossbit.user.impl.persistance.event
import java.util.UUID
import com.grossbit.user.api.ResponseDone
import com.lightbend.lagom.scaladsl.persistence.{AggregateEvent, AggregateEventTag, AggregateEventTagger}
import play.api.libs.json.{Format, Json}
sealed trait UserEvent extends AggregateEvent[UserEvent]{
override def aggregateTag: AggregateEventTagger[UserEvent] = UserEvent.UserEventTag
}
object UserEvent{
val NumShards=4
val UserEventTag = AggregateEventTag.sharded[UserEvent](NumShards)
}
case class UserCreated(uuid:String,name: String, email: String, password: String) extends UserEvent
case class UserUpdated(uuid:String,name: String, email: String, password: String) extends UserEvent
case class UserDeleted(name: String) extends UserEvent
object UserCreated {
implicit val format: Format[UserCreated] = Json.format
}
object UserUpdated {
implicit val format: Format[UserUpdated] = Json.format
}
object UserDeleted {
implicit val format: Format[UserDeleted] = Json.format
}
示例4: ReadSideTestDriver
//设置package包名称以及导入依赖的类
package com.example.auction.item.impl
import akka.Done
import akka.persistence.query.Offset
import akka.stream.Materializer
import akka.stream.scaladsl.{Sink, Source}
import com.lightbend.lagom.scaladsl.persistence.ReadSideProcessor.ReadSideHandler
import com.lightbend.lagom.scaladsl.persistence.{AggregateEvent, EventStreamElement, ReadSide, ReadSideProcessor}
import scala.concurrent.{ExecutionContext, Future}
class ReadSideTestDriver(implicit val materializer: Materializer, ec: ExecutionContext) extends ReadSide {
private var processors = Map.empty[Class[_], Seq[Future[(ReadSideHandler[_], Offset)]]]
override def register[Event <: AggregateEvent[Event]](processorFactory: => ReadSideProcessor[Event]): Unit = {
val processor = processorFactory
val eventTags = processor.aggregateTags
val handler = processor.buildHandler()
val future = for {
_ <- handler.globalPrepare()
offset <- handler.prepare(eventTags.head)
} yield {
handler -> offset
}
synchronized {
val handlers = processors.getOrElse(eventTags.head.eventType, Nil)
processors += (eventTags.head.eventType -> (handlers :+ future))
}
}
def feed[Event <: AggregateEvent[Event]](entityId: String, event: Event, offset: Offset): Future[Done] = {
processors.get(event.aggregateTag.eventType) match {
case None => sys.error(s"No processor registered for Event ${event.aggregateTag.eventType.getCanonicalName}")
case Some(handlerFutures) =>
for {
handlers <- Future.sequence(handlerFutures)
_ <- Future.sequence(handlers.map {
case (handler: ReadSideHandler[Event], _) =>
Source.single(new EventStreamElement(entityId, event, offset))
.via(handler.handle())
.runWith(Sink.ignore)
})
} yield {
Done
}
}
}
}
示例5: aggregateTag
//设置package包名称以及导入依赖的类
package org.wex.cmsfs.monitor.impl
import com.lightbend.lagom.scaladsl.persistence.{AggregateEvent, AggregateEventTag}
import org.wex.cmsfs.monitor.api.Monitor
import play.api.libs.json.{Format, Json}
sealed trait MonitorEvent extends AggregateEvent[MonitorEvent] {
override def aggregateTag = MonitorEvent.Tag
}
object MonitorEvent {
val NumShards = 1
val Tag = AggregateEventTag.sharded[MonitorEvent](NumShards)
}
case class MonitorCreated(monitor: Monitor) extends MonitorEvent
object MonitorCreated {
implicit val format: Format[MonitorCreated] = Json.format
}
示例6: ExOneService
//设置package包名称以及导入依赖的类
package com.example.exone.api
import com.lightbend.lagom.scaladsl.api.Service
import com.lightbend.lagom.scaladsl.api.broker.Topic
import com.lightbend.lagom.scaladsl.persistence.{AggregateEvent, AggregateEventTag, AggregateEventTagger}
import play.api.libs.json.{Format, Json}
object ExOneService {
val TOPIC_NAME = "exoneEvents"
}
trait ExOneService extends Service {
def exOneEvents(): Topic[ExOneEvent]
override final def descriptor = {
import Service._
named("exonedocs").withTopics(
topic(ExOneService.TOPIC_NAME, exOneEvents)
).withAutoAcl(true)
}
}
case class ExOneEvent(id:String, value:String) extends AggregateEvent[ExOneEvent]{
override def aggregateTag: AggregateEventTagger[ExOneEvent] = ExOneEvent.Tag
}
object ExOneEvent{
val NumShards = 4
val Tag = AggregateEventTag.sharded[ExOneEvent](4)
implicit val format: Format[ExOneEvent] = Json.format[ExOneEvent]
}
示例7: 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]
)
}
示例8: IdentityEvent
//设置package包名称以及导入依赖的类
package io.digitalcat.publictransportation.services.identity.impl
import java.util.UUID
import com.lightbend.lagom.scaladsl.persistence.{AggregateEvent, AggregateEventShards, AggregateEventTag}
import play.api.libs.json.{Format, Json}
object IdentityEvent {
val NumShards = 5
val Tag = AggregateEventTag.sharded[IdentityEvent]("IdentityEvent", NumShards)
}
sealed trait IdentityEvent extends AggregateEvent[IdentityEvent] {
override def aggregateTag(): AggregateEventShards[IdentityEvent] = IdentityEvent.Tag
}
case class ClientCreated(company: String) extends IdentityEvent
object ClientCreated {
implicit val format: Format[ClientCreated] = Json.format
}
case class UserCreated(userId: UUID, firstName: String, lastName: String, email: String, username: String, hashedPassword: String) extends IdentityEvent
object UserCreated {
implicit val format: Format[UserCreated] = Json.format
}
示例9: ShardedBlogEventTag
//设置package包名称以及导入依赖的类
package docs.home.scaladsl.persistence
import com.lightbend.lagom.scaladsl.persistence.AggregateEventShards
import com.lightbend.lagom.scaladsl.persistence.AggregateEvent
import com.lightbend.lagom.scaladsl.persistence.AggregateEventTag
class ShardedBlogEventTag {
//#sharded-tags
object BlogEvent {
val NumShards = 20
val Tag = AggregateEventTag.sharded[BlogEvent](NumShards)
}
sealed trait BlogEvent extends AggregateEvent[BlogEvent] {
override def aggregateTag: AggregateEventShards[BlogEvent] = BlogEvent.Tag
}
//#sharded-tags
}
示例10: BlogEventTag
//设置package包名称以及导入依赖的类
package docs.home.scaladsl.persistence
import com.lightbend.lagom.scaladsl.persistence.AggregateEventShards
import com.lightbend.lagom.scaladsl.persistence.AggregateEvent
import com.lightbend.lagom.scaladsl.persistence.AggregateEventTag
class BlogEventTag {
//#aggregate-tag
object BlogEvent {
val BlogEventTag = AggregateEventTag[BlogEvent]
}
sealed trait BlogEvent extends AggregateEvent[BlogEvent] {
override def aggregateTag: AggregateEventTag[BlogEvent] =
BlogEvent.BlogEventTag
}
//#aggregate-tag
}
示例11: BlogEvent
//设置package包名称以及导入依赖的类
package docs.home.scaladsl.persistence
//#full-example
import com.lightbend.lagom.scaladsl.persistence.AggregateEvent
import com.lightbend.lagom.scaladsl.persistence.AggregateEventShards
import com.lightbend.lagom.scaladsl.persistence.AggregateEventTag
import com.lightbend.lagom.scaladsl.playjson.JsonSerializer
object BlogEvent {
val NumShards = 20
// second param is optional, defaults to the class name
val Tag = AggregateEventTag.sharded[BlogEvent](NumShards)
import play.api.libs.json._
private implicit val postContentFormat = Json.format[PostContent]
val serializers = Vector(
JsonSerializer(Json.format[PostAdded]),
JsonSerializer(Json.format[BodyChanged]),
JsonSerializer(Json.format[PostPublished]))
}
sealed trait BlogEvent extends AggregateEvent[BlogEvent] {
override def aggregateTag: AggregateEventShards[BlogEvent] = BlogEvent.Tag
}
final case class PostAdded(postId: String, content: PostContent) extends BlogEvent
final case class BodyChanged(postId: String, body: String) extends BlogEvent
final case class PostPublished(postId: String) extends BlogEvent
//#full-example
示例12: CassandraReadSideImpl
//设置package包名称以及导入依赖的类
package com.lightbend.lagom.internal.scaladsl.persistence.cassandra
import akka.Done
import akka.actor.ActorSystem
import com.datastax.driver.core.BoundStatement
import com.lightbend.lagom.internal.persistence.cassandra.CassandraOffsetStore
import com.lightbend.lagom.scaladsl.persistence.ReadSideProcessor.ReadSideHandler
import com.lightbend.lagom.scaladsl.persistence.cassandra.CassandraReadSide.ReadSideHandlerBuilder
import com.lightbend.lagom.scaladsl.persistence.cassandra.{ CassandraReadSide, CassandraSession }
import com.lightbend.lagom.scaladsl.persistence.{ AggregateEvent, AggregateEventTag, EventStreamElement }
import scala.collection.immutable
import scala.concurrent.Future
import scala.reflect.ClassTag
private[lagom] final class CassandraReadSideImpl(
system: ActorSystem, session: CassandraSession, offsetStore: CassandraOffsetStore
) extends CassandraReadSide {
private val dispatcher = system.settings.config.getString("lagom.persistence.read-side.use-dispatcher")
implicit val ec = system.dispatchers.lookup(dispatcher)
override def builder[Event <: AggregateEvent[Event]](eventProcessorId: String): ReadSideHandlerBuilder[Event] = {
new ReadSideHandlerBuilder[Event] {
import CassandraAutoReadSideHandler.Handler
private var prepareCallback: AggregateEventTag[Event] => Future[Done] = tag => Future.successful(Done)
private var globalPrepareCallback: () => Future[Done] = () => Future.successful(Done)
private var handlers = Map.empty[Class[_ <: Event], Handler[Event]]
override def setGlobalPrepare(callback: () => Future[Done]): ReadSideHandlerBuilder[Event] = {
globalPrepareCallback = callback
this
}
override def setPrepare(callback: (AggregateEventTag[Event]) => Future[Done]): ReadSideHandlerBuilder[Event] = {
prepareCallback = callback
this
}
override def setEventHandler[E <: Event: ClassTag](handler: EventStreamElement[E] => Future[immutable.Seq[BoundStatement]]): ReadSideHandlerBuilder[Event] = {
val eventClass = implicitly[ClassTag[E]].runtimeClass.asInstanceOf[Class[Event]]
handlers += (eventClass -> handler.asInstanceOf[Handler[Event]])
this
}
override def build(): ReadSideHandler[Event] = {
new CassandraAutoReadSideHandler[Event](session, offsetStore, handlers, globalPrepareCallback, prepareCallback, eventProcessorId, dispatcher)
}
}
}
}
示例13: 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]
)
}
示例14: aggregateTag
//设置package包名称以及导入依赖的类
package com.livelygig.product.message.impl
import com.lightbend.lagom.scaladsl.persistence.{AggregateEvent, AggregateEventTag, AggregateEventTagger}
import com.lightbend.lagom.scaladsl.playjson.Jsonable
import com.livelygig.product.message.api.Message
import play.api.libs.json.Json
sealed trait MessageEvent extends AggregateEvent[MessageEvent] with Jsonable {
override def aggregateTag: AggregateEventTagger[MessageEvent] = MessageEvent.Tag
}
object MessageEvent {
val NumShards = 4
val Tag = AggregateEventTag.sharded[MessageEvent](NumShards)
}
case class MessagePosted(message: Message) extends MessageEvent
object MessagePosted {
implicit val format = Json.format[MessagePosted]
}
示例15: aggregateTag
//设置package包名称以及导入依赖的类
package com.livelygig.product.user.impl
import com.lightbend.lagom.scaladsl.persistence.{AggregateEvent, AggregateEventTag, AggregateEventTagger}
import com.lightbend.lagom.scaladsl.playjson.Jsonable
import com.livelygig.product.user.api.User
import play.api.libs.json.{Format, Json}
sealed trait UserEvent extends AggregateEvent[UserEvent] with Jsonable {
override def aggregateTag: AggregateEventTagger[UserEvent] = UserEvent.Tag
}
object UserEvent {
val NumShards = 4
val Tag = AggregateEventTag.sharded[UserEvent](NumShards)
}
case class UserCreated(user: User) extends UserEvent
object UserCreated {
implicit val format: Format[UserCreated] = Json.format
}