Search in sources :

Example 31 with Producer

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();
    }
}
Also used : Producer(com.rabbitmq.stream.Producer) Test(org.junit.jupiter.api.Test)

Example 32 with Producer

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();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Host.memoryAlarm(com.rabbitmq.stream.Host.memoryAlarm) IntStream.range(java.util.stream.IntStream.range) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) AtomicReference(java.util.concurrent.atomic.AtomicReference) Host.diskAlarm(com.rabbitmq.stream.Host.diskAlarm) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) AfterAll(org.junit.jupiter.api.AfterAll) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) StreamException(com.rabbitmq.stream.StreamException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) EventLoopGroup(io.netty.channel.EventLoopGroup) Environment(com.rabbitmq.stream.Environment) Consumer(com.rabbitmq.stream.Consumer) Producer(com.rabbitmq.stream.Producer) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) TestUtils.responseCode(com.rabbitmq.stream.impl.TestUtils.responseCode) Constants(com.rabbitmq.stream.Constants) SECONDS(java.util.concurrent.TimeUnit.SECONDS) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Producer(com.rabbitmq.stream.Producer) Consumer(com.rabbitmq.stream.Consumer) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 33 with Producer

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;
}
Also used : Producer(com.rabbitmq.stream.Producer) RoutingStrategy(com.rabbitmq.stream.RoutingStrategy)

Example 34 with 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);
        }
    }
}
Also used : ConfirmationStatus(com.rabbitmq.stream.ConfirmationStatus) Producer(com.rabbitmq.stream.Producer)

Example 35 with Producer

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");
}
Also used : Producer(com.rabbitmq.stream.Producer) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CountDownLatch(java.util.concurrent.CountDownLatch) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test)

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