Search in sources :

Example 1 with RabbitStreamTemplate

use of org.springframework.rabbit.stream.producer.RabbitStreamTemplate in project spring-boot by spring-projects.

the class RabbitStreamConfigurationTests method testRabbitStreamTemplateConfigurationWithCustomMessageConverter.

@Test
void testRabbitStreamTemplateConfigurationWithCustomMessageConverter() {
    this.contextRunner.withUserConfiguration(MessageConvertersConfiguration.class).withPropertyValues("spring.rabbitmq.listener.type:stream", "spring.rabbitmq.stream.name:stream-test").run((context) -> {
        assertThat(context).hasSingleBean(RabbitStreamTemplate.class);
        RabbitStreamTemplate streamTemplate = context.getBean(RabbitStreamTemplate.class);
        assertThat(streamTemplate).hasFieldOrPropertyWithValue("streamName", "stream-test");
        assertThat(streamTemplate).extracting("messageConverter").isSameAs(context.getBean(MessageConverter.class));
    });
}
Also used : RabbitStreamTemplate(org.springframework.rabbit.stream.producer.RabbitStreamTemplate) MessageConverter(org.springframework.amqp.support.converter.MessageConverter) StreamMessageConverter(org.springframework.rabbit.stream.support.converter.StreamMessageConverter) Test(org.junit.jupiter.api.Test)

Example 2 with RabbitStreamTemplate

use of org.springframework.rabbit.stream.producer.RabbitStreamTemplate in project spring-cloud-stream by spring-cloud.

the class StreamUtils method createStreamMessageHandler.

/**
 * Create a {@link RabbitStreamMessageHandler}.
 *
 * @param producerDestination the destination.
 * @param producerProperties the properties.
 * @param errorChannel the error channel
 * @param destination the destination.
 * @param extendedProperties the extended properties.
 * @param abstractApplicationContext the application context.
 * @param headerMapperFunction the header mapper function.
 * @return the handler.
 */
public static MessageHandler createStreamMessageHandler(ProducerDestination producerDestination, ExtendedProducerProperties<RabbitProducerProperties> producerProperties, MessageChannel errorChannel, String destination, RabbitProducerProperties extendedProperties, AbstractApplicationContext applicationContext, Function<RabbitProducerProperties, AmqpHeaderMapper> headerMapperFunction) {
    RabbitStreamTemplate template = new RabbitStreamTemplate(applicationContext.getBean(Environment.class), producerDestination.getName());
    String beanName = extendedProperties.getStreamMessageConverterBeanName();
    if (beanName != null) {
        template.setMessageConverter(applicationContext.getBean(beanName, MessageConverter.class));
    }
    beanName = extendedProperties.getStreamStreamMessageConverterBeanName();
    if (beanName != null) {
        template.setStreamConverter(applicationContext.getBean(beanName, StreamMessageConverter.class));
    }
    RabbitStreamMessageHandler handler = new RabbitStreamMessageHandler(template);
    if (errorChannel != null) {
        handler.setFailureCallback((msg, ex) -> {
            errorChannel.send(new ErrorMessage(new MessageHandlingException(msg, ex)));
        });
    }
    handler.setHeaderMapper(headerMapperFunction.apply(extendedProperties));
    handler.setSync(ProducerType.STREAM_SYNC.equals(producerProperties.getExtension().getProducerType()));
    return handler;
}
Also used : Environment(com.rabbitmq.stream.Environment) RabbitStreamTemplate(org.springframework.rabbit.stream.producer.RabbitStreamTemplate) MessageConverter(org.springframework.amqp.support.converter.MessageConverter) StreamMessageConverter(org.springframework.rabbit.stream.support.converter.StreamMessageConverter) ErrorMessage(org.springframework.messaging.support.ErrorMessage) StreamMessageConverter(org.springframework.rabbit.stream.support.converter.StreamMessageConverter) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 3 with RabbitStreamTemplate

use of org.springframework.rabbit.stream.producer.RabbitStreamTemplate in project spring-amqp by spring-projects.

the class RabbitListenerTests method simple.

