use of com.rabbitmq.stream.Producer in project rabbitmq-stream-java-client by rabbitmq.
the class StreamEnvironmentTest method environmentPublishersConsumersShouldCloseSuccessfullyWhenBrokerIsDown.
@Test
@TestUtils.DisabledIfRabbitMqCtlNotSet
void environmentPublishersConsumersShouldCloseSuccessfullyWhenBrokerIsDown() throws Exception {
Environment environment = environmentBuilder.recoveryBackOffDelayPolicy(BackOffDelayPolicy.fixed(Duration.ofSeconds(10))).build();
CountDownLatch consumeLatch = new CountDownLatch(2);
Consumer consumer = environment.consumerBuilder().stream(stream).messageHandler((context, message) -> consumeLatch.countDown()).build();
// will be closed by the environment
environment.consumerBuilder().stream(stream).messageHandler((context, message) -> consumeLatch.countDown()).build();
Producer producer = environment.producerBuilder().stream(stream).build();
// will be closed by the environment
environment.producerBuilder().stream(stream).build();
producer.send(producer.messageBuilder().addData("".getBytes(StandardCharsets.UTF_8)).build(), confirmationStatus -> {
});
latchAssert(consumeLatch).completes();
try {
Host.rabbitmqctl("stop_app");
producer.close();
consumer.close();
environment.close();
} finally {
Host.rabbitmqctl("start_app");
}
waitAtMost(30, () -> {
Client client = cf.get();
Map<String, StreamMetadata> metadata = client.metadata(stream);
return metadata.containsKey(stream) && metadata.get(stream).isResponseOk();
});
}
use of com.rabbitmq.stream.Producer in project rabbitmq-stream-java-client by rabbitmq.
the class StreamEnvironmentTest method producersAndConsumersShouldBeClosedWhenEnvironmentIsClosed.
@ParameterizedTest
@ValueSource(booleans = { false, true })
void producersAndConsumersShouldBeClosedWhenEnvironmentIsClosed(boolean lazyInit) {
Environment environment = environmentBuilder.lazyInitialization(lazyInit).build();
Collection<Producer> producers = IntStream.range(0, 2).mapToObj(i -> environment.producerBuilder().stream(stream).build()).collect(Collectors.toList());
Collection<Consumer> consumers = IntStream.range(0, 2).mapToObj(i -> environment.consumerBuilder().stream(stream).name(UUID.randomUUID().toString()).messageHandler((offset, message) -> {
}).build()).collect(Collectors.toList());
producers.forEach(producer -> assertThat(((StreamProducer) producer).isOpen()).isTrue());
consumers.forEach(consumer -> assertThat(((StreamConsumer) consumer).isOpen()).isTrue());
EnvironmentInfo environmentInfo = MonitoringTestUtils.extract(environment);
assertThat(environmentInfo.getLocator()).isNotNull();
assertThat(environmentInfo.getProducers()).hasSize(1).element(0).extracting(pool -> pool.getClients()).asList().hasSize(1);
assertThat(environmentInfo.getProducers().get(0).getClients().get(0).getProducerCount()).isEqualTo(2);
assertThat(environmentInfo.getProducers().get(0).getClients().get(0).getTrackingConsumerCount()).isEqualTo(2);
assertThat(environmentInfo.getConsumers()).hasSize(1).element(0).extracting(pool -> pool.getClients()).asList().hasSize(1);
assertThat(environmentInfo.getConsumers().get(0).getClients().get(0).getConsumerCount()).isEqualTo(2);
environment.close();
producers.forEach(producer -> assertThat(((StreamProducer) producer).isOpen()).isFalse());
consumers.forEach(consumer -> assertThat(((StreamConsumer) consumer).isOpen()).isFalse());
environmentInfo = MonitoringTestUtils.extract(environment);
assertThat(environmentInfo.getLocator()).isNull();
assertThat(environmentInfo.getProducers()).isEmpty();
assertThat(environmentInfo.getConsumers()).isEmpty();
}
use of com.rabbitmq.stream.Producer in project rabbitmq-stream-java-client by rabbitmq.
the class StreamEnvironmentTest method locatorShouldReconnectIfConnectionIsLost.
@Test
@TestUtils.DisabledIfRabbitMqCtlNotSet
void locatorShouldReconnectIfConnectionIsLost(TestInfo info) throws Exception {
try (Environment environment = environmentBuilder.recoveryBackOffDelayPolicy(BackOffDelayPolicy.fixed(Duration.ofSeconds(1))).build()) {
String s = streamName(info);
environment.streamCreator().stream(s).create();
environment.deleteStream(s);
Host.killConnection("rabbitmq-stream-locator-0");
environment.streamCreator().stream(s).create();
try {
Producer producer = environment.producerBuilder().stream(s).build();
Consumer consumer = environment.consumerBuilder().stream(s).build();
producer.close();
consumer.close();
} finally {
environment.deleteStream(s);
}
}
}
use of com.rabbitmq.stream.Producer in project rabbitmq-stream-java-client by rabbitmq.
the class StreamProducerTest method producerShouldBeClosedWhenStreamIsDeleted.
@ParameterizedTest
@ValueSource(ints = { 1, 7 })
void producerShouldBeClosedWhenStreamIsDeleted(int subEntrySize, TestInfo info) throws Exception {
Level initialLogLevel = TestUtils.newLoggerLevel(ProducersCoordinator.class, Level.DEBUG);
try {
String s = streamName(info);
environment.streamCreator().stream(s).create();
StreamProducer producer = (StreamProducer) environment.producerBuilder().subEntrySize(subEntrySize).stream(s).build();
AtomicInteger published = new AtomicInteger(0);
AtomicInteger confirmed = new AtomicInteger(0);
AtomicInteger errored = new AtomicInteger(0);
Set<Number> errorCodes = ConcurrentHashMap.newKeySet();
AtomicBoolean continuePublishing = new AtomicBoolean(true);
Thread publishThread = new Thread(() -> {
ConfirmationHandler confirmationHandler = confirmationStatus -> {
if (confirmationStatus.isConfirmed()) {
confirmed.incrementAndGet();
} else {
errored.incrementAndGet();
errorCodes.add(confirmationStatus.getCode());
}
};
while (continuePublishing.get()) {
try {
producer.send(producer.messageBuilder().addData("".getBytes(StandardCharsets.UTF_8)).build(), confirmationHandler);
published.incrementAndGet();
} catch (StreamException e) {
// OK
}
}
});
publishThread.start();
Thread.sleep(1000L);
assertThat(producer.isOpen()).isTrue();
environment.deleteStream(s);
waitAtMost(() -> !producer.isOpen());
continuePublishing.set(false);
waitAtMost(() -> !errorCodes.isEmpty(), () -> "The producer should have received negative publish confirms");
} finally {
TestUtils.newLoggerLevel(ProducersCoordinator.class, initialLogLevel);
}
}
use of com.rabbitmq.stream.Producer in project rabbitmq-stream-java-client by rabbitmq.
the class StreamProducerTest method subEntryBatchesSentCompressedShouldBeConsumedProperly.
@Test
void subEntryBatchesSentCompressedShouldBeConsumedProperly() {
int messagePerProducer = 10000;
int messageCount = Compression.values().length * messagePerProducer;
CountDownLatch publishLatch = new CountDownLatch(messageCount);
ConfirmationHandler confirmationHandler = confirmationStatus -> publishLatch.countDown();
AtomicInteger messageIndex = new AtomicInteger(0);
Set<String> publishedBodies = ConcurrentHashMap.newKeySet(messageCount);
for (Compression compression : Compression.values()) {
Producer producer = environment.producerBuilder().stream(stream).subEntrySize(100).compression(compression).build();
IntStream.range(0, messagePerProducer).forEach(i -> {
String body = "compression " + compression.name() + " message " + messageIndex.getAndIncrement();
producer.send(producer.messageBuilder().addData(body.getBytes(StandardCharsets.UTF_8)).build(), confirmationHandler);
publishedBodies.add(body);
});
}
assertThat(latchAssert(publishLatch)).completes();
Set<String> consumedBodies = ConcurrentHashMap.newKeySet(messageCount);
CountDownLatch consumeLatch = new CountDownLatch(messageCount);
environment.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> {
consumedBodies.add(new String(message.getBodyAsBinary(), StandardCharsets.UTF_8));
consumeLatch.countDown();
}).build();
assertThat(latchAssert(consumeLatch)).completes();
assertThat(consumedBodies).isNotEmpty().hasSameSizeAs(publishedBodies);
publishedBodies.forEach(publishBody -> assertThat(consumedBodies.contains(publishBody)).isTrue());
}
Aggregations