use of com.rabbitmq.stream.Producer in project rabbitmq-stream-java-client by rabbitmq.
the class AlarmsTest method creatingPublisherWhenMemoryAlarmIsOnShouldSucceed.
@Test
void creatingPublisherWhenMemoryAlarmIsOnShouldSucceed() throws Exception {
try (AutoCloseable alarm = memoryAlarm()) {
Producer producer = env.producerBuilder().stream(stream).build();
producer.close();
}
}
use of com.rabbitmq.stream.Producer in project rabbitmq-stream-java-client by rabbitmq.
the class AlarmsTest method publishingWhenMemoryAlarmIsOnShouldWork.
@Test
void publishingWhenMemoryAlarmIsOnShouldWork() throws Exception {
int messageCount = 50_000;
AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(messageCount));
ConfirmationHandler confirmationHandler = confirmationStatus -> latch.get().countDown();
Producer producer = env.producerBuilder().stream(stream).build();
range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().build(), confirmationHandler));
assertThat(latchAssert(latch.get())).completes();
try (AutoCloseable alarm = memoryAlarm()) {
latch.set(new CountDownLatch(messageCount));
new Thread(() -> range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().build(), confirmationHandler))).start();
assertThat(latchAssert(latch.get())).completes();
}
assertThat(latchAssert(latch.get())).completes();
producer.close();
latch.set(new CountDownLatch(messageCount * 2));
Consumer consumer = env.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> latch.get().countDown()).build();
assertThat(latchAssert(latch.get())).completes();
consumer.close();
}
use of com.rabbitmq.stream.Producer in project rabbitmq-stream-java-client by rabbitmq.
the class StreamProducerBuilder method build.
public Producer build() {
if (subEntrySize == 1 && compression != null) {
throw new IllegalArgumentException("Sub-entry batching must be enabled to enable compression");
}
if (subEntrySize > 1 && compression == null) {
compression = Compression.NONE;
}
this.environment.maybeInitializeLocator();
Producer producer;
if (this.routingConfiguration == null) {
producer = new StreamProducer(name, stream, subEntrySize, batchSize, compression, batchPublishingDelay, maxUnconfirmedMessages, confirmTimeout, enqueueTimeout, environment);
this.environment.addProducer((StreamProducer) producer);
} else {
RoutingStrategy routingStrategy = this.routingConfiguration.routingStrategy;
if (routingStrategy == null) {
if (this.routingConfiguration.hash == null) {
routingStrategy = new RoutingKeyRoutingStrategy(this.routingConfiguration.routingKeyExtractor);
} else {
routingStrategy = new HashRoutingStrategy(this.routingConfiguration.routingKeyExtractor, this.routingConfiguration.hash);
}
}
producer = new SuperStreamProducer(this, this.name, this.stream, routingStrategy, this.environment);
}
return producer;
}
use of com.rabbitmq.stream.Producer in project rabbitmq-stream-java-client by rabbitmq.
the class SuperStreamProducer method send.
@Override
public void send(Message message, ConfirmationHandler confirmationHandler) {
List<String> streams = this.routingStrategy.route(message, superStreamMetadata);
if (streams.isEmpty()) {
confirmationHandler.handle(new ConfirmationStatus(message, false, Constants.CODE_NO_ROUTE_FOUND));
} else {
for (String stream : streams) {
Producer producer = producers.computeIfAbsent(stream, stream1 -> {
Producer p = producerBuilder.duplicate().stream(stream1).build();
return p;
});
producer.send(message, confirmationHandler);
}
}
}
use of com.rabbitmq.stream.Producer in project rabbitmq-stream-java-client by rabbitmq.
the class StompInteroperabilityTest method publishToStreamConsumeFromStomp.
@Test
void publishToStreamConsumeFromStomp() throws Exception {
byte[] messageBody = UUID.randomUUID().toString().getBytes(StandardCharsets.UTF_8);
stompConnect();
stompSubscribe(stream, "client", 1);
CountDownLatch publishLatch = new CountDownLatch(1);
Producer producer = env.producerBuilder().stream(this.stream).build();
producer.send(producer.messageBuilder().addData(messageBody).properties().messageId(42).userId("the user ID".getBytes(StandardCharsets.UTF_8)).replyTo("reply to").correlationId("the correlation id").contentType("text/plain").contentEncoding("identity").creationTime(1_000_000).messageBuilder().applicationProperties().entry("some-header", "some header value").messageBuilder().build(), confirmationStatus -> publishLatch.countDown());
assertThat(latchAssert(publishLatch)).completes();
List<String> lines = new CopyOnWriteArrayList<>();
read(line -> {
lines.add(line);
return line.contains(NULL);
});
assertThat(lines).contains("MESSAGE");
String payload = null;
Map<String, String> headers = new HashMap<>();
for (String line : lines) {
if (line.contains(NULL)) {
payload = line.replace(NULL, "");
} else if (line.contains(":")) {
headers.put(line.split(":")[0], line.split(":")[1]);
}
}
assertThat(payload).isNotNull().isEqualTo(new String(messageBody));
assertThat(headers.get("x-message-id-type")).isEqualTo("ulong");
assertThat(headers.get("amqp-message-id")).isEqualTo("42");
assertThat(headers.get("message-id")).isNotEqualTo("42");
assertThat(headers.get("user-id")).isEqualTo("the user ID");
assertThat(headers.get("reply-to")).isEqualTo("/reply-queue/reply to");
assertThat(headers.get("content-type")).isEqualTo("text/plain");
assertThat(headers.get("content-encoding")).isEqualTo("identity");
assertThat(headers.get("correlation-id")).isEqualTo("the correlation id");
// in seconds
assertThat(headers.get("timestamp")).isEqualTo("1000");
assertThat(headers.get("some-header")).isEqualTo("some header value");
assertThat(headers.get("x-stream-offset")).isNotNull().isEqualTo("0");
}
Aggregations