@Test
void simple(@Autowired RabbitStreamTemplate template) throws Exception {
    Future<Boolean> future = template.convertAndSend("foo");
    assertThat(future.get(10, TimeUnit.SECONDS)).isTrue();
    future = template.convertAndSend("bar", msg -> msg);
    assertThat(future.get(10, TimeUnit.SECONDS)).isTrue();
    future = template.send(new org.springframework.amqp.core.Message("baz".getBytes(), new StreamMessageProperties()));
    assertThat(future.get(10, TimeUnit.SECONDS)).isTrue();
    future = template.send(template.messageBuilder().addData("qux".getBytes()).build());
    assertThat(future.get(10, TimeUnit.SECONDS)).isTrue();
    future = template.convertAndSend("bar", msg -> null);
    assertThat(future.get(10, TimeUnit.SECONDS)).isFalse();
    assertThat(this.config.latch1.await(10, TimeUnit.SECONDS)).isTrue();
    assertThat(this.config.received).containsExactly("foo", "foo", "bar", "baz", "qux");
    assertThat(this.config.id).isEqualTo("testNative");
}
Also used : EnableRabbit(org.springframework.amqp.rabbit.annotation.EnableRabbit) StreamMessageProperties(org.springframework.rabbit.stream.support.StreamMessageProperties) DirtiesContext(org.springframework.test.annotation.DirtiesContext) QueueBuilder(org.springframework.amqp.core.QueueBuilder) Message(com.rabbitmq.stream.Message) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Autowired(org.springframework.beans.factory.annotation.Autowired) DependsOn(org.springframework.context.annotation.DependsOn) Address(com.rabbitmq.stream.Address) ArrayList(java.util.ArrayList) RetryInterceptorBuilder(org.springframework.amqp.rabbit.config.RetryInterceptorBuilder) SpringJUnitConfig(org.springframework.test.context.junit.jupiter.SpringJUnitConfig) Future(java.util.concurrent.Future) StreamRetryOperationsInterceptorFactoryBean(org.springframework.rabbit.stream.retry.StreamRetryOperationsInterceptorFactoryBean) CachingConnectionFactory(org.springframework.amqp.rabbit.connection.CachingConnectionFactory) Queue(org.springframework.amqp.core.Queue) StreamRabbitListenerContainerFactory(org.springframework.rabbit.stream.config.StreamRabbitListenerContainerFactory) RabbitAdmin(org.springframework.amqp.rabbit.core.RabbitAdmin) RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) RabbitListener(org.springframework.amqp.rabbit.annotation.RabbitListener) RabbitListenerContainerFactory(org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory) RabbitStreamTemplate(org.springframework.rabbit.stream.producer.RabbitStreamTemplate) Environment(com.rabbitmq.stream.Environment) QueueInfo(com.rabbitmq.http.client.domain.QueueInfo) SmartLifecycle(org.springframework.context.SmartLifecycle) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Context(com.rabbitmq.stream.MessageHandler.Context) RetryOperationsInterceptor(org.springframework.retry.interceptor.RetryOperationsInterceptor) Bean(org.springframework.context.annotation.Bean) Client(com.rabbitmq.http.client.Client) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) Message(com.rabbitmq.stream.Message) StreamMessageProperties(org.springframework.rabbit.stream.support.StreamMessageProperties) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.jupiter.api.Test)

Example 4 with RabbitStreamTemplate

use of org.springframework.rabbit.stream.producer.RabbitStreamTemplate 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 5 with RabbitStreamTemplate

use of org.springframework.rabbit.stream.producer.RabbitStreamTemplate 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

RabbitStreamTemplate (org.springframework.rabbit.stream.producer.RabbitStreamTemplate)6 Environment (com.rabbitmq.stream.Environment)4 Test (org.junit.jupiter.api.Test)4 Address (com.rabbitmq.stream.Address)3 OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 TimeUnit (java.util.concurrent.TimeUnit)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Consumer (com.rabbitmq.stream.Consumer)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Disabled (org.junit.jupiter.api.Disabled)2 MessageConverter (org.springframework.amqp.support.converter.MessageConverter)2 RabbitStreamMessageHandler (org.springframework.cloud.stream.binder.rabbit.RabbitStreamMessageHandler)2 RabbitTestContainer (org.springframework.cloud.stream.binder.rabbit.RabbitTestContainer)2 Bean (org.springframework.context.annotation.Bean)2 MessageBuilder (org.springframework.integration.support.MessageBuilder)2 StreamMessageConverter (org.springframework.rabbit.stream.support.converter.StreamMessageConverter)2 RabbitMQContainer (org.testcontainers.containers.RabbitMQContainer)2 Client (com.rabbitmq.http.client.Client)1 QueueInfo (com.rabbitmq.http.client.domain.QueueInfo)1