Search in sources :

Example 1 with Registration

use of com.rabbitmq.stream.impl.OffsetTrackingCoordinator.Registration in project rabbitmq-stream-java-client by rabbitmq.

the class StreamEnvironment method registerTrackingConsumer.

TrackingConsumerRegistration registerTrackingConsumer(StreamConsumer streamConsumer, TrackingConfiguration configuration) {
    Runnable closingCallable = this.producersCoordinator.registerTrackingConsumer(streamConsumer);
    Registration offsetTrackingRegistration;
    if (this.offsetTrackingCoordinator.needTrackingRegistration(configuration)) {
        offsetTrackingRegistration = this.offsetTrackingCoordinator.registerTrackingConsumer(streamConsumer, configuration);
    } else {
        offsetTrackingRegistration = null;
    }
    Runnable closingSequence;
    if (offsetTrackingRegistration == null) {
        closingSequence = closingCallable;
    } else {
        closingSequence = () -> {
            try {
                LOGGER.debug("Executing offset tracking registration closing sequence");
                offsetTrackingRegistration.closingCallback().run();
                LOGGER.debug("Offset tracking registration closing sequence executed");
            } catch (Exception e) {
                LOGGER.warn("Error while executing offset tracking registration closing sequence: {}", e.getMessage());
            }
            closingCallable.run();
        };
    }
    return new TrackingConsumerRegistration(closingSequence, offsetTrackingRegistration == null ? null : offsetTrackingRegistration.postMessageProcessingCallback(), offsetTrackingRegistration == null ? Utils.NO_OP_LONG_CONSUMER : offsetTrackingRegistration.trackingCallback());
}
Also used : Registration(com.rabbitmq.stream.impl.OffsetTrackingCoordinator.Registration) TimeoutException(java.util.concurrent.TimeoutException) StreamException(com.rabbitmq.stream.StreamException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SSLException(javax.net.ssl.SSLException)

Example 2 with Registration

use of com.rabbitmq.stream.impl.OffsetTrackingCoordinator.Registration in project rabbitmq-stream-java-client by rabbitmq.

the class OffsetTrackingCoordinatorTest method autoShouldNotStoreLastProcessedOffsetOnClosingIfBehindStoredOffset.

@Test
void autoShouldNotStoreLastProcessedOffsetOnClosingIfBehindStoredOffset() {
    Duration checkInterval = Duration.ofMillis(100);
    OffsetTrackingCoordinator coordinator = new OffsetTrackingCoordinator(env, checkInterval);
    when(consumer.lastStoredOffset()).thenReturn(15L);
    Registration registration = coordinator.registerTrackingConsumer(consumer, new TrackingConfiguration(true, true, 1, Duration.ofHours(1), Duration.ZERO));
    long lastProcessedOffset = 10;
    registration.postMessageProcessingCallback().accept(context(lastProcessedOffset, () -> {
    }));
    registration.closingCallback().run();
    verify(consumer, never()).store(anyLong());
}
Also used : Registration(com.rabbitmq.stream.impl.OffsetTrackingCoordinator.Registration) Duration(java.time.Duration) TrackingConfiguration(com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration) Test(org.junit.jupiter.api.Test)

Example 3 with Registration

use of com.rabbitmq.stream.impl.OffsetTrackingCoordinator.Registration in project rabbitmq-stream-java-client by rabbitmq.

the class OffsetTrackingCoordinatorTest method autoShouldStoreLastProcessedOffsetOnClosing.

@Test
void autoShouldStoreLastProcessedOffsetOnClosing() {
    Duration checkInterval = Duration.ofMillis(100);
    OffsetTrackingCoordinator coordinator = new OffsetTrackingCoordinator(env, checkInterval);
    when(consumer.lastStoredOffset()).thenReturn(5L);
    Registration registration = coordinator.registerTrackingConsumer(consumer, new TrackingConfiguration(true, true, 1, Duration.ofHours(1), Duration.ZERO));
    long lastProcessedOffset = 10;
    registration.postMessageProcessingCallback().accept(context(lastProcessedOffset, () -> {
    }));
    registration.closingCallback().run();
    verify(consumer, times(1)).store(lastProcessedOffset);
}
Also used : Registration(com.rabbitmq.stream.impl.OffsetTrackingCoordinator.Registration) Duration(java.time.Duration) TrackingConfiguration(com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration) Test(org.junit.jupiter.api.Test)

Aggregations

Registration (com.rabbitmq.stream.impl.OffsetTrackingCoordinator.Registration)3 TrackingConfiguration (com.rabbitmq.stream.impl.StreamConsumerBuilder.TrackingConfiguration)2 Duration (java.time.Duration)2 Test (org.junit.jupiter.api.Test)2 StreamException (com.rabbitmq.stream.StreamException)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 SSLException (javax.net.ssl.SSLException)1