Search in sources :

Example 1 with Message

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

the class StreamEnvironmentTest method environmentPublishersConsumersShouldCloseSuccessfullyWhenBrokerIsDown.

@Test
@TestUtils.DisabledIfRabbitMqCtlNotSet
void environmentPublishersConsumersShouldCloseSuccessfullyWhenBrokerIsDown() throws Exception {
    Environment environment = environmentBuilder.recoveryBackOffDelayPolicy(BackOffDelayPolicy.fixed(Duration.ofSeconds(10))).build();
    CountDownLatch consumeLatch = new CountDownLatch(2);
    Consumer consumer = environment.consumerBuilder().stream(stream).messageHandler((context, message) -> consumeLatch.countDown()).build();
    // will be closed by the environment
    environment.consumerBuilder().stream(stream).messageHandler((context, message) -> consumeLatch.countDown()).build();
    Producer producer = environment.producerBuilder().stream(stream).build();
    // will be closed by the environment
    environment.producerBuilder().stream(stream).build();
    producer.send(producer.messageBuilder().addData("".getBytes(StandardCharsets.UTF_8)).build(), confirmationStatus -> {
    });
    latchAssert(consumeLatch).completes();
    try {
        Host.rabbitmqctl("stop_app");
        producer.close();
        consumer.close();
        environment.close();
    } finally {
        Host.rabbitmqctl("start_app");
    }
    waitAtMost(30, () -> {
        Client client = cf.get();
        Map<String, StreamMetadata> metadata = client.metadata(stream);
        return metadata.containsKey(stream) && metadata.get(stream).isResponseOk();
    });
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SNIHostName(javax.net.ssl.SNIHostName) Message(com.rabbitmq.stream.Message) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) TestUtils.streamName(com.rabbitmq.stream.impl.TestUtils.streamName) AuthenticationFailureException(com.rabbitmq.stream.AuthenticationFailureException) AfterAll(org.junit.jupiter.api.AfterAll) StreamException(com.rabbitmq.stream.StreamException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) Duration(java.time.Duration) Map(java.util.Map) Host(com.rabbitmq.stream.Host) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) Collection(java.util.Collection) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) StandardCharsets(java.nio.charset.StandardCharsets) TestInfo(org.junit.jupiter.api.TestInfo) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) StreamCreator(com.rabbitmq.stream.StreamCreator) SslHandler(io.netty.handler.ssl.SslHandler) ProducerBuilder(com.rabbitmq.stream.ProducerBuilder) Constants(com.rabbitmq.stream.Constants) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) IntStream(java.util.stream.IntStream) SSLParameters(javax.net.ssl.SSLParameters) StreamMetadata(com.rabbitmq.stream.impl.Client.StreamMetadata) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) Address(com.rabbitmq.stream.Address) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) EnvironmentInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.EnvironmentInfo) ConnectException(java.net.ConnectException) ChannelCustomizer(com.rabbitmq.stream.ChannelCustomizer) ValueSource(org.junit.jupiter.params.provider.ValueSource) 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) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) DisabledIfTlsNotEnabled(com.rabbitmq.stream.impl.TestUtils.DisabledIfTlsNotEnabled) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) TestUtils.localhostTls(com.rabbitmq.stream.impl.TestUtils.localhostTls) StreamMetadata(com.rabbitmq.stream.impl.Client.StreamMetadata) Consumer(com.rabbitmq.stream.Consumer) Producer(com.rabbitmq.stream.Producer) Environment(com.rabbitmq.stream.Environment) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with Message

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

the class StreamEnvironmentTest method producersAndConsumersShouldBeClosedWhenEnvironmentIsClosed.

