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));
});
}
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;
}
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");
}
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();
}
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();
}
Aggregations