/** * Starts recording event published by the event bus. */ public void startRecording() { eventBus.subscribe(eventMessages -> eventMessages.forEach(this::handle)); }
@Before public void setUp() { EventStore eventStore = spy(EmbeddedEventStore.builder() .storageEngine(new InMemoryEventStorageEngine()) .build()); Configurer configurer = DefaultConfigurer.defaultConfiguration(); configurer.eventProcessing() .usingSubscribingEventProcessors() .registerSaga(MySaga.class); configuration = configurer.configureEventStore(c -> eventStore) .configureAggregate(MyAggregate.class) .registerComponent(DeadlineManager.class, this::buildDeadlineManager) .start(); published = new CopyOnWriteArrayList<>(); configuration.eventBus().subscribe(msgs -> msgs.forEach(msg -> published.add(msg.getPayload()))); }
@Test public void testCommandsAreRoutedToCorrectEntity() throws Exception { AggregateModel<Book> bookAggregateModel = AnnotatedAggregateMetaModelFactory.inspectAggregate(Book.class); EventBus mockEventBus = SimpleEventBus.builder().build(); mockEventBus.subscribe(m -> m.forEach(i -> System.out.println(i.getPayloadType().getName()))); AnnotatedAggregate<Book> bookAggregate = AnnotatedAggregate.initialize((Book) null, bookAggregateModel, mockEventBus); bookAggregate.handle(command(new CreateBookCommand("book1"))); bookAggregate.handle(command(new CreatePageCommand("book1"))); bookAggregate.handle(command(new CreateParagraphCommand("book1", 0))); bookAggregate.handle(command(new CreateParagraphCommand("book1", 0))); bookAggregate.handle(command(new UpdateParagraphCommand("book1", 0, 0, "Hello world"))); bookAggregate.handle(command(new UpdateParagraphCommand("book1", 0, 1, "Hello world2"))); assertEquals("Hello world", bookAggregate.getAggregateRoot().getPages().get(0).getParagraphs().get(0).getText()); assertEquals("Hello world2", bookAggregate.getAggregateRoot().getPages().get(0).getParagraphs().get(1).getText()); }
public void initKafkaPublisher(final EventBus eventBus, final Sender sender, final Serializer serializer) { final KafkaMessageConverter converter = new KafkaMessageConverter(serializer); eventBus.subscribe(events -> { events.stream().map(event -> converter.createKafkaMessage(event)).forEach(kafkaMessage -> sender.send(kafkaMessage)); }); }
public void sendAndWaitForEvent(Object command, final EventCallback callback, final long timeoutInMillis) { final CorrelationToken correlationToken = new CorrelationToken(); final TimedEventCallback timedEventCallback = new TimedEventCallback(callback); callbacks.put(correlationToken, timedEventCallback); timedEventCallback.scheduleTimer(new Runnable() { @Override public void run() { callbacks.remove(correlationToken); } }, timeoutInMillis); if (started.compareAndSet(false, true)) { eventBus.subscribe(new AnnotationEventListenerAdapter(this)); } final CommandMessage commandMessage = asCommandMessage(command) .andMetaData(Collections.singletonMap(CorrelationToken.KEY, correlationToken)); commandBus.dispatch(commandMessage); }