use of com.rabbitmq.stream.Environment in project rabbitmq-stream-java-client by rabbitmq.
the class StreamEnvironmentTest method createPublishConsumeDelete.
@ParameterizedTest
@ValueSource(booleans = { false, true })
void createPublishConsumeDelete(boolean lazyInit, TestInfo info) {
try (Environment env = environmentBuilder.lazyInitialization(lazyInit).build()) {
String s = streamName(info);
env.streamCreator().stream(s).create();
int messageCount = 50_000;
CountDownLatch confirmLatch = new CountDownLatch(messageCount);
CountDownLatch consumeLatch = new CountDownLatch(messageCount);
Producer producer = env.producerBuilder().stream(s).build();
ConfirmationHandler confirmationHandler = confirmationStatus -> confirmLatch.countDown();
IntStream.range(0, messageCount).forEach(i -> {
Message message = producer.messageBuilder().addData("".getBytes(StandardCharsets.UTF_8)).build();
producer.send(message, confirmationHandler);
});
latchAssert(confirmLatch).completes();
Consumer consumer = env.consumerBuilder().stream(s).offset(OffsetSpecification.first()).messageHandler((context, message) -> consumeLatch.countDown()).build();
latchAssert(consumeLatch).completes();
producer.close();
consumer.close();
env.deleteStream(s);
}
}
use of com.rabbitmq.stream.Environment in project rabbitmq-stream-java-client by rabbitmq.
the class StreamEnvironmentTest method growShrinkResourcesWhenProducersConsumersAreOpenedAndClosed.
@Test
void growShrinkResourcesWhenProducersConsumersAreOpenedAndClosed(TestInfo info) throws Exception {
int messageCount = 100;
int streamCount = 20;
int producersCount = ProducersCoordinator.MAX_PRODUCERS_PER_CLIENT * 3 + 10;
int consumersCount = ConsumersCoordinator.MAX_SUBSCRIPTIONS_PER_CLIENT * 2 + 10;
try (Environment environment = environmentBuilder.build()) {
List<String> streams = IntStream.range(0, streamCount).mapToObj(i -> streamName(info)).map(s -> {
environment.streamCreator().stream(s).create();
return s;
}).collect(Collectors.toCollection(() -> new CopyOnWriteArrayList<>()));
CountDownLatch confirmLatch = new CountDownLatch(messageCount * producersCount);
CountDownLatch consumeLatch = new CountDownLatch(messageCount * producersCount);
List<Producer> producers = IntStream.range(0, producersCount).mapToObj(i -> {
String s = streams.get(i % streams.size());
return environment.producerBuilder().stream(s).build();
}).collect(Collectors.toList());
List<Consumer> consumers = IntStream.range(0, consumersCount).mapToObj(i -> {
String s = streams.get(new Random().nextInt(streams.size()));
return environment.consumerBuilder().stream(s).messageHandler((offset, message) -> consumeLatch.countDown()).build();
}).collect(Collectors.toList());
producers.stream().parallel().forEach(producer -> {
IntStream.range(0, messageCount).forEach(messageIndex -> {
producer.send(producer.messageBuilder().addData("".getBytes()).build(), confirmationStatus -> {
if (confirmationStatus.isConfirmed()) {
confirmLatch.countDown();
}
});
});
});
assertThat(confirmLatch.await(10, SECONDS)).isTrue();
assertThat(consumeLatch.await(10, SECONDS)).isTrue();
EnvironmentInfo environmentInfo = MonitoringTestUtils.extract(environment);
assertThat(environmentInfo.getProducers()).hasSize(1);
int producerManagerCount = environmentInfo.getProducers().get(0).getClients().size();
assertThat(producerManagerCount).isPositive();
assertThat(environmentInfo.getConsumers()).hasSize(1);
int consumerManagerCount = environmentInfo.getConsumers().get(0).getClients().size();
assertThat(consumerManagerCount).isPositive();
java.util.function.Consumer<AutoCloseable> closing = agent -> {
try {
agent.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
};
Collections.reverse(producers);
List<Producer> subProducers = producers.subList(0, ProducersCoordinator.MAX_PRODUCERS_PER_CLIENT);
subProducers.forEach(closing);
Collections.reverse(consumers);
List<Consumer> subConsumers = consumers.subList(0, ConsumersCoordinator.MAX_SUBSCRIPTIONS_PER_CLIENT);
subConsumers.forEach(closing);
producers.removeAll(subProducers);
consumers.removeAll(subConsumers);
environmentInfo = MonitoringTestUtils.extract(environment);
assertThat(environmentInfo.getProducers()).hasSize(1);
assertThat(environmentInfo.getProducers().get(0).getClients()).hasSizeLessThan(producerManagerCount);
assertThat(environmentInfo.getConsumers()).hasSize(1);
assertThat(environmentInfo.getConsumers().get(0).getClients()).hasSizeLessThan(consumerManagerCount);
producers.forEach(closing);
consumers.forEach(closing);
streams.stream().forEach(stream -> environment.deleteStream(stream));
}
}
use of com.rabbitmq.stream.Environment in project rabbitmq-stream-java-client by rabbitmq.
the class StreamEnvironmentTest method streamCreationShouldBeIdempotent.
@Test
void streamCreationShouldBeIdempotent(TestInfo info) {
String s = streamName(info);
Client client = cf.get();
try (Environment env = environmentBuilder.build()) {
Duration retention = Duration.ofDays(4);
env.streamCreator().stream(s).maxAge(retention).create();
env.streamCreator().stream(s).maxAge(retention).create();
} finally {
assertThat(client.delete(s).isOk()).isTrue();
}
}
use of com.rabbitmq.stream.Environment 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();
}
}
use of com.rabbitmq.stream.Environment in project spring-amqp by spring-projects.
the class StreamListenerContainerTests method testAdviceChain.
@Test
void testAdviceChain() throws Exception {
Environment env = mock(Environment.class);
ConsumerBuilder builder = mock(ConsumerBuilder.class);
given(env.consumerBuilder()).willReturn(builder);
AtomicReference<MessageHandler> handler = new AtomicReference<>();
willAnswer(inv -> {
handler.set(inv.getArgument(0));
return null;
}).given(builder).messageHandler(any());
AtomicBoolean advised = new AtomicBoolean();
MethodInterceptor advice = (inv) -> {
advised.set(true);
return inv.proceed();
};
StreamListenerContainer container = new StreamListenerContainer(env);
container.setAdviceChain(advice);
AtomicBoolean called = new AtomicBoolean();
MessageListener ml = mock(MessageListener.class);
willAnswer(inv -> {
called.set(true);
return null;
}).given(ml).onMessage(any());
container.setupMessageListener(ml);
Message message = mock(Message.class);
given(message.getBodyAsBinary()).willReturn("foo".getBytes());
Context context = mock(Context.class);
handler.get().handle(context, message);
assertThat(advised.get()).isTrue();
assertThat(called.get()).isTrue();
advised.set(false);
called.set(false);
ChannelAwareMessageListener cal = mock(ChannelAwareMessageListener.class);
willAnswer(inv -> {
called.set(true);
return null;
}).given(cal).onMessage(any(), isNull());
container.setupMessageListener(cal);
handler.get().handle(context, message);
assertThat(advised.get()).isTrue();
assertThat(called.get()).isTrue();
called.set(false);
StreamMessageListener sml = mock(StreamMessageListener.class);
willAnswer(inv -> {
called.set(true);
return null;
}).given(sml).onStreamMessage(message, context);
container.setupMessageListener(sml);
handler.get().handle(context, message);
assertThat(advised.get()).isTrue();
assertThat(called.get()).isTrue();
}
Aggregations