use of com.rabbitmq.stream.ConfirmationHandler in project rabbitmq-stream-java-client by rabbitmq.
the class StreamProducerTest method newIncarnationOfProducerCanQueryItsLastPublishingId.
@ParameterizedTest
@ValueSource(ints = { 1, 7 })
void newIncarnationOfProducerCanQueryItsLastPublishingId(int subEntrySize) throws Exception {
Producer p = environment.producerBuilder().name("producer-1").stream(stream).subEntrySize(subEntrySize).build();
AtomicReference<Producer> producer = new AtomicReference<>(p);
AtomicLong publishingSequence = new AtomicLong(0);
AtomicLong lastConfirmed = new AtomicLong(-1);
ConfirmationHandler confirmationHandler = confirmationStatus -> {
if (confirmationStatus.isConfirmed()) {
lastConfirmed.set(confirmationStatus.getMessage().getPublishingId());
}
};
AtomicBoolean canPublish = new AtomicBoolean(true);
Runnable publish = () -> {
while (canPublish.get()) {
producer.get().send(producer.get().messageBuilder().publishingId(publishingSequence.getAndIncrement()).addData(String.valueOf(publishingSequence.get()).getBytes()).build(), confirmationHandler);
}
};
new Thread(publish).start();
Thread.sleep(1000L);
canPublish.set(false);
waitAtMost(10, () -> publishingSequence.get() == lastConfirmed.get() + 1);
assertThat(lastConfirmed.get()).isPositive();
producer.get().close();
p = environment.producerBuilder().name("producer-1").stream(stream).subEntrySize(subEntrySize).build();
producer.set(p);
long lastPublishingId = producer.get().getLastPublishingId();
assertThat(lastPublishingId).isEqualTo(lastConfirmed.get());
canPublish.set(true);
new Thread(publish).start();
Thread.sleep(1000L);
canPublish.set(false);
waitAtMost(10, () -> publishingSequence.get() == lastConfirmed.get() + 1);
assertThat(lastConfirmed.get()).isGreaterThan(lastPublishingId);
CountDownLatch consumeLatch = new CountDownLatch((int) (lastConfirmed.get() + 1));
AtomicInteger consumed = new AtomicInteger();
environment.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((offset, message) -> {
consumed.incrementAndGet();
consumeLatch.countDown();
}).build();
assertThat(consumeLatch.await(10, TimeUnit.SECONDS)).isTrue();
Thread.sleep(1000);
assertThat(consumed.get()).isEqualTo(lastConfirmed.get() + 1);
}
use of com.rabbitmq.stream.ConfirmationHandler in project rabbitmq-stream-java-client by rabbitmq.
the class StreamProducerTest method messagesShouldBeDeDuplicatedWhenUsingNameAndPublishingId.
@ParameterizedTest
@ValueSource(ints = { 1, 7 })
void messagesShouldBeDeDuplicatedWhenUsingNameAndPublishingId(int subEntrySize) throws Exception {
int lineCount = 50_000;
int firstWaveLineCount = lineCount / 5;
int backwardCount = firstWaveLineCount / 10;
SortedSet<Integer> document = new TreeSet<>();
IntStream.range(0, lineCount).forEach(i -> document.add(i));
Producer producer = environment.producerBuilder().name("producer-1").stream(stream).subEntrySize(subEntrySize).build();
AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(firstWaveLineCount));
ConfirmationHandler confirmationHandler = confirmationStatus -> latch.get().countDown();
Consumer<Integer> publishMessage = i -> producer.send(producer.messageBuilder().publishingId(i).addData(String.valueOf(i).getBytes()).build(), confirmationHandler);
document.headSet(firstWaveLineCount).forEach(publishMessage);
assertThat(latch.get().await(10, TimeUnit.SECONDS)).isTrue();
latch.set(new CountDownLatch(lineCount - firstWaveLineCount + backwardCount));
document.tailSet(firstWaveLineCount - backwardCount).forEach(publishMessage);
assertThat(latch.get().await(5, TimeUnit.SECONDS)).isTrue();
CountDownLatch consumeLatch = new CountDownLatch(lineCount);
AtomicInteger consumed = new AtomicInteger();
environment.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((offset, message) -> {
consumed.incrementAndGet();
consumeLatch.countDown();
}).build();
assertThat(consumeLatch.await(10, TimeUnit.SECONDS)).isTrue();
Thread.sleep(1000);
// if we are using sub-entries, we cannot avoid duplicates.
// here, a sub-entry in the second wave, right at the end of the re-submitted
// values will contain those duplicates, because its publishing ID will be
// the one of its last message, so the server will accept the whole sub-entry,
// including the duplicates.
assertThat(consumed.get()).isEqualTo(lineCount + backwardCount % subEntrySize);
}
use of com.rabbitmq.stream.ConfirmationHandler in project rabbitmq-stream-java-client by rabbitmq.
the class TlsTest method environmentPublisherConsumer.
@Test
void environmentPublisherConsumer() throws Exception {
try (Environment env = Environment.builder().uri("rabbitmq-stream+tls://localhost").addressResolver(addr -> new Address("localhost", Client.DEFAULT_TLS_PORT)).tls().sslContext(SslContextBuilder.forClient().trustManager(caCertificate()).build()).environmentBuilder().build()) {
int messageCount = 10_000;
CountDownLatch latchConfirm = new CountDownLatch(messageCount);
Producer producer = env.producerBuilder().stream(this.stream).build();
ConfirmationHandler confirmationHandler = confirmationStatus -> latchConfirm.countDown();
IntStream.range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().addData("".getBytes(StandardCharsets.UTF_8)).build(), confirmationHandler));
assertThat(latchAssert(latchConfirm)).completes();
CountDownLatch latchConsume = new CountDownLatch(messageCount);
env.consumerBuilder().stream(this.stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> latchConsume.countDown()).build();
assertThat(latchAssert(latchConsume)).completes();
}
}
Aggregations