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