@Override public int append( final String aggregateId, final Iterable<? extends TEvent> events, final ExpectedVersion expectedVersion, final Map<String, String> metadata) { List<EventData<TEvent>> eventDatas = StreamSupport .stream(events.spliterator(), false) .map(e -> new EventData<TEvent>( getEventType(e), UUID.randomUUID(), metadata, e)) .collect(Collectors.toList()); return eventRepository.append(aggregateId, eventDatas, expectedVersion); }
/** * Reads all events for a given stream id from the beginning. * * @param streamId The id of the stream to read events for. * @return A result record describing the outcome of the read and the events themselves, or null * if no stream was found. */ default EventReadResult<T> read(final String streamId) { return read(streamId, 0); }
/** * Reads from the stream of all events kept in the event repository. * * @param version The version to read events from. Versions are monotonically increasing * starting with 0, specifying a version of 0 is equivalent to reading the events * from the beginning of the stream. * @return A result record describing the outcome of the read and the events themselves, or null * if no events exist in this repository. */ default EventReadResult<T> readAll(final int version) { return readAll(version, Integer.MAX_VALUE); }
private void runOneSession() throws InterruptedException { subscriberCount++; Integer subscriptionPosition = positionSource.getSubscriptionPosition(); BlockingQueue<Update<T>> currentUpdates = new ArrayBlockingQueue<>(config.getBatchSize()); SessionSubscriber<T> subscriber = new SessionSubscriber<>(currentUpdates, "" + subscriberCount); LambdaSubscriber<EventSubscriptionUpdate<T>> boundedSubscriber = Subscribers.bounded( config.getBatchSize(), subscriber::onNext, subscriber::onError, subscriber::onComplete); try { logger.info("Subscribing to event store ..."); repository.getPublisher(subscriptionPosition).subscribe(boundedSubscriber); while (processUpdates(currentUpdates)) { logger.debug("Processed updates, will do more"); retryCount.set(0); } // Clean exit, can only mean we're at the end of the subscription, or been explicitly // cancelled logger.info("Subscription worker finishing cleanly"); } finally { logger.info("Subscription worker exiting"); subscriber.kill(); boundedSubscriber.dispose(); } }
@Test public void readFirstReturnsNullForMissingStream() { EventRecord<TestEventType> actual = repository.readFirst(streamId); assertThat(actual, nullValue()); }
@Test public void readLastReturnsNullForMissingStream() { EventRecord<TestEventType> actual = repository.readLast(streamId); assertThat(actual, nullValue()); }
private void runOneSession() throws InterruptedException { subscriberCount++; Integer subscriptionPosition = positionSource.getSubscriptionPosition(); BlockingQueue<Update<T>> currentUpdates = new ArrayBlockingQueue<>(config.getBatchSize()); SessionSubscriber<T> subscriber = new SessionSubscriber<>(currentUpdates, "" + subscriberCount); LambdaSubscriber<EventSubscriptionUpdate<T>> boundedSubscriber = Subscribers.bounded( config.getBatchSize(), subscriber::onNext, subscriber::onError, subscriber::onComplete); try { logger.info("Subscribing to event store ..."); repository.getPublisher(subscriptionPosition).subscribe(boundedSubscriber); while (processUpdates(currentUpdates)) { logger.debug("Processed updates, will do more"); retryCount.set(0); } // Clean exit, can only mean we're at the end of the subscription, or been explicitly // cancelled logger.info("Subscription worker finishing cleanly"); } finally { logger.info("Subscription worker exiting"); subscriber.kill(); boundedSubscriber.dispose(); } }
@Test public void readFirstReturnsNullForMissingStream() { EventRecord<TestEventType> actual = repository.readFirst(streamId); assertThat(actual, nullValue()); }
@Test public void readLastReturnsNullForMissingStream() { EventRecord<TestEventType> actual = repository.readLast(streamId); assertThat(actual, nullValue()); }
@Override public int append( final String aggregateId, final Iterable<? extends TEvent> events, final ExpectedVersion expectedVersion, final Map<String, String> metadata) { List<EventData<TEvent>> eventDatas = StreamSupport .stream(events.spliterator(), false) .map(e -> new EventData<TEvent>( getEventType(e), UUID.randomUUID(), metadata, e)) .collect(Collectors.toList()); return eventRepository.append(aggregateId, eventDatas, expectedVersion); }
/** * Reads all events for a given stream id from the beginning. * * @param streamId The id of the stream to read events for. * @return A result record describing the outcome of the read and the events themselves, or null * if no stream was found. */ default EventReadResult<T> read(final String streamId) { return read(streamId, 0); }
@Test @Ignore // Manual only public void testRetriesOnHandlerErrorWithBackoff() { EventRepository<String> repository = mock(EventRepository.class); EventSubscriptionPositionSource positionSource = mock(EventSubscriptionPositionSource.class); when(repository.getPublisher(any())).then(position -> { WorkQueueProcessor<EventRecord<String>> processor = WorkQueueProcessor.create(); Flux<EventRecord<String>> eventSource = Flux .fromStream(IntStream.range(0, 1000000).mapToObj(this::wrapIntAsEvent)) .doOnNext(e -> { lastProducedValue = e.getEvent(); }); eventSource.subscribe(processor); return processor; }); when(positionSource.getSubscriptionPosition()).thenReturn(null); ErroringSubscriptionHandler<String> subscriptionHandler = new ErroringSubscriptionHandler<>(0); EventSubscriptionManager subscriptionManager = new EventSubscriptionManager<>( repository, positionSource, subscriptionHandler, new SubscriptionWorkerConfig().withBatchSize(64)); SubscriptionToken token = subscriptionManager.start(); sleep(100000); token.stop(); }
@Test public void readFirstEvent() { append(new TestEventType("payload-1")); append(new TestEventType("payload-2")); EventRecord<TestEventType> actual = repository.readFirst(streamId); assertThat(actual.getEvent().getValue(), equalTo("payload-1")); }
/** * Reads from the stream of all events kept in the event repository. * * @param version The version to read events from. Versions are monotonically increasing * starting with 0, specifying a version of 0 is equivalent to reading the events * from the beginning of the stream. * @return A result record describing the outcome of the read and the events themselves, or null * if no events exist in this repository. */ default EventReadResult<T> readAll(final int version) { return readAll(version, Integer.MAX_VALUE); }
@Test public void readLastEvent() { append(new TestEventType("payload-1")); append(new TestEventType("payload-2")); EventRecord<TestEventType> actual = repository.readLast(streamId); assertThat(actual.getEvent().getValue(), equalTo("payload-2")); }
private void append(final TestEventType type) { EventData<TestEventType> events = eventData(type); repository.append(streamId, Collections.singletonList(events), ExpectedVersion.any()); }
/** * Reads all events for a given stream id from a given version and onwards, with no specified * upper bound on the number of events returned. * * @param streamId The id of the stream to read events for. * @param version The version to read events from. Versions are monotonically increasing * starting with 0, specifying a version of 0 is equivalent to reading the * events from the beginning of the stream. * @return A result record describing the outcome of the read and the events themselves, or null * if no stream was found. */ default EventReadResult<T> read(final String streamId, final int version) { return read(streamId, version, Integer.MAX_VALUE); }
mock(EventSubscriptionPositionSource.class); when(repository.getPublisher(any())).then(position -> { WorkQueueProcessor<EventSubscriptionUpdate<String>> processor = WorkQueueProcessor.create();
@Test public void readFirstEvent() { append(new TestEventType("payload-1")); append(new TestEventType("payload-2")); EventRecord<TestEventType> actual = repository.readFirst(streamId); assertThat(actual.getEvent().getValue(), equalTo("payload-1")); }
/** * Reads from the stream of all events kept in the event repository. * * @return A result record describing the outcome of the read and the events themselves, or null * if no events exist in this repository. */ default EventReadResult<T> readAll() { return readAll(0); }