Search in sources :

Example 11 with Environment

use of com.rabbitmq.stream.Environment in project rabbitmq-stream-java-client by rabbitmq.

the class EnvironmentUsage method createStreamWithRetention.

void createStreamWithRetention() {
    Environment environment = Environment.builder().build();
    // tag::stream-creation-retention[]
    environment.streamCreator().stream("my-stream").maxLengthBytes(// <1>
    ByteCapacity.GB(10)).maxSegmentSizeBytes(// <2>
    ByteCapacity.MB(500)).create();
// end::stream-creation-retention[]
}
Also used : Environment(com.rabbitmq.stream.Environment)

Example 12 with Environment

use of com.rabbitmq.stream.Environment in project rabbitmq-stream-java-client by rabbitmq.

the class SanityCheck method main.

public static void main(String[] args) {
    Environment env = null;
    String s = "sanity-check-stream";
    try {
        LOGGER.info("connecting");
        env = Environment.builder().build();
        LOGGER.info("connected");
        env.streamCreator().stream(s).create();
        LOGGER.info("test stream created");
        CountDownLatch publishLatch = new CountDownLatch(1);
        Producer producer = env.producerBuilder().stream(s).build();
        LOGGER.info("producer created");
        producer.send(producer.messageBuilder().addData("".getBytes(StandardCharsets.UTF_8)).build(), confirmationStatus -> publishLatch.countDown());
        LOGGER.info("waiting for publish confirm");
        boolean done = publishLatch.await(5, TimeUnit.SECONDS);
        if (!done) {
            throw new IllegalStateException("Did not receive publish confirm");
        }
        LOGGER.info("got publish confirm");
        CountDownLatch consumeLatch = new CountDownLatch(1);
        env.consumerBuilder().stream(s).offset(OffsetSpecification.first()).messageHandler((context, message) -> consumeLatch.countDown()).build();
        LOGGER.info("created consumer, waiting for message");
        done = consumeLatch.await(5, TimeUnit.SECONDS);
        if (!done) {
            throw new IllegalStateException("Did not receive message");
        }
        LOGGER.info("got message");
        LOGGER.info("Test succeeded with Stream Client {}", Environment.class.getPackage().getImplementationVersion());
        System.exit(0);
    } catch (Exception e) {
        LOGGER.info("Test failed with Stream Client {}", Environment.class.getPackage().getImplementationVersion(), e);
        System.exit(1);
    } finally {
        if (env != null) {
            env.deleteStream(s);
            env.close();
        }
    }
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Logger(org.slf4j.Logger) Environment(com.rabbitmq.stream.Environment) LoggerFactory(org.slf4j.LoggerFactory) Producer(com.rabbitmq.stream.Producer) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) StandardCharsets(java.nio.charset.StandardCharsets) Producer(com.rabbitmq.stream.Producer) Environment(com.rabbitmq.stream.Environment) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 13 with Environment

use of com.rabbitmq.stream.Environment in project rabbitmq-stream-java-client by rabbitmq.

the class ProducerUsage method producerCreation.

void producerCreation() throws Exception {
    Environment environment = Environment.builder().build();
    // tag::producer-creation[]
    Producer producer = // <1>
    environment.producerBuilder().stream(// <2>
    "my-stream").build();
    // ...
    // <4>
    producer.close();
// end::producer-creation[]
}
Also used : Producer(com.rabbitmq.stream.Producer) Environment(com.rabbitmq.stream.Environment)

Example 14 with Environment

use of com.rabbitmq.stream.Environment in project rabbitmq-stream-java-client by rabbitmq.

the class ProducerUsage method producerWithNameQueryLastPublishingId.

void producerWithNameQueryLastPublishingId() {
    Environment environment = Environment.builder().build();
    // tag::producer-queries-last-publishing-id[]
    Producer producer = environment.producerBuilder().name(// <1>
    "my-app-producer").confirmTimeout(// <2>
    Duration.ZERO).stream("my-stream").build();
    // <3>
    long nextPublishingId = producer.getLastPublishingId() + 1;
    while (moreContent(nextPublishingId)) {
        // <4>
        byte[] content = getContent(nextPublishingId);
        Message message = producer.messageBuilder().publishingId(// <5>
        nextPublishingId).addData(content).build();
        producer.send(message, confirmationStatus -> {
        });
        nextPublishingId++;
    }
// end::producer-queries-last-publishing-id[]
}
Also used : Producer(com.rabbitmq.stream.Producer) Message(com.rabbitmq.stream.Message) Environment(com.rabbitmq.stream.Environment)

Example 15 with Environment

use of com.rabbitmq.stream.Environment in project rabbitmq-stream-java-client by rabbitmq.

the class ProducerUsage method producerComplexMessage.

void producerComplexMessage() {
    Environment environment = Environment.builder().build();
    Producer producer = environment.producerBuilder().stream("my-stream").build();
    // tag::producer-publish-complex-message[]
    Message message = // <1>
    producer.messageBuilder().properties().messageId(UUID.randomUUID()).correlationId(UUID.randomUUID()).contentType("text/plain").messageBuilder().addData(// <4>
    "hello".getBytes(StandardCharsets.UTF_8)).build();
    // <6>
    producer.send(message, confirmationStatus -> {
    });
// end::producer-publish-complex-message[]
}
Also used : Producer(com.rabbitmq.stream.Producer) Message(com.rabbitmq.stream.Message) Environment(com.rabbitmq.stream.Environment)

Aggregations

Environment (com.rabbitmq.stream.Environment)35 Producer (com.rabbitmq.stream.Producer)20 Test (org.junit.jupiter.api.Test)13 Consumer (com.rabbitmq.stream.Consumer)12 Message (com.rabbitmq.stream.Message)12 Address (com.rabbitmq.stream.Address)10 Collections (java.util.Collections)10 OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)9 List (java.util.List)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 ConsumerBuilder (com.rabbitmq.stream.ConsumerBuilder)7 StreamException (com.rabbitmq.stream.StreamException)7 Duration (java.time.Duration)7 ChannelCustomizer (com.rabbitmq.stream.ChannelCustomizer)6 ConfirmationHandler (com.rabbitmq.stream.ConfirmationHandler)6 ProducerBuilder (com.rabbitmq.stream.ProducerBuilder)6 StreamCreator (com.rabbitmq.stream.StreamCreator)6 SslHandler (io.netty.handler.ssl.SslHandler)6 StandardCharsets (java.nio.charset.StandardCharsets)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6