use of com.rabbitmq.stream.Producer in project rabbitmq-stream-java-client by rabbitmq.
the class SuperStreamUsage method producerSimple.
void producerSimple() {
Environment environment = Environment.builder().build();
// tag::producer-simple[]
Producer producer = environment.producerBuilder().stream(// <1>
"invoices").routing(// <2>
message -> message.getProperties().getMessageIdAsString()).producerBuilder().build();
// ...
// <4>
producer.close();
// end::producer-simple[]
}
use of com.rabbitmq.stream.Producer in project rabbitmq-stream-java-client by rabbitmq.
the class SuperStreamUsage method producerKeyRoutingStrategy.
void producerKeyRoutingStrategy() {
Environment environment = Environment.builder().build();
// tag::producer-key-routing-strategy[]
Producer producer = environment.producerBuilder().stream("invoices").routing(// <1>
msg -> msg.getApplicationProperties().get("region").toString()).key().producerBuilder().build();
// end::producer-key-routing-strategy[]
}
use of com.rabbitmq.stream.Producer in project rabbitmq-stream-java-client by rabbitmq.
the class SuperStreamUsage method producerCustomRoutingStrategy.
void producerCustomRoutingStrategy() {
Environment environment = Environment.builder().build();
// tag::producer-custom-routing-strategy[]
AtomicLong messageCount = new AtomicLong(0);
RoutingStrategy routingStrategy = (message, metadata) -> {
List<String> partitions = metadata.partitions();
String stream = partitions.get((int) messageCount.getAndIncrement() % partitions.size());
return Collections.singletonList(stream);
};
Producer producer = environment.producerBuilder().stream("invoices").routing(// <1>
null).strategy(// <2>
routingStrategy).producerBuilder().build();
// end::producer-custom-routing-strategy[]
}
use of com.rabbitmq.stream.Producer in project rabbitmq-stream-java-client by rabbitmq.
the class AlarmsTest method publishingShouldBeBlockedWhenDiskAlarmIsOn.
@Test
void publishingShouldBeBlockedWhenDiskAlarmIsOn() 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 = diskAlarm()) {
latch.set(new CountDownLatch(messageCount));
new Thread(() -> range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().build(), confirmationHandler))).start();
assertThat(latchAssert(latch.get())).doesNotComplete(Duration.ofSeconds(5));
}
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 AlarmsTest method diskAlarmShouldNotPreventConsumption.
@Test
void diskAlarmShouldNotPreventConsumption() 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)).completes();
producer.close();
try (AutoCloseable alarm = diskAlarm()) {
latch.set(new CountDownLatch(messageCount));
Consumer consumer = env.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> latch.get().countDown()).build();
assertThat(latchAssert(latch)).completes();
consumer.close();
}
}
Aggregations