use of com.rabbitmq.stream.Consumer in project rabbitmq-stream-java-client by rabbitmq.
the class SuperStreamUsage method consumerSimple.
void consumerSimple() {
Environment environment = Environment.builder().build();
// tag::consumer-simple[]
Consumer consumer = environment.consumerBuilder().superStream(// <1>
"invoices").messageHandler((context, message) -> {
// message processing
}).build();
// ...
// <2>
consumer.close();
// end::consumer-simple[]
}
use of com.rabbitmq.stream.Consumer in project rabbitmq-stream-java-client by rabbitmq.
the class AlarmsTest method publishingShouldBeBlockedWhenDiskAlarmIsOn.
@Test
void publishingShouldBeBlockedWhenDiskAlarmIsOn() throws Exception {
int messageCount = 50_000;
AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(messageCount));
ConfirmationHandler confirmationHandler = confirmationStatus -> latch.get().countDown();
Producer producer = env.producerBuilder().stream(stream).build();
range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().build(), confirmationHandler));
assertThat(latchAssert(latch.get())).completes();
try (AutoCloseable alarm = diskAlarm()) {
latch.set(new CountDownLatch(messageCount));
new Thread(() -> range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().build(), confirmationHandler))).start();
assertThat(latchAssert(latch.get())).doesNotComplete(Duration.ofSeconds(5));
}
assertThat(latchAssert(latch.get())).completes();
producer.close();
latch.set(new CountDownLatch(messageCount * 2));
Consumer consumer = env.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> latch.get().countDown()).build();
assertThat(latchAssert(latch.get())).completes();
consumer.close();
}
use of com.rabbitmq.stream.Consumer in project rabbitmq-stream-java-client by rabbitmq.
the class AlarmsTest method diskAlarmShouldNotPreventConsumption.
@Test
void diskAlarmShouldNotPreventConsumption() throws Exception {
int messageCount = 50_000;
AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(messageCount));
ConfirmationHandler confirmationHandler = confirmationStatus -> latch.get().countDown();
Producer producer = env.producerBuilder().stream(stream).build();
range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().build(), confirmationHandler));
assertThat(latchAssert(latch)).completes();
producer.close();
try (AutoCloseable alarm = diskAlarm()) {
latch.set(new CountDownLatch(messageCount));
Consumer consumer = env.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> latch.get().countDown()).build();
assertThat(latchAssert(latch)).completes();
consumer.close();
}
}
use of com.rabbitmq.stream.Consumer in project rabbitmq-stream-java-client by rabbitmq.
the class AlarmsTest method publishingWhenMemoryAlarmIsOnShouldWork.
@Test
void publishingWhenMemoryAlarmIsOnShouldWork() throws Exception {
int messageCount = 50_000;
AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(messageCount));
ConfirmationHandler confirmationHandler = confirmationStatus -> latch.get().countDown();
Producer producer = env.producerBuilder().stream(stream).build();
range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().build(), confirmationHandler));
assertThat(latchAssert(latch.get())).completes();
try (AutoCloseable alarm = memoryAlarm()) {
latch.set(new CountDownLatch(messageCount));
new Thread(() -> range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().build(), confirmationHandler))).start();
assertThat(latchAssert(latch.get())).completes();
}
assertThat(latchAssert(latch.get())).completes();
producer.close();
latch.set(new CountDownLatch(messageCount * 2));
Consumer consumer = env.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> latch.get().countDown()).build();
assertThat(latchAssert(latch.get())).completes();
consumer.close();
}
use of com.rabbitmq.stream.Consumer in project rabbitmq-stream-java-client by rabbitmq.
the class StreamConsumerTest method autoTrackingShouldStoreAfterClosing.
@Test
void autoTrackingShouldStoreAfterClosing() throws Exception {
int storeEvery = 10_000;
int messageCount = storeEvery * 5 - 100;
CountDownLatch consumeLatch = new CountDownLatch(messageCount);
String reference = "ref-1";
AtomicLong lastReceivedOffset = new AtomicLong(0);
Consumer consumer = environment.consumerBuilder().name(reference).stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> {
lastReceivedOffset.set(context.offset());
consumeLatch.countDown();
}).autoTrackingStrategy().flushInterval(// long flush interval
Duration.ofHours(1)).messageCountBeforeStorage(storeEvery).builder().build();
Producer producer = environment.producerBuilder().stream(stream).build();
IntStream.range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().addData("".getBytes()).build(), confirmationStatus -> {
}));
latchAssert(consumeLatch).completes();
consumer.close();
Client client = cf.get();
waitAtMost(5, () -> client.queryOffset(reference, stream).getOffset() == lastReceivedOffset.get(), () -> format("Expecting stored offset %d to be equal to last received offset %d", client.queryOffset(reference, stream).getOffset(), lastReceivedOffset.get()));
}
Aggregations