use of com.rabbitmq.stream.Message 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.Message 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.Message in project rabbitmq-stream-java-client by rabbitmq.
the class OutboundMappingCallbackTest method publishBatches.
@Test
void publishBatches() throws Exception {
int subEntryCount = 10;
int messagesInFrameCount = 100;
int frameCount = 1000;
CountDownLatch mappingLatch = new CountDownLatch(frameCount * messagesInFrameCount);
CountDownLatch confirmLatch = new CountDownLatch(frameCount * messagesInFrameCount);
Set<Long> mapped = ConcurrentHashMap.newKeySet(frameCount * messagesInFrameCount);
Client client = cf.get(new Client.ClientParameters().publishConfirmListener((publisherId, publishingId) -> confirmLatch.countDown()));
client.declarePublisher(b(1), null, stream);
IntStream.range(0, frameCount).forEach(frameIndex -> {
List<MessageBatch> batches = new ArrayList<>(messagesInFrameCount);
IntStream.range(0, messagesInFrameCount).forEach(batchIndex -> {
List<Message> messages = IntStream.range(0, subEntryCount).mapToObj(messageIndex -> String.valueOf(messageIndex).getBytes()).map(body -> client.messageBuilder().addData(body).build()).collect(Collectors.toList());
MessageBatch batch = new MessageBatch(Compression.NONE, messages);
batches.add(batch);
});
client.publishBatches(b(1), batches, (publishingId, original) -> {
assertThat(original).isNotNull().isInstanceOf(MessageBatch.class);
mapped.add(publishingId);
mappingLatch.countDown();
});
});
assertThat(mappingLatch.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(confirmLatch.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(mapped).hasSize(frameCount * messagesInFrameCount);
}
use of com.rabbitmq.stream.Message in project rabbitmq-stream-java-client by rabbitmq.
the class MqttInteroperabilityTest method publishToMqttTopicConsumeFromStream.
@Test
void publishToMqttTopicConsumeFromStream() throws Exception {
try (Connection c = new ConnectionFactory().newConnection()) {
Channel ch = c.createChannel();
ch.queueBind(stream, "amq.topic", stream);
}
byte[] messageBody = UUID.randomUUID().toString().getBytes(StandardCharsets.UTF_8);
String uri = "tcp://localhost:1883";
String clientId = "mqtt-test";
try (IMqttClient c = new MqttClient(uri, clientId, null)) {
MqttConnectOptions opts = new MqttConnectOptions();
opts.setUserName("guest");
opts.setPassword("guest".toCharArray());
opts.setCleanSession(true);
c.connect(opts);
MqttTopic topic = c.getTopic(stream);
MqttMessage message = new MqttMessage(messageBody);
message.setQos(0);
message.setRetained(false);
MqttDeliveryToken token = topic.publish(message);
token.waitForCompletion();
c.disconnect(5000);
}
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<Message> messageReference = new AtomicReference<>();
env.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message1) -> {
messageReference.set(message1);
latch.countDown();
}).build();
assertThat(latchAssert(latch)).completes();
Message message = messageReference.get();
assertThat(message.getBodyAsBinary()).isEqualTo(messageBody);
// see
// https://github.com/rabbitmq/rabbitmq-mqtt/blob/ebcb6dabf0e2b2f34315bc90530ca7791330df24/src/rabbit_mqtt_processor.erl#L856-L860
assertThat(message.getMessageAnnotations().get("x-basic-delivery-mode")).isEqualTo(UnsignedByte.valueOf("1"));
assertThat(message.getApplicationProperties().get("x-mqtt-publish-qos")).isEqualTo(Byte.valueOf("0"));
assertThat(message.getApplicationProperties().get("x-mqtt-dup")).isEqualTo(Boolean.FALSE);
}
use of com.rabbitmq.stream.Message in project rabbitmq-stream-java-client by rabbitmq.
the class EncodeDecodeForPerformanceToolBenchmark method decode.
@Benchmark
public void decode() {
Message message = codec.decode(messageToDecode);
long latency = timestampProvider.get() - readLong(message.getBodyAsBinary());
}
Aggregations