use of org.springframework.integration.channel.DirectChannel in project spring-cloud-stream by spring-cloud.
the class AbstractBinderTests method testSendAndReceiveMultipleTopics.
@Test
@SuppressWarnings("rawtypes")
public void testSendAndReceiveMultipleTopics(TestInfo testInfo) throws Exception {
Binder binder = getBinder();
BindingProperties producerBindingProperties = createProducerBindingProperties(createProducerProperties(testInfo));
DirectChannel moduleOutputChannel1 = createBindableChannel("output1", producerBindingProperties);
DirectChannel moduleOutputChannel2 = createBindableChannel("output2", producerBindingProperties);
QueueChannel moduleInputChannel = new QueueChannel();
Binding<MessageChannel> producerBinding1 = binder.bindProducer(String.format("foo%sxy", getDestinationNameDelimiter()), moduleOutputChannel1, producerBindingProperties.getProducer());
Binding<MessageChannel> producerBinding2 = binder.bindProducer(String.format("foo%syz", getDestinationNameDelimiter()), moduleOutputChannel2, producerBindingProperties.getProducer());
Binding<MessageChannel> consumerBinding1 = binder.bindConsumer(String.format("foo%sxy", getDestinationNameDelimiter()), "testSendAndReceiveMultipleTopics", moduleInputChannel, createConsumerProperties());
Binding<MessageChannel> consumerBinding2 = binder.bindConsumer(String.format("foo%syz", getDestinationNameDelimiter()), "testSendAndReceiveMultipleTopics", moduleInputChannel, createConsumerProperties());
String testPayload1 = "foo" + UUID.randomUUID().toString();
Message<?> message1 = MessageBuilder.withPayload(testPayload1.getBytes()).setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_OCTET_STREAM).build();
String testPayload2 = "foo" + UUID.randomUUID().toString();
Message<?> message2 = MessageBuilder.withPayload(testPayload2.getBytes()).setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_OCTET_STREAM).build();
// Let the consumer actually bind to the producer before sending a msg
binderBindUnbindLatency();
moduleOutputChannel1.send(message1);
moduleOutputChannel2.send(message2);
Message<?>[] messages = new Message[2];
messages[0] = receive(moduleInputChannel);
messages[1] = receive(moduleInputChannel);
assertThat(messages[0]).isNotNull();
assertThat(messages[1]).isNotNull();
assertThat(messages).extracting("payload").containsExactlyInAnyOrder(testPayload1.getBytes(), testPayload2.getBytes());
producerBinding1.unbind();
producerBinding2.unbind();
consumerBinding1.unbind();
consumerBinding2.unbind();
}
use of org.springframework.integration.channel.DirectChannel in project spring-cloud-stream by spring-cloud.
the class AbstractBinderTests method testSendAndReceiveNoOriginalContentType.
@Test
@SuppressWarnings("rawtypes")
public void testSendAndReceiveNoOriginalContentType(TestInfo testInfo) throws Exception {
Binder binder = getBinder();
BindingProperties producerBindingProperties = createProducerBindingProperties(createProducerProperties(testInfo));
DirectChannel moduleOutputChannel = createBindableChannel("output", producerBindingProperties);
BindingProperties inputBindingProperties = createConsumerBindingProperties(createConsumerProperties());
DirectChannel moduleInputChannel = createBindableChannel("input", inputBindingProperties);
Binding<MessageChannel> producerBinding = binder.bindProducer(String.format("bar%s0", getDestinationNameDelimiter()), moduleOutputChannel, producerBindingProperties.getProducer());
Binding<MessageChannel> consumerBinding = binder.bindConsumer(String.format("bar%s0", getDestinationNameDelimiter()), "testSendAndReceiveNoOriginalContentType", moduleInputChannel, createConsumerProperties());
binderBindUnbindLatency();
Message<?> message = MessageBuilder.withPayload("foo").setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN).build();
moduleOutputChannel.send(message);
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<Message<byte[]>> inboundMessageRef = new AtomicReference<Message<byte[]>>();
moduleInputChannel.subscribe(message1 -> {
try {
inboundMessageRef.set((Message<byte[]>) message1);
} finally {
latch.countDown();
}
});
moduleOutputChannel.send(message);
Assert.isTrue(latch.await(5, TimeUnit.SECONDS), "Failed to receive message");
assertThat(inboundMessageRef.get()).isNotNull();
assertThat(inboundMessageRef.get().getPayload()).isEqualTo("foo".getBytes());
assertThat(inboundMessageRef.get().getHeaders().get(MessageHeaders.CONTENT_TYPE).toString()).isEqualTo(MimeTypeUtils.TEXT_PLAIN_VALUE);
producerBinding.unbind();
consumerBinding.unbind();
}
use of org.springframework.integration.channel.DirectChannel in project spring-cloud-stream by spring-cloud.
the class PartitionCapableBinderTests method testOneRequiredGroup.
@Test
public void testOneRequiredGroup(TestInfo testInfo) throws Exception {
B binder = getBinder();
PP producerProperties = createProducerProperties(testInfo);
DirectChannel output = createBindableChannel("output", createProducerBindingProperties(producerProperties));
String testDestination = "testDestination" + UUID.randomUUID().toString().replace("-", "");
producerProperties.setRequiredGroups("test1");
Binding<MessageChannel> producerBinding = binder.bindProducer(testDestination, output, producerProperties);
String testPayload = "foo-" + UUID.randomUUID().toString();
output.send(MessageBuilder.withPayload(testPayload).setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN).build());
QueueChannel inbound1 = new QueueChannel();
Binding<MessageChannel> consumerBinding = binder.bindConsumer(testDestination, "test1", inbound1, createConsumerProperties());
Message<?> receivedMessage1 = receive(inbound1);
assertThat(receivedMessage1).isNotNull();
assertThat(new String((byte[]) receivedMessage1.getPayload())).isEqualTo(testPayload);
producerBinding.unbind();
consumerBinding.unbind();
}
use of org.springframework.integration.channel.DirectChannel in project spring-cloud-stream by spring-cloud.
the class PartitionCapableBinderTests method testPartitionedModuleSpEL.
@Test
public void testPartitionedModuleSpEL(TestInfo testInfo) throws Exception {
B binder = getBinder();
CP consumerProperties = createConsumerProperties();
consumerProperties.setConcurrency(2);
consumerProperties.setInstanceIndex(0);
consumerProperties.setInstanceCount(3);
consumerProperties.setPartitioned(true);
QueueChannel input0 = new QueueChannel();
input0.setBeanName("test.input0S");
Binding<MessageChannel> input0Binding = binder.bindConsumer(String.format("part%s0", getDestinationNameDelimiter()), "testPartitionedModuleSpEL", input0, consumerProperties);
consumerProperties.setInstanceIndex(1);
QueueChannel input1 = new QueueChannel();
input1.setBeanName("test.input1S");
Binding<MessageChannel> input1Binding = binder.bindConsumer(String.format("part%s0", getDestinationNameDelimiter()), "testPartitionedModuleSpEL", input1, consumerProperties);
consumerProperties.setInstanceIndex(2);
QueueChannel input2 = new QueueChannel();
input2.setBeanName("test.input2S");
Binding<MessageChannel> input2Binding = binder.bindConsumer(String.format("part%s0", getDestinationNameDelimiter()), "testPartitionedModuleSpEL", input2, consumerProperties);
PP producerProperties = createProducerProperties(testInfo);
producerProperties.setPartitionKeyExpression(spelExpressionParser.parseExpression("payload"));
producerProperties.setPartitionSelectorExpression(spelExpressionParser.parseExpression("hashCode()"));
producerProperties.setPartitionCount(3);
DirectChannel output = createBindableChannel("output", createProducerBindingProperties(producerProperties));
output.setBeanName("test.output");
Binding<MessageChannel> outputBinding = binder.bindProducer(String.format("part%s0", getDestinationNameDelimiter()), output, producerProperties);
try {
Object endpoint = extractEndpoint(outputBinding);
checkRkExpressionForPartitionedModuleSpEL(endpoint);
} catch (UnsupportedOperationException ignored) {
}
Message<String> message2 = MessageBuilder.withPayload("2").setHeader(IntegrationMessageHeaderAccessor.CORRELATION_ID, "foo").setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN).setHeader(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER, 42).setHeader(IntegrationMessageHeaderAccessor.SEQUENCE_SIZE, 43).build();
output.send(message2);
output.send(MessageBuilder.withPayload("1").setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN).build());
output.send(MessageBuilder.withPayload("0").setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN).build());
Message<?> receive0 = receive(input0);
assertThat(receive0).isNotNull();
Message<?> receive1 = receive(input1);
assertThat(receive1).isNotNull();
Message<?> receive2 = receive(input2);
assertThat(receive2).isNotNull();
Condition<Message<?>> correlationHeadersForPayload2 = new Condition<Message<?>>() {
@Override
public boolean matches(Message<?> value) {
IntegrationMessageHeaderAccessor accessor = new IntegrationMessageHeaderAccessor(value);
return "foo".equals(accessor.getCorrelationId()) && 42 == accessor.getSequenceNumber() && 43 == accessor.getSequenceSize();
}
};
if (usesExplicitRouting()) {
assertThat(receive0.getPayload()).isEqualTo("0".getBytes());
assertThat(receive1.getPayload()).isEqualTo("1".getBytes());
assertThat(receive2.getPayload()).isEqualTo("2".getBytes());
assertThat(receive2).has(correlationHeadersForPayload2);
} else {
List<Message<?>> receivedMessages = Arrays.asList(receive0, receive1, receive2);
assertThat(receivedMessages).extracting("payload").containsExactlyInAnyOrder("0".getBytes(), "1".getBytes(), "2".getBytes());
Condition<Message<?>> payloadIs2 = new Condition<Message<?>>() {
@Override
public boolean matches(Message<?> value) {
return value.getPayload().equals("2".getBytes());
}
};
assertThat(receivedMessages).filteredOn(payloadIs2).areExactly(1, correlationHeadersForPayload2);
}
input0Binding.unbind();
input1Binding.unbind();
input2Binding.unbind();
outputBinding.unbind();
}
use of org.springframework.integration.channel.DirectChannel in project camel by apache.
the class SpringIntegrationTwoWayConsumerTest method testSendingTwoWayMessage.
@Test
public void testSendingTwoWayMessage() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
MessageChannel requestChannel = getMandatoryBean(MessageChannel.class, "requestChannel");
Map<String, Object> maps = new HashMap<String, Object>();
maps.put(MessageHeaders.REPLY_CHANNEL, "responseChannel");
Message<String> message = new GenericMessage<String>(MESSAGE_BODY, maps);
DirectChannel responseChannel = getMandatoryBean(DirectChannel.class, "responseChannel");
responseChannel.subscribe(new MessageHandler() {
public void handleMessage(Message<?> message) {
latch.countDown();
assertEquals("Get the wrong result", MESSAGE_BODY + " is processed", message.getPayload());
assertEquals("Done", message.getHeaders().get("Status"));
}
});
requestChannel.send(message);
assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Aggregations