protected override void PreStart() { base.PreStart(); //Inside the event flow self isn't the actor to receive var connectionHandler = Self; _readJournal.EventsByTag("book-reserved-updated").RunForeach( pEnvelope => { switch (pEnvelope) { case EventEnvelope e when e.Event is Book.BookReservedUpdated: long id = ((Sequence)e.Offset).Value; var reservedEvent = (Book.BookReservedUpdated)e.Event; string isbn = e.PersistenceId.Split("-").Last(); connectionHandler.Tell( new BookReservedEventEnvelope( id.ToString(), new BookReservedEvent(isbn, reservedEvent.Reserved))); break; } }, ActorMaterializer.Create(Context.System)); ValidateConnection(); KeepAliveConnection(); }
public BookQueryService(SqlReadJournal readJournal, ActorSystem actorSystem) { this.readJournal = readJournal; this.actorSystem = actorSystem; var source = readJournal.EventsByTag("book"); var mat = ActorMaterializer.Create(actorSystem); source.RunForeach(envelope => Handle(envelope.Event), mat); }
public void Sql_live_query_EventsByTag_should_find_new_events() { Sql_query_EventsByTag_should_find_events_from_offset(); var d = SetupEmpty("d"); var blackSrc = _queries.EventsByTag("black", offset: 0); var probe = blackSrc.RunWith(this.SinkProbe <EventEnvelope>(), _materializer); probe.Request(2) .ExpectNext(new EventEnvelope(3, "b", 1, "a black car")) .ExpectNoMsg(TimeSpan.FromMilliseconds(100)); d.Tell("a black dog"); ExpectMsg("a black dog-done"); d.Tell("a black night"); ExpectMsg("a black night-done"); probe.ExpectNext(new EventEnvelope(7, "d", 1, "a black dog")) .ExpectNoMsg(TimeSpan.FromMilliseconds(100)); probe.Request(10) .ExpectNext(new EventEnvelope(8, "d", 2, "a black night")); }
protected override async void PreStart() { journal.EventsByTag("Account") .To(Sink.ActorRef <EventEnvelope>(Context.Self, null)) .Run(ActorMaterializer.Create(Context.System)); }
public BookViewBuilder(IResumableProjection resumableProjection) { _resumableProjection = resumableProjection; _storageContext = new StorageContext(); var self = Self; SqlReadJournal queries = PersistenceQuery.Get(Context.System) .ReadJournalFor <SqlReadJournal>(SqlReadJournal.Identifier); Receive <Option <long> >(option => { var materializer = ActorMaterializer.Create(Context.System); var eventsByTag = queries.EventsByTag("book", new Sequence(option.Value)); eventsByTag .SelectAsync(1, x => self.Ask(x)) .Recover(exception => { Console.WriteLine(exception); return(Option <object> .None); }) .Select(x => (Sequence)x) .SelectAsync(1, x => _resumableProjection.StoreLatestOffset("book-view", x.Value)) .RunWith(Sink.Ignore <bool>(), materializer); }); Receive <EventEnvelope>(envelope => envelope.Event.Match() .With <BookCreated>(bookCreated => { var updateDefinition = new UpdateDefinitionBuilder <BookReadModel>() .SetOnInsert(model => model.Title, bookCreated.Title) .SetOnInsert(model => model.Author, bookCreated.Author) .SetOnInsert(model => model.Tags, bookCreated.Tags) .SetOnInsert(model => model.Cost, bookCreated.Cost) .SetOnInsert(model => model.InventoryAmount, bookCreated.InventoryAmount) .SetOnInsert(model => model.SequenceNr, envelope.SequenceNr); var updateOptions = new UpdateOptions { IsUpsert = true }; _storageContext.Books .UpdateOneAsync(x => x.Id == bookCreated.Id, updateDefinition, updateOptions) .PipeTo(Sender, Self, () => envelope.Offset, exception => new Status.Failure(exception)); }) .With <TagAdded>(tagAdded => { var updateDefinition = new UpdateDefinitionBuilder <BookReadModel>() .Push(model => model.Tags, tagAdded.Tag) .Set(model => model.SequenceNr, envelope.SequenceNr); _storageContext.Books .FindOneAndUpdateAsync(ShouldBeApplied(tagAdded.Id, envelope.SequenceNr), updateDefinition) .PipeTo(Sender, Self, () => envelope.Offset, exception => new Status.Failure(exception)); }) .With <TagRemoved>(tagRemoved => { var updateDefinition = new UpdateDefinitionBuilder <BookReadModel>() .Pull(model => model.Tags, tagRemoved.Tag) .Set(model => model.SequenceNr, envelope.SequenceNr); _storageContext.Books .FindOneAndUpdateAsync(ShouldBeApplied(tagRemoved.Id, envelope.SequenceNr), updateDefinition) .PipeTo(Sender, Self, () => envelope.Offset, exception => new Status.Failure(exception)); }) ); }