Search in sources :

Example 1 with RabbitStreamMessageHandler

use of org.springframework.cloud.stream.binder.rabbit.RabbitStreamMessageHandler in project spring-cloud-stream by spring-cloud.

the class RabbitStreamMessageHandlerTests method convertAndSend.

@Test
void convertAndSend() throws InterruptedException {
    Environment env = Environment.builder().lazyInitialization(true).addressResolver(add -> new Address("localhost", RABBITMQ.getMappedPort(5552))).build();
    try {
        env.deleteStream("stream.stream");
    } catch (Exception e) {
    }
    env.streamCreator().stream("stream.stream").create();
    RabbitStreamTemplate streamTemplate = new RabbitStreamTemplate(env, "stream.stream");
    RabbitStreamMessageHandler handler = new RabbitStreamMessageHandler(streamTemplate);
    handler.setSync(true);
    handler.handleMessage(MessageBuilder.withPayload("foo").setHeader("bar", "baz").build());
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<com.rabbitmq.stream.Message> received = new AtomicReference<>();
    Consumer consumer = env.consumerBuilder().stream("stream.stream").offset(OffsetSpecification.first()).messageHandler((context, msg) -> {
        received.set(msg);
        latch.countDown();
    }).build();
    assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
    assertThat(received.get()).isNotNull();
    assertThat(received.get().getBodyAsBinary()).isEqualTo("foo".getBytes());
    assertThat((String) received.get().getApplicationProperties().get("bar")).isEqualTo("baz");
    consumer.close();
    handler.stop();
}
Also used : RabbitStreamTemplate(org.springframework.rabbit.stream.producer.RabbitStreamTemplate) RabbitStreamMessageHandler(org.springframework.cloud.stream.binder.rabbit.RabbitStreamMessageHandler) Environment(com.rabbitmq.stream.Environment) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Address(com.rabbitmq.stream.Address) Disabled(org.junit.jupiter.api.Disabled) AtomicReference(java.util.concurrent.atomic.AtomicReference) Consumer(com.rabbitmq.stream.Consumer) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) MessageBuilder(org.springframework.integration.support.MessageBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) RabbitTestContainer(org.springframework.cloud.stream.binder.rabbit.RabbitTestContainer) RabbitMQContainer(org.testcontainers.containers.RabbitMQContainer) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) Address(com.rabbitmq.stream.Address) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) RabbitStreamMessageHandler(org.springframework.cloud.stream.binder.rabbit.RabbitStreamMessageHandler) Consumer(com.rabbitmq.stream.Consumer) Environment(com.rabbitmq.stream.Environment) RabbitStreamTemplate(org.springframework.rabbit.stream.producer.RabbitStreamTemplate) Test(org.junit.jupiter.api.Test)

Example 2 with RabbitStreamMessageHandler

use of org.springframework.cloud.stream.binder.rabbit.RabbitStreamMessageHandler in project spring-cloud-stream by spring-cloud.

the class RabbitStreamMessageHandlerTests method sendNative.

@Test
@Disabled
void sendNative() throws InterruptedException {
    Environment env = Environment.builder().lazyInitialization(true).build();
    try {
        env.deleteStream("stream.stream");
    } catch (Exception e) {
    }
    env.streamCreator().stream("stream.stream").create();
    RabbitStreamTemplate streamTemplate = new RabbitStreamTemplate(env, "stream.stream");
    RabbitStreamMessageHandler handler = new RabbitStreamMessageHandler(streamTemplate);
    handler.setSync(true);
    handler.handleMessage(MessageBuilder.withPayload(streamTemplate.messageBuilder().addData("foo".getBytes()).applicationProperties().entry("bar", "baz").messageBuilder().build()).build());
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<com.rabbitmq.stream.Message> received = new AtomicReference<>();
    Consumer consumer = env.consumerBuilder().stream("stream.stream").offset(OffsetSpecification.first()).messageHandler((context, msg) -> {
        received.set(msg);
        latch.countDown();
    }).build();
    assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
    assertThat(received.get()).isNotNull();
    assertThat(received.get().getBodyAsBinary()).isEqualTo("foo".getBytes());
    assertThat((String) received.get().getApplicationProperties().get("bar")).isEqualTo("baz");
    consumer.close();
    handler.stop();
}
Also used : RabbitStreamTemplate(org.springframework.rabbit.stream.producer.RabbitStreamTemplate) RabbitStreamMessageHandler(org.springframework.cloud.stream.binder.rabbit.RabbitStreamMessageHandler) Environment(com.rabbitmq.stream.Environment) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Address(com.rabbitmq.stream.Address) Disabled(org.junit.jupiter.api.Disabled) AtomicReference(java.util.concurrent.atomic.AtomicReference) Consumer(com.rabbitmq.stream.Consumer) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) MessageBuilder(org.springframework.integration.support.MessageBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) RabbitTestContainer(org.springframework.cloud.stream.binder.rabbit.RabbitTestContainer) RabbitMQContainer(org.testcontainers.containers.RabbitMQContainer) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) RabbitStreamMessageHandler(org.springframework.cloud.stream.binder.rabbit.RabbitStreamMessageHandler) Consumer(com.rabbitmq.stream.Consumer) Environment(com.rabbitmq.stream.Environment) AtomicReference(java.util.concurrent.atomic.AtomicReference) RabbitStreamTemplate(org.springframework.rabbit.stream.producer.RabbitStreamTemplate) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

Address (com.rabbitmq.stream.Address)2 Consumer (com.rabbitmq.stream.Consumer)2 Environment (com.rabbitmq.stream.Environment)2 OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Disabled (org.junit.jupiter.api.Disabled)2 Test (org.junit.jupiter.api.Test)2 RabbitStreamMessageHandler (org.springframework.cloud.stream.binder.rabbit.RabbitStreamMessageHandler)2 RabbitTestContainer (org.springframework.cloud.stream.binder.rabbit.RabbitTestContainer)2 MessageBuilder (org.springframework.integration.support.MessageBuilder)2 RabbitStreamTemplate (org.springframework.rabbit.stream.producer.RabbitStreamTemplate)2 RabbitMQContainer (org.testcontainers.containers.RabbitMQContainer)2