@ParameterizedTest
@ValueSource(booleans = { false, true })
void producersAndConsumersShouldBeClosedWhenEnvironmentIsClosed(boolean lazyInit) {
    Environment environment = environmentBuilder.lazyInitialization(lazyInit).build();
    Collection<Producer> producers = IntStream.range(0, 2).mapToObj(i -> environment.producerBuilder().stream(stream).build()).collect(Collectors.toList());
    Collection<Consumer> consumers = IntStream.range(0, 2).mapToObj(i -> environment.consumerBuilder().stream(stream).name(UUID.randomUUID().toString()).messageHandler((offset, message) -> {
    }).build()).collect(Collectors.toList());
    producers.forEach(producer -> assertThat(((StreamProducer) producer).isOpen()).isTrue());
    consumers.forEach(consumer -> assertThat(((StreamConsumer) consumer).isOpen()).isTrue());
    EnvironmentInfo environmentInfo = MonitoringTestUtils.extract(environment);
    assertThat(environmentInfo.getLocator()).isNotNull();
    assertThat(environmentInfo.getProducers()).hasSize(1).element(0).extracting(pool -> pool.getClients()).asList().hasSize(1);
    assertThat(environmentInfo.getProducers().get(0).getClients().get(0).getProducerCount()).isEqualTo(2);
    assertThat(environmentInfo.getProducers().get(0).getClients().get(0).getTrackingConsumerCount()).isEqualTo(2);
    assertThat(environmentInfo.getConsumers()).hasSize(1).element(0).extracting(pool -> pool.getClients()).asList().hasSize(1);
    assertThat(environmentInfo.getConsumers().get(0).getClients().get(0).getConsumerCount()).isEqualTo(2);
    environment.close();
    producers.forEach(producer -> assertThat(((StreamProducer) producer).isOpen()).isFalse());
    consumers.forEach(consumer -> assertThat(((StreamConsumer) consumer).isOpen()).isFalse());
    environmentInfo = MonitoringTestUtils.extract(environment);
    assertThat(environmentInfo.getLocator()).isNull();
    assertThat(environmentInfo.getProducers()).isEmpty();
    assertThat(environmentInfo.getConsumers()).isEmpty();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SNIHostName(javax.net.ssl.SNIHostName) Message(com.rabbitmq.stream.Message) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) TestUtils.streamName(com.rabbitmq.stream.impl.TestUtils.streamName) AuthenticationFailureException(com.rabbitmq.stream.AuthenticationFailureException) AfterAll(org.junit.jupiter.api.AfterAll) StreamException(com.rabbitmq.stream.StreamException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) Duration(java.time.Duration) Map(java.util.Map) Host(com.rabbitmq.stream.Host) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) Collection(java.util.Collection) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) StandardCharsets(java.nio.charset.StandardCharsets) TestInfo(org.junit.jupiter.api.TestInfo) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) StreamCreator(com.rabbitmq.stream.StreamCreator) SslHandler(io.netty.handler.ssl.SslHandler) ProducerBuilder(com.rabbitmq.stream.ProducerBuilder) Constants(com.rabbitmq.stream.Constants) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) IntStream(java.util.stream.IntStream) SSLParameters(javax.net.ssl.SSLParameters) StreamMetadata(com.rabbitmq.stream.impl.Client.StreamMetadata) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) Address(com.rabbitmq.stream.Address) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) EnvironmentInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.EnvironmentInfo) ConnectException(java.net.ConnectException) ChannelCustomizer(com.rabbitmq.stream.ChannelCustomizer) ValueSource(org.junit.jupiter.params.provider.ValueSource) 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) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) DisabledIfTlsNotEnabled(com.rabbitmq.stream.impl.TestUtils.DisabledIfTlsNotEnabled) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) TestUtils.localhostTls(com.rabbitmq.stream.impl.TestUtils.localhostTls) Producer(com.rabbitmq.stream.Producer) Consumer(com.rabbitmq.stream.Consumer) EnvironmentInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.EnvironmentInfo) Environment(com.rabbitmq.stream.Environment) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with Message

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

the class OutboundMappingCallbackTest method publishBatches.

@Test
void publishBatches() throws Exception {
    int subEntryCount = 10;
    int messagesInFrameCount = 100;
    int frameCount = 1000;
    CountDownLatch mappingLatch = new CountDownLatch(frameCount * messagesInFrameCount);
    CountDownLatch confirmLatch = new CountDownLatch(frameCount * messagesInFrameCount);
    Set<Long> mapped = ConcurrentHashMap.newKeySet(frameCount * messagesInFrameCount);
    Client client = cf.get(new Client.ClientParameters().publishConfirmListener((publisherId, publishingId) -> confirmLatch.countDown()));
    client.declarePublisher(b(1), null, stream);
    IntStream.range(0, frameCount).forEach(frameIndex -> {
        List<MessageBatch> batches = new ArrayList<>(messagesInFrameCount);
        IntStream.range(0, messagesInFrameCount).forEach(batchIndex -> {
            List<Message> messages = IntStream.range(0, subEntryCount).mapToObj(messageIndex -> String.valueOf(messageIndex).getBytes()).map(body -> client.messageBuilder().addData(body).build()).collect(Collectors.toList());
            MessageBatch batch = new MessageBatch(Compression.NONE, messages);
            batches.add(batch);
        });
        client.publishBatches(b(1), batches, (publishingId, original) -> {
            assertThat(original).isNotNull().isInstanceOf(MessageBatch.class);
            mapped.add(publishingId);
            mappingLatch.countDown();
        });
    });
    assertThat(mappingLatch.await(10, TimeUnit.SECONDS)).isTrue();
    assertThat(confirmLatch.await(10, TimeUnit.SECONDS)).isTrue();
    assertThat(mapped).hasSize(frameCount * messagesInFrameCount);
}
Also used : IntStream(java.util.stream.IntStream) Message(com.rabbitmq.stream.Message) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Compression(com.rabbitmq.stream.compression.Compression) Collectors(java.util.stream.Collectors) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Message(com.rabbitmq.stream.Message) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 4 with Message

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

