Search in sources :

Example 16 with Producer

use of com.rabbitmq.stream.Producer 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 17 with Producer

use of com.rabbitmq.stream.Producer 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 18 with Producer

use of com.rabbitmq.stream.Producer 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 19 with Producer

use of com.rabbitmq.stream.Producer 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)

Example 20 with Producer

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

the class ProducerUsage method producerWithName.

void producerWithName() {
    Environment environment = Environment.builder().build();
    // tag::producer-with-name[]
    Producer producer = environment.producerBuilder().name(// <1>
    "my-app-producer").confirmTimeout(// <2>
    Duration.ZERO).stream("my-stream").build();
    // end::producer-with-name[]
    // tag::message-with-publishing-id[]
    Message message = producer.messageBuilder().publishingId(// <1>
    1).addData("hello".getBytes(StandardCharsets.UTF_8)).build();
    producer.send(message, confirmationStatus -> {
    });
// end::message-with-publishing-id[]
}
Also used : Producer(com.rabbitmq.stream.Producer) Message(com.rabbitmq.stream.Message) Environment(com.rabbitmq.stream.Environment)

Aggregations

Producer (com.rabbitmq.stream.Producer)51 Environment (com.rabbitmq.stream.Environment)44 CountDownLatch (java.util.concurrent.CountDownLatch)36 Test (org.junit.jupiter.api.Test)36 OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)32 EnvironmentBuilder (com.rabbitmq.stream.EnvironmentBuilder)30 TestUtils.latchAssert (com.rabbitmq.stream.impl.TestUtils.latchAssert)30 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)30 BeforeEach (org.junit.jupiter.api.BeforeEach)30 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)30 TestUtils.localhost (com.rabbitmq.stream.impl.TestUtils.localhost)29 EventLoopGroup (io.netty.channel.EventLoopGroup)29 IntStream (java.util.stream.IntStream)28 UUID (java.util.UUID)26 AfterEach (org.junit.jupiter.api.AfterEach)26 TestInfo (org.junit.jupiter.api.TestInfo)26 ConfirmationHandler (com.rabbitmq.stream.ConfirmationHandler)25 AtomicLong (java.util.concurrent.atomic.AtomicLong)25 TestUtils.waitAtMost (com.rabbitmq.stream.impl.TestUtils.waitAtMost)24 Duration (java.time.Duration)24