use of com.rabbitmq.stream.Message in project rabbitmq-stream-java-client by rabbitmq.
the class Client method publish.
public List<Long> publish(byte publisherId, List<Message> messages, ToLongFunction<Object> publishSequenceFunction) {
List<Object> encodedMessages = new ArrayList<>(messages.size());
for (Message message : messages) {
Codec.EncodedMessage encodedMessage = codec.encode(message);
checkMessageFitsInFrame(encodedMessage);
encodedMessages.add(encodedMessage);
}
return publishInternal(this.channel, publisherId, encodedMessages, OUTBOUND_MESSAGE_WRITE_CALLBACK, publishSequenceFunction);
}
use of com.rabbitmq.stream.Message in project rabbitmq-stream-java-client by rabbitmq.
the class Client method publishBatches.
public List<Long> publishBatches(byte publisherId, List<MessageBatch> messageBatches, OutboundEntityMappingCallback mappingCallback, ToLongFunction<Object> publishSequenceFunction) {
List<Object> encodedMessageBatches = new ArrayList<>(messageBatches.size());
for (MessageBatch batch : messageBatches) {
EncodedMessageBatch encodedMessageBatch = createEncodedMessageBatch(batch.compression, batch.messages.size());
for (Message message : batch.messages) {
Codec.EncodedMessage encodedMessage = codec.encode(message);
checkMessageFitsInFrame(encodedMessage);
encodedMessageBatch.add(encodedMessage);
}
encodedMessageBatch.close();
checkMessageBatchFitsInFrame(encodedMessageBatch);
OriginalAndEncodedOutboundEntity wrapper = new OriginalAndEncodedOutboundEntity(batch, encodedMessageBatch);
encodedMessageBatches.add(wrapper);
}
return publishInternal(this.channel, publisherId, encodedMessageBatches, new OriginalEncodedEntityOutboundEntityWriteCallback(mappingCallback, OUTBOUND_MESSAGE_BATCH_WRITE_CALLBACK), publishSequenceFunction);
}
use of com.rabbitmq.stream.Message in project rabbitmq-stream-java-client by rabbitmq.
the class StompInteroperabilityTest method publishToStompDestinationConsumeFromStream.
@Test
void publishToStompDestinationConsumeFromStream() throws Exception {
String messageBody = UUID.randomUUID().toString();
stompConnect();
String receipt = UUID.randomUUID().toString();
byte[] frame = frameBuilder().command("SEND").header("destination", "/amq/queue/" + stream).header("content-type", "text/plain").header("content-length", String.valueOf(messageBody.length())).header("some-header", "some header value").header("receipt", receipt).body(messageBody).build();
out.write(frame);
AtomicBoolean gotReceipt = new AtomicBoolean(false);
read(line -> {
gotReceipt.compareAndSet(false, line.contains(receipt));
return line.equals(NULL) && gotReceipt.get();
});
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.getBytes(StandardCharsets.UTF_8));
assertThat(message.getProperties().getContentType()).isEqualTo("text/plain");
assertThat(message.getApplicationProperties().get("content-length")).isEqualTo(String.valueOf(messageBody.length()));
assertThat(message.getApplicationProperties().get("receipt")).isNotNull().isInstanceOf(String.class);
assertThat(message.getApplicationProperties().get("some-header")).isEqualTo("some header value");
assertThat(message.getMessageAnnotations().get("x-routing-key")).isEqualTo(stream);
assertThat(message.getMessageAnnotations().get("x-exchange")).isEqualTo("");
}
use of com.rabbitmq.stream.Message 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.Message 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));
}
}
Aggregations