the class MqttInteroperabilityTest method publishToMqttTopicConsumeFromStream.

@Test
void publishToMqttTopicConsumeFromStream() throws Exception {
    try (Connection c = new ConnectionFactory().newConnection()) {
        Channel ch = c.createChannel();
        ch.queueBind(stream, "amq.topic", stream);
    }
    byte[] messageBody = UUID.randomUUID().toString().getBytes(StandardCharsets.UTF_8);
    String uri = "tcp://localhost:1883";
    String clientId = "mqtt-test";
    try (IMqttClient c = new MqttClient(uri, clientId, null)) {
        MqttConnectOptions opts = new MqttConnectOptions();
        opts.setUserName("guest");
        opts.setPassword("guest".toCharArray());
        opts.setCleanSession(true);
        c.connect(opts);
        MqttTopic topic = c.getTopic(stream);
        MqttMessage message = new MqttMessage(messageBody);
        message.setQos(0);
        message.setRetained(false);
        MqttDeliveryToken token = topic.publish(message);
        token.waitForCompletion();
        c.disconnect(5000);
    }
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<Message> messageReference = new AtomicReference<>();
    env.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message1) -> {
        messageReference.set(message1);
        latch.countDown();
    }).build();
    assertThat(latchAssert(latch)).completes();
    Message message = messageReference.get();
    assertThat(message.getBodyAsBinary()).isEqualTo(messageBody);
    // see
    // https://github.com/rabbitmq/rabbitmq-mqtt/blob/ebcb6dabf0e2b2f34315bc90530ca7791330df24/src/rabbit_mqtt_processor.erl#L856-L860
    assertThat(message.getMessageAnnotations().get("x-basic-delivery-mode")).isEqualTo(UnsignedByte.valueOf("1"));
    assertThat(message.getApplicationProperties().get("x-mqtt-publish-qos")).isEqualTo(Byte.valueOf("0"));
    assertThat(message.getApplicationProperties().get("x-mqtt-dup")).isEqualTo(Boolean.FALSE);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) Message(com.rabbitmq.stream.Message) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MqttTopic(org.eclipse.paho.client.mqttv3.MqttTopic) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) Connection(com.rabbitmq.client.Connection) UnsignedByte(com.rabbitmq.stream.amqp.UnsignedByte) AtomicReference(java.util.concurrent.atomic.AtomicReference) AfterAll(org.junit.jupiter.api.AfterAll) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) DisabledIfMqttNotEnabled(com.rabbitmq.stream.impl.TestUtils.DisabledIfMqttNotEnabled) MqttDeliveryToken(org.eclipse.paho.client.mqttv3.MqttDeliveryToken) MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) EventLoopGroup(io.netty.channel.EventLoopGroup) IMqttClient(org.eclipse.paho.client.mqttv3.IMqttClient) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Environment(com.rabbitmq.stream.Environment) UUID(java.util.UUID) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) Channel(com.rabbitmq.client.Channel) SECONDS(java.util.concurrent.TimeUnit.SECONDS) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) Message(com.rabbitmq.stream.Message) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IMqttClient(org.eclipse.paho.client.mqttv3.IMqttClient) MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) IMqttClient(org.eclipse.paho.client.mqttv3.IMqttClient) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) MqttTopic(org.eclipse.paho.client.mqttv3.MqttTopic) MqttDeliveryToken(org.eclipse.paho.client.mqttv3.MqttDeliveryToken) Test(org.junit.jupiter.api.Test)

Example 5 with Message

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

the class EncodeDecodeForPerformanceToolBenchmark method decode.

@Benchmark
public void decode() {
    Message message = codec.decode(messageToDecode);
    long latency = timestampProvider.get() - readLong(message.getBodyAsBinary());
}
Also used : Message(com.rabbitmq.stream.Message)

Aggregations

Message (com.rabbitmq.stream.Message)29 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)14 Environment (com.rabbitmq.stream.Environment)13 List (java.util.List)13 Producer (com.rabbitmq.stream.Producer)11 StandardCharsets (java.nio.charset.StandardCharsets)11 CountDownLatch (java.util.concurrent.CountDownLatch)11 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 Test (org.junit.jupiter.api.Test)10 EncodedMessage (com.rabbitmq.stream.Codec.EncodedMessage)9 ArrayList (java.util.ArrayList)9 UUID (java.util.UUID)9 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)9 SECONDS (java.util.concurrent.TimeUnit.SECONDS)9 IntStream (java.util.stream.IntStream)9 Codec (com.rabbitmq.stream.Codec)8 OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)8 Collections (java.util.Collections)8 Consumer (com.rabbitmq.stream.Consumer)7