Java Code Examples for org.springframework.integration.channel.DirectChannel#subscribe()
The following examples show how to use
org.springframework.integration.channel.DirectChannel#subscribe() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SqsInboundChannelAdapterParserTests.java From spring-integration-aws with MIT License | 6 votes |
@Test public void testMessageFlow() throws Exception { setUp("SqsInboundChannelAdapterParserTests.xml", getClass(), "sqsInboundChannelAdapter"); @SuppressWarnings("unchecked") BlockingQueue<String> testQueue = (BlockingQueue<String>) context .getBean("testQueue"); DirectChannel out = (DirectChannel) context.getBean("out"); out.subscribe(new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { assertEquals("Hello, World", message.getPayload()); } }); testQueue .add("{\"payload\": \"Hello, World\", \"payloadClazz\": \"java.lang.String\", \"headers\": {}, \"properties\": {}}"); }
Example 2
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testTopicPatterns() throws Exception { try (AdminClient admin = AdminClient.create( Collections.singletonMap(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, embeddedKafka.getEmbeddedKafka().getBrokersAsString()))) { admin.createTopics(Collections .singletonList(new NewTopic("topicPatterns.1", 1, (short) 1))).all() .get(); Binder binder = getBinder(); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); consumerProperties.getExtension().setDestinationIsPattern(true); DirectChannel moduleInputChannel = createBindableChannel("input", createConsumerBindingProperties(consumerProperties)); final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<String> topic = new AtomicReference<>(); moduleInputChannel.subscribe(m -> { topic.set(m.getHeaders().get(KafkaHeaders.RECEIVED_TOPIC, String.class)); latch.countDown(); }); Binding<MessageChannel> consumerBinding = binder.bindConsumer( "topicPatterns\\..*", "testTopicPatterns", moduleInputChannel, consumerProperties); DefaultKafkaProducerFactory pf = new DefaultKafkaProducerFactory( KafkaTestUtils.producerProps(embeddedKafka.getEmbeddedKafka())); KafkaTemplate template = new KafkaTemplate(pf); template.send("topicPatterns.1", "foo"); assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); assertThat(topic.get()).isEqualTo("topicPatterns.1"); consumerBinding.unbind(); pf.destroy(); } }
Example 3
Source File: RabbitBinderTests.java From spring-cloud-stream-binder-rabbit with Apache License 2.0 | 5 votes |
@Test public void testNonDurablePubSubWithAutoBindDLQ() throws Exception { RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource()); RabbitTestBinder binder = getBinder(); ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties(); consumerProperties.getExtension().setPrefix(TEST_PREFIX); consumerProperties.getExtension().setAutoBindDlq(true); consumerProperties.getExtension().setDurableSubscription(false); consumerProperties.setMaxAttempts(1); // disable retry BindingProperties bindingProperties = createConsumerBindingProperties( consumerProperties); DirectChannel moduleInputChannel = createBindableChannel("input", bindingProperties); moduleInputChannel.setBeanName("nondurabletest"); moduleInputChannel.subscribe(new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { throw new RuntimeException("foo"); } }); Binding<MessageChannel> consumerBinding = binder.bindConsumer("nondurabletest.0", "tgroup", moduleInputChannel, consumerProperties); consumerBinding.unbind(); assertThat(admin.getQueueProperties(TEST_PREFIX + "nondurabletest.0.dlq")) .isNull(); }
Example 4
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("unchecked") public void testConfigurableDlqName() throws Exception { Binder binder = getBinder(); ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties(); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); consumerProperties.setMaxAttempts(3); consumerProperties.setBackOffInitialInterval(100); consumerProperties.setBackOffMaxInterval(150); consumerProperties.getExtension().setEnableDlq(true); consumerProperties.getExtension().setAutoRebalanceEnabled(false); String dlqName = "dlqTest"; consumerProperties.getExtension().setDlqName(dlqName); BindingProperties producerBindingProperties = createProducerBindingProperties( producerProperties); DirectChannel moduleOutputChannel = createBindableChannel("output", producerBindingProperties); DirectChannel moduleInputChannel = createBindableChannel("input", createConsumerBindingProperties(consumerProperties)); FailingInvocationCountingMessageHandler handler = new FailingInvocationCountingMessageHandler(); moduleInputChannel.subscribe(handler); long uniqueBindingId = System.currentTimeMillis(); Binding<MessageChannel> producerBinding = binder.bindProducer( "retryTest." + uniqueBindingId + ".0", moduleOutputChannel, producerProperties); Binding<MessageChannel> consumerBinding = binder.bindConsumer( "retryTest." + uniqueBindingId + ".0", "testGroup", moduleInputChannel, consumerProperties); ExtendedConsumerProperties<KafkaConsumerProperties> dlqConsumerProperties = createConsumerProperties(); dlqConsumerProperties.setMaxAttempts(1); QueueChannel dlqChannel = new QueueChannel(); Binding<MessageChannel> dlqConsumerBinding = binder.bindConsumer(dlqName, null, dlqChannel, dlqConsumerProperties); String testMessagePayload = "test." + UUID.randomUUID().toString(); Message<byte[]> testMessage = MessageBuilder .withPayload(testMessagePayload.getBytes()).build(); moduleOutputChannel.send(testMessage); Message<?> dlqMessage = receive(dlqChannel, 3); assertThat(dlqMessage).isNotNull(); assertThat(dlqMessage.getPayload()).isEqualTo(testMessagePayload.getBytes()); // first attempt fails assertThat(handler.getReceivedMessages().entrySet()).hasSize(1); Message<?> handledMessage = handler.getReceivedMessages().entrySet().iterator() .next().getValue(); assertThat(handledMessage).isNotNull(); assertThat( new String((byte[]) handledMessage.getPayload(), StandardCharsets.UTF_8)) .isEqualTo(testMessagePayload); assertThat(handler.getInvocationCount()) .isEqualTo(consumerProperties.getMaxAttempts()); binderBindUnbindLatency(); dlqConsumerBinding.unbind(); consumerBinding.unbind(); // on the second attempt the message is not redelivered because the DLQ is set QueueChannel successfulInputChannel = new QueueChannel(); consumerBinding = binder.bindConsumer("retryTest." + uniqueBindingId + ".0", "testGroup", successfulInputChannel, consumerProperties); String testMessage2Payload = "test." + UUID.randomUUID().toString(); Message<byte[]> testMessage2 = MessageBuilder .withPayload(testMessage2Payload.getBytes()).build(); moduleOutputChannel.send(testMessage2); Message<?> receivedMessage = receive(successfulInputChannel); assertThat(receivedMessage.getPayload()) .isEqualTo(testMessage2Payload.getBytes()); binderBindUnbindLatency(); consumerBinding.unbind(); producerBinding.unbind(); }
Example 5
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("unchecked") public void testResetOffsets() throws Exception { Binding<?> producerBinding = null; Binding<?> consumerBinding = null; try { String testPayload = "test"; ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties(); DirectChannel moduleOutputChannel = createBindableChannel("output", createProducerBindingProperties(producerProperties)); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); consumerProperties.setConcurrency(2); consumerProperties.setInstanceCount(5); // 10 partitions across 2 threads consumerProperties.getExtension().setResetOffsets(true); DirectChannel moduleInputChannel = createBindableChannel("input", createConsumerBindingProperties(consumerProperties)); String testTopicName = "existing" + System.currentTimeMillis(); KafkaBinderConfigurationProperties configurationProperties = createConfigurationProperties(); configurationProperties.setAutoAddPartitions(true); Binder binder = getBinder(configurationProperties); producerBinding = binder.bindProducer(testTopicName, moduleOutputChannel, producerProperties); consumerBinding = binder.bindConsumer(testTopicName, "testReset", moduleInputChannel, consumerProperties); // Let the consumer actually bind to the producer before sending a msg binderBindUnbindLatency(); IntStream.range(0, 10).forEach(i -> moduleOutputChannel.send(MessageBuilder.withPayload(testPayload) .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN) .setHeader(KafkaHeaders.PARTITION_ID, i) .build())); CountDownLatch latch1 = new CountDownLatch(10); CountDownLatch latch2 = new CountDownLatch(20); AtomicReference<Message<byte[]>> inboundMessageRef = new AtomicReference<>(); AtomicInteger received = new AtomicInteger(); moduleInputChannel.subscribe(message1 -> { try { inboundMessageRef.set((Message<byte[]>) message1); } finally { received.incrementAndGet(); latch1.countDown(); latch2.countDown(); } }); assertThat(latch1.await(10, TimeUnit.SECONDS)).as("Failed to receive messages").isTrue(); consumerBinding.unbind(); consumerBinding = binder.bindConsumer(testTopicName, "testReset", moduleInputChannel, consumerProperties); assertThat(latch2.await(10, TimeUnit.SECONDS)).as("Failed to receive message").isTrue(); binder.bindConsumer(testTopicName + "-x", "testReset", moduleInputChannel, consumerProperties).unbind(); // cause another rebalance assertThat(received.get()).as("Unexpected reset").isEqualTo(20); assertThat(inboundMessageRef.get()).isNotNull(); assertThat(inboundMessageRef.get().getPayload()).isEqualTo("test".getBytes()); assertThat(inboundMessageRef.get().getHeaders()).containsEntry("contentType", MimeTypeUtils.TEXT_PLAIN); } finally { if (producerBinding != null) { producerBinding.unbind(); } if (consumerBinding != null) { consumerBinding.unbind(); } } }
Example 6
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("unchecked") public void testBuiltinSerialization() throws Exception { Binding<?> producerBinding = null; Binding<?> consumerBinding = null; try { String testPayload = "test"; Message<?> message = MessageBuilder.withPayload(testPayload) .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN) .build(); ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties(); DirectChannel moduleOutputChannel = createBindableChannel("output", createProducerBindingProperties(producerProperties)); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); consumerProperties.getExtension().setAutoRebalanceEnabled(false); DirectChannel moduleInputChannel = createBindableChannel("input", createConsumerBindingProperties(consumerProperties)); String testTopicName = "existing" + System.currentTimeMillis(); KafkaBinderConfigurationProperties configurationProperties = createConfigurationProperties(); configurationProperties.setAutoAddPartitions(true); Binder binder = getBinder(configurationProperties); producerBinding = binder.bindProducer(testTopicName, moduleOutputChannel, producerProperties); consumerBinding = binder.bindConsumer(testTopicName, "test", moduleInputChannel, consumerProperties); // Let the consumer actually bind to the producer before sending a msg binderBindUnbindLatency(); moduleOutputChannel.send(message); CountDownLatch latch = new CountDownLatch(1); AtomicReference<Message<byte[]>> inboundMessageRef = new AtomicReference<>(); moduleInputChannel.subscribe(message1 -> { try { inboundMessageRef.set((Message<byte[]>) message1); } finally { latch.countDown(); } }); Assert.isTrue(latch.await(5, TimeUnit.SECONDS), "Failed to receive message"); assertThat(inboundMessageRef.get()).isNotNull(); assertThat(inboundMessageRef.get().getPayload()).isEqualTo("test".getBytes()); assertThat(inboundMessageRef.get().getHeaders()).containsEntry("contentType", MimeTypeUtils.TEXT_PLAIN); } finally { if (producerBinding != null) { producerBinding.unbind(); } if (consumerBinding != null) { consumerBinding.unbind(); } } }
Example 7
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("unchecked") public void testAutoCreateTopicsDisabledOnBinderStillWorksAsLongAsBrokerCreatesTopic() throws Exception { KafkaBinderConfigurationProperties configurationProperties = createConfigurationProperties(); configurationProperties.setAutoCreateTopics(false); Binder binder = getBinder(configurationProperties); BindingProperties producerBindingProperties = createProducerBindingProperties( createProducerProperties()); DirectChannel output = createBindableChannel("output", producerBindingProperties); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); DirectChannel input = createBindableChannel("input", createConsumerBindingProperties(consumerProperties)); String testTopicName = "createdByBroker-" + System.currentTimeMillis(); Binding<MessageChannel> producerBinding = binder.bindProducer(testTopicName, output, producerBindingProperties.getProducer()); String testPayload = "foo1-" + UUID.randomUUID().toString(); output.send(new GenericMessage<>(testPayload)); Binding<MessageChannel> consumerBinding = binder.bindConsumer(testTopicName, "test", input, consumerProperties); CountDownLatch latch = new CountDownLatch(1); AtomicReference<Message<byte[]>> inboundMessageRef = new AtomicReference<>(); input.subscribe(message1 -> { try { inboundMessageRef.set((Message<byte[]>) message1); } finally { latch.countDown(); } }); Assert.isTrue(latch.await(5, TimeUnit.SECONDS), "Failed to receive message"); assertThat(inboundMessageRef.get()).isNotNull(); assertThat( new String(inboundMessageRef.get().getPayload(), StandardCharsets.UTF_8)) .isEqualTo(testPayload); producerBinding.unbind(); consumerBinding.unbind(); }
Example 8
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("unchecked") public void testResume() throws Exception { Binding<MessageChannel> producerBinding = null; Binding<MessageChannel> consumerBinding = null; try { KafkaBinderConfigurationProperties configurationProperties = createConfigurationProperties(); Binder binder = getBinder(configurationProperties); BindingProperties producerBindingProperties = createProducerBindingProperties( createProducerProperties()); DirectChannel output = createBindableChannel("output", producerBindingProperties); DirectChannel input1 = createBindableChannel("input", createConsumerBindingProperties(createConsumerProperties())); String testTopicName = UUID.randomUUID().toString(); producerBinding = binder.bindProducer(testTopicName, output, producerBindingProperties.getProducer()); ExtendedConsumerProperties<KafkaConsumerProperties> firstConsumerProperties = createConsumerProperties(); consumerBinding = binder.bindConsumer(testTopicName, "startOffsets", input1, firstConsumerProperties); CountDownLatch latch = new CountDownLatch(1); AtomicReference<Message<byte[]>> inboundMessageRef1 = new AtomicReference<>(); MessageHandler messageHandler = message1 -> { try { inboundMessageRef1.set((Message<byte[]>) message1); } finally { latch.countDown(); } }; input1.subscribe(messageHandler); String testPayload1 = "foo1-" + UUID.randomUUID().toString(); output.send(new GenericMessage<>(testPayload1)); Assert.isTrue(latch.await(15, TimeUnit.SECONDS), "Failed to receive message"); assertThat(inboundMessageRef1.get()).isNotNull(); assertThat(inboundMessageRef1.get().getPayload()).isNotNull(); input1.unsubscribe(messageHandler); CountDownLatch latch1 = new CountDownLatch(1); AtomicReference<Message<byte[]>> inboundMessageRef2 = new AtomicReference<>(); MessageHandler messageHandler1 = message1 -> { try { inboundMessageRef2.set((Message<byte[]>) message1); } finally { latch1.countDown(); } }; input1.subscribe(messageHandler1); String testPayload2 = "foo2-" + UUID.randomUUID().toString(); output.send(new GenericMessage<>(testPayload2.getBytes())); Assert.isTrue(latch1.await(15, TimeUnit.SECONDS), "Failed to receive message"); assertThat(inboundMessageRef2.get()).isNotNull(); assertThat(inboundMessageRef2.get().getPayload()).isNotNull(); consumerBinding.unbind(); Thread.sleep(2000); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); consumerBinding = binder.bindConsumer(testTopicName, "startOffsets", input1, consumerProperties); input1.unsubscribe(messageHandler1); CountDownLatch latch2 = new CountDownLatch(1); AtomicReference<Message<byte[]>> inboundMessageRef3 = new AtomicReference<>(); MessageHandler messageHandler2 = message1 -> { try { inboundMessageRef3.set((Message<byte[]>) message1); } finally { latch2.countDown(); } }; input1.subscribe(messageHandler2); String testPayload3 = "foo3-" + UUID.randomUUID().toString(); output.send(new GenericMessage<>(testPayload3.getBytes())); Assert.isTrue(latch2.await(15, TimeUnit.SECONDS), "Failed to receive message"); assertThat(inboundMessageRef3.get()).isNotNull(); assertThat(new String(inboundMessageRef3.get().getPayload(), StandardCharsets.UTF_8)).isEqualTo(testPayload3); } finally { if (consumerBinding != null) { consumerBinding.unbind(); } if (producerBinding != null) { producerBinding.unbind(); } } }
Example 9
Source File: AbstractBinderTests.java From spring-cloud-stream with Apache License 2.0 | 4 votes |
@SuppressWarnings("rawtypes") @Test public void testSendPojoReceivePojoWithStreamListenerDefaultContentType() throws Exception { StreamListenerMessageHandler handler = this.buildStreamListener( AbstractBinderTests.class, "echoStation", Station.class); Binder binder = getBinder(); BindingProperties producerBindingProperties = createProducerBindingProperties( createProducerProperties()); DirectChannel moduleOutputChannel = createBindableChannel("output", producerBindingProperties); BindingProperties consumerBindingProperties = createConsumerBindingProperties( createConsumerProperties()); DirectChannel moduleInputChannel = createBindableChannel("input", consumerBindingProperties); Binding<MessageChannel> producerBinding = binder.bindProducer( String.format("bad%s0a", getDestinationNameDelimiter()), moduleOutputChannel, producerBindingProperties.getProducer()); Binding<MessageChannel> consumerBinding = binder.bindConsumer( String.format("bad%s0a", getDestinationNameDelimiter()), "test-1", moduleInputChannel, consumerBindingProperties.getConsumer()); Station station = new Station(); Message<?> message = MessageBuilder.withPayload(station).build(); moduleInputChannel.subscribe(handler); moduleOutputChannel.send(message); QueueChannel replyChannel = (QueueChannel) handler.getOutputChannel(); Message<?> replyMessage = replyChannel.receive(5000); assertThat(replyMessage.getPayload() instanceof Station).isTrue(); producerBinding.unbind(); consumerBinding.unbind(); }
Example 10
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void testSendAndReceiveWithRawMode() throws Exception { Binder binder = getBinder(); ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties(); producerProperties.setHeaderMode(HeaderMode.none); DirectChannel moduleOutputChannel = createBindableChannel("output", createProducerBindingProperties(producerProperties)); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); consumerProperties.setHeaderMode(HeaderMode.none); DirectChannel moduleInputChannel = createBindableChannel("input", createConsumerBindingProperties(consumerProperties)); Binding<MessageChannel> producerBinding = binder.bindProducer("raw.0", moduleOutputChannel, producerProperties); Binding<MessageChannel> consumerBinding = binder.bindConsumer("raw.0", "test", moduleInputChannel, consumerProperties); Message<?> message = org.springframework.integration.support.MessageBuilder .withPayload("testSendAndReceiveWithRawMode".getBytes()).build(); // Let the consumer actually bind to the producer before sending a msg binderBindUnbindLatency(); moduleOutputChannel.send(message); CountDownLatch latch = new CountDownLatch(1); AtomicReference<Message<byte[]>> inboundMessageRef = new AtomicReference<>(); moduleInputChannel.subscribe(message1 -> { try { inboundMessageRef.set((Message<byte[]>) message1); } finally { latch.countDown(); } }); Assert.isTrue(latch.await(5, TimeUnit.SECONDS), "Failed to receive message"); assertThat(inboundMessageRef.get()).isNotNull(); assertThat( new String(inboundMessageRef.get().getPayload(), StandardCharsets.UTF_8)) .isEqualTo("testSendAndReceiveWithRawMode"); producerBinding.unbind(); consumerBinding.unbind(); }
Example 11
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("unchecked") public void testEarliest() throws Exception { Binding<MessageChannel> producerBinding = null; Binding<MessageChannel> consumerBinding = null; try { Binder binder = getBinder(); BindingProperties producerBindingProperties = createProducerBindingProperties( createProducerProperties()); DirectChannel output = createBindableChannel("output", producerBindingProperties); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); consumerProperties.getExtension().setAutoRebalanceEnabled(false); consumerProperties.getExtension() .setStartOffset(KafkaConsumerProperties.StartOffset.earliest); consumerProperties.getExtension().setAutoRebalanceEnabled(false); DirectChannel input1 = createBindableChannel("input", createConsumerBindingProperties(consumerProperties)); String testTopicName = UUID.randomUUID().toString(); producerBinding = binder.bindProducer(testTopicName, output, producerBindingProperties.getProducer()); String testPayload1 = "foo-" + UUID.randomUUID().toString(); output.send(new GenericMessage<>(testPayload1.getBytes())); consumerBinding = binder.bindConsumer(testTopicName, "startOffsets", input1, consumerProperties); CountDownLatch latch = new CountDownLatch(1); AtomicReference<Message<byte[]>> inboundMessageRef1 = new AtomicReference<>(); MessageHandler messageHandler = message1 -> { try { inboundMessageRef1.set((Message<byte[]>) message1); } finally { latch.countDown(); } }; input1.subscribe(messageHandler); Assert.isTrue(latch.await(5, TimeUnit.SECONDS), "Failed to receive message"); assertThat(inboundMessageRef1.get()).isNotNull(); String testPayload2 = "foo-" + UUID.randomUUID().toString(); input1.unsubscribe(messageHandler); output.send(new GenericMessage<>(testPayload2.getBytes())); CountDownLatch latch1 = new CountDownLatch(1); AtomicReference<Message<byte[]>> inboundMessageRef2 = new AtomicReference<>(); input1.subscribe(message1 -> { try { inboundMessageRef2.set((Message<byte[]>) message1); } finally { latch1.countDown(); } }); Assert.isTrue(latch1.await(5, TimeUnit.SECONDS), "Failed to receive message"); assertThat(inboundMessageRef2.get()).isNotNull(); assertThat(new String(inboundMessageRef2.get().getPayload(), StandardCharsets.UTF_8)).isEqualTo(testPayload2); Thread.sleep(2000); producerBinding.unbind(); consumerBinding.unbind(); } finally { if (consumerBinding != null) { consumerBinding.unbind(); } if (producerBinding != null) { producerBinding.unbind(); } } }
Example 12
Source File: AbstractBinderTests.java From spring-cloud-stream with Apache License 2.0 | 4 votes |
@SuppressWarnings("rawtypes") @Test public void testSendPojoReceivePojoWithStreamListener() throws Exception { StreamListenerMessageHandler handler = this.buildStreamListener( AbstractBinderTests.class, "echoStation", Station.class); Binder binder = getBinder(); BindingProperties producerBindingProperties = createProducerBindingProperties( createProducerProperties()); DirectChannel moduleOutputChannel = createBindableChannel("output", producerBindingProperties); BindingProperties consumerBindingProperties = createConsumerBindingProperties( createConsumerProperties()); DirectChannel moduleInputChannel = createBindableChannel("input", consumerBindingProperties); Binding<MessageChannel> producerBinding = binder.bindProducer( String.format("bad%s0f", getDestinationNameDelimiter()), moduleOutputChannel, producerBindingProperties.getProducer()); Binding<MessageChannel> consumerBinding = binder.bindConsumer( String.format("bad%s0f", getDestinationNameDelimiter()), "test-6", moduleInputChannel, consumerBindingProperties.getConsumer()); Readings r1 = new Readings(); r1.setCustomerid("123"); r1.setStationid("XYZ"); Readings r2 = new Readings(); r2.setCustomerid("546"); r2.setStationid("ABC"); Station station = new Station(); station.setReadings(Arrays.asList(r1, r2)); Message<?> message = MessageBuilder.withPayload(station) .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON) .build(); moduleInputChannel.subscribe(handler); moduleOutputChannel.send(message); QueueChannel channel = (QueueChannel) handler.getOutputChannel(); Message<Station> reply = (Message<Station>) channel.receive(5000); assertThat(reply).isNotNull(); assertThat(reply.getPayload() instanceof Station).isTrue(); producerBinding.unbind(); consumerBinding.unbind(); }
Example 13
Source File: AbstractBinderTests.java From spring-cloud-stream with Apache License 2.0 | 4 votes |
@SuppressWarnings("rawtypes") @Test public void testSendAndReceive() throws Exception { Binder binder = getBinder(); BindingProperties outputBindingProperties = createProducerBindingProperties( createProducerProperties()); DirectChannel moduleOutputChannel = createBindableChannel("output", outputBindingProperties); BindingProperties inputBindingProperties = createConsumerBindingProperties( createConsumerProperties()); DirectChannel moduleInputChannel = createBindableChannel("input", inputBindingProperties); Binding<MessageChannel> producerBinding = binder.bindProducer( String.format("foo%s0", getDestinationNameDelimiter()), moduleOutputChannel, outputBindingProperties.getProducer()); Binding<MessageChannel> consumerBinding = binder.bindConsumer( String.format("foo%s0", getDestinationNameDelimiter()), "testSendAndReceive", moduleInputChannel, inputBindingProperties.getConsumer()); Message<?> message = MessageBuilder.withPayload("foo") .setHeader(MessageHeaders.CONTENT_TYPE, "text/plain").build(); // Let the consumer actually bind to the producer before sending a msg binderBindUnbindLatency(); 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().getPayload()).isEqualTo("foo".getBytes()); assertThat(inboundMessageRef.get().getHeaders().get(MessageHeaders.CONTENT_TYPE) .toString()).isEqualTo("text/plain"); producerBinding.unbind(); consumerBinding.unbind(); }
Example 14
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("unchecked") public void testAutoCommitOnErrorWhenManualAcknowledgement() throws Exception { Binder binder = getBinder(); ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties(); BindingProperties producerBindingProperties = createProducerBindingProperties( producerProperties); DirectChannel moduleOutputChannel = createBindableChannel("output", producerBindingProperties); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); consumerProperties.setMaxAttempts(3); consumerProperties.setBackOffInitialInterval(100); consumerProperties.setBackOffMaxInterval(150); //When auto commit is disabled, then the record is committed after publishing to DLQ using the manual acknowledgement. // (if DLQ is enabled, which is, in this case). consumerProperties.getExtension().setAutoCommitOffset(false); consumerProperties.getExtension().setEnableDlq(true); DirectChannel moduleInputChannel = createBindableChannel("input", createConsumerBindingProperties(consumerProperties)); FailingInvocationCountingMessageHandler handler = new FailingInvocationCountingMessageHandler(); moduleInputChannel.subscribe(handler); long uniqueBindingId = System.currentTimeMillis(); Binding<MessageChannel> producerBinding = binder.bindProducer( "retryTest." + uniqueBindingId + ".0", moduleOutputChannel, producerProperties); Binding<MessageChannel> consumerBinding = binder.bindConsumer( "retryTest." + uniqueBindingId + ".0", "testGroup", moduleInputChannel, consumerProperties); ExtendedConsumerProperties<KafkaConsumerProperties> dlqConsumerProperties = createConsumerProperties(); dlqConsumerProperties.setMaxAttempts(1); QueueChannel dlqChannel = new QueueChannel(); Binding<MessageChannel> dlqConsumerBinding = binder.bindConsumer( "error.retryTest." + uniqueBindingId + ".0.testGroup", null, dlqChannel, dlqConsumerProperties); String testMessagePayload = "test." + UUID.randomUUID().toString(); Message<byte[]> testMessage = MessageBuilder .withPayload(testMessagePayload.getBytes()).build(); moduleOutputChannel.send(testMessage); Message<?> dlqMessage = receive(dlqChannel, 3); assertThat(dlqMessage).isNotNull(); assertThat(dlqMessage.getPayload()).isEqualTo(testMessagePayload.getBytes()); // first attempt fails assertThat(handler.getReceivedMessages().entrySet()).hasSize(1); Message<?> handledMessage = handler.getReceivedMessages().entrySet().iterator() .next().getValue(); assertThat(handledMessage).isNotNull(); assertThat( new String((byte[]) handledMessage.getPayload(), StandardCharsets.UTF_8)) .isEqualTo(testMessagePayload); assertThat(handler.getInvocationCount()) .isEqualTo(consumerProperties.getMaxAttempts()); binderBindUnbindLatency(); dlqConsumerBinding.unbind(); consumerBinding.unbind(); // on the second attempt the message is not redelivered because the DLQ is set and the record in error is already committed. QueueChannel successfulInputChannel = new QueueChannel(); consumerBinding = binder.bindConsumer("retryTest." + uniqueBindingId + ".0", "testGroup", successfulInputChannel, consumerProperties); String testMessage2Payload = "test1." + UUID.randomUUID().toString(); Message<byte[]> testMessage2 = MessageBuilder .withPayload(testMessage2Payload.getBytes()).build(); moduleOutputChannel.send(testMessage2); Message<?> receivedMessage = receive(successfulInputChannel); assertThat(receivedMessage.getPayload()) .isEqualTo(testMessage2Payload.getBytes()); binderBindUnbindLatency(); consumerBinding.unbind(); producerBinding.unbind(); }
Example 15
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("unchecked") public void testDefaultAutoCommitOnErrorWithDlq() throws Exception { Binder binder = getBinder(); ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties(); BindingProperties producerBindingProperties = createProducerBindingProperties( producerProperties); DirectChannel moduleOutputChannel = createBindableChannel("output", producerBindingProperties); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); consumerProperties.setMaxAttempts(3); consumerProperties.setBackOffInitialInterval(100); consumerProperties.setBackOffMaxInterval(150); consumerProperties.getExtension().setEnableDlq(true); DirectChannel moduleInputChannel = createBindableChannel("input", createConsumerBindingProperties(consumerProperties)); FailingInvocationCountingMessageHandler handler = new FailingInvocationCountingMessageHandler(); moduleInputChannel.subscribe(handler); long uniqueBindingId = System.currentTimeMillis(); Binding<MessageChannel> producerBinding = binder.bindProducer( "retryTest." + uniqueBindingId + ".0", moduleOutputChannel, producerProperties); Binding<MessageChannel> consumerBinding = binder.bindConsumer( "retryTest." + uniqueBindingId + ".0", "testGroup", moduleInputChannel, consumerProperties); ExtendedConsumerProperties<KafkaConsumerProperties> dlqConsumerProperties = createConsumerProperties(); dlqConsumerProperties.setMaxAttempts(1); QueueChannel dlqChannel = new QueueChannel(); Binding<MessageChannel> dlqConsumerBinding = binder.bindConsumer( "error.retryTest." + uniqueBindingId + ".0.testGroup", null, dlqChannel, dlqConsumerProperties); String testMessagePayload = "test." + UUID.randomUUID().toString(); Message<byte[]> testMessage = MessageBuilder .withPayload(testMessagePayload.getBytes()).build(); moduleOutputChannel.send(testMessage); Message<?> dlqMessage = receive(dlqChannel, 3); assertThat(dlqMessage).isNotNull(); assertThat(dlqMessage.getPayload()).isEqualTo(testMessagePayload.getBytes()); // first attempt fails assertThat(handler.getReceivedMessages().entrySet()).hasSize(1); Message<?> handledMessage = handler.getReceivedMessages().entrySet().iterator() .next().getValue(); assertThat(handledMessage).isNotNull(); assertThat( new String((byte[]) handledMessage.getPayload(), StandardCharsets.UTF_8)) .isEqualTo(testMessagePayload); assertThat(handler.getInvocationCount()) .isEqualTo(consumerProperties.getMaxAttempts()); binderBindUnbindLatency(); dlqConsumerBinding.unbind(); consumerBinding.unbind(); // on the second attempt the message is not redelivered because the DLQ is set QueueChannel successfulInputChannel = new QueueChannel(); consumerBinding = binder.bindConsumer("retryTest." + uniqueBindingId + ".0", "testGroup", successfulInputChannel, consumerProperties); String testMessage2Payload = "test." + UUID.randomUUID().toString(); Message<byte[]> testMessage2 = MessageBuilder .withPayload(testMessage2Payload.getBytes()).build(); moduleOutputChannel.send(testMessage2); Message<?> receivedMessage = receive(successfulInputChannel); assertThat(receivedMessage.getPayload()) .isEqualTo(testMessage2Payload.getBytes()); binderBindUnbindLatency(); consumerBinding.unbind(); producerBinding.unbind(); }
Example 16
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("unchecked") public void testDlqWithNativeDecodingOnConsumerButMissingSerializerOnDlqProducer() throws Exception { Binder binder = getBinder(); ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties(); // Native serialization for producer producerProperties.setUseNativeEncoding(true); Map<String, String> producerConfig = new HashMap<>(); producerConfig.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); producerProperties.getExtension().setConfiguration(producerConfig); BindingProperties outputBindingProperties = createProducerBindingProperties( producerProperties); DirectChannel moduleOutputChannel = createBindableChannel("output", outputBindingProperties); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); // Native Deserialization for consumer consumerProperties.setUseNativeDecoding(true); Map<String, String> consumerConfig = new HashMap<>(); consumerConfig.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); // No Dlq producer properties set on the consumer with a native serializer. // This should cause an error for DLQ sending. consumerProperties.getExtension().setConfiguration(consumerConfig); consumerProperties.getExtension().setEnableDlq(true); DirectChannel moduleInputChannel = createBindableChannel("input", createConsumerBindingProperties(consumerProperties)); Binding<MessageChannel> producerBinding = binder.bindProducer("foo.bar", moduleOutputChannel, outputBindingProperties.getProducer()); Binding<MessageChannel> consumerBinding = binder.bindConsumer("foo.bar", "testDlqWithNativeEncoding-2", moduleInputChannel, consumerProperties); // Let the consumer actually bind to the producer before sending a msg binderBindUnbindLatency(); FailingInvocationCountingMessageHandler handler = new FailingInvocationCountingMessageHandler(); moduleInputChannel.subscribe(handler); // Consumer for the DLQ destination QueueChannel dlqChannel = new QueueChannel(); ExtendedConsumerProperties<KafkaConsumerProperties> dlqConsumerProperties = createConsumerProperties(); dlqConsumerProperties.setMaxAttempts(1); Binding<MessageChannel> dlqConsumerBinding = binder.bindConsumer( "error.foo.bar." + "testDlqWithNativeEncoding-2", null, dlqChannel, dlqConsumerProperties); binderBindUnbindLatency(); Message<?> message = org.springframework.integration.support.MessageBuilder .withPayload("foo").build(); moduleOutputChannel.send(message); Message<?> receivedMessage = dlqChannel.receive(5000); // Ensure that we didn't receive anything on DLQ because of serializer config // missing on dlq producer while native Decoding is enabled. assertThat(receivedMessage).isNull(); binderBindUnbindLatency(); dlqConsumerBinding.unbind(); producerBinding.unbind(); consumerBinding.unbind(); }
Example 17
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void testSendAndReceiveBatch() throws Exception { Binder binder = getBinder(); BindingProperties outputBindingProperties = createProducerBindingProperties( createProducerProperties()); DirectChannel moduleOutputChannel = createBindableChannel("output", outputBindingProperties); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); consumerProperties.setBatchMode(true); consumerProperties.getExtension().getConfiguration().put("fetch.min.bytes", "1000"); consumerProperties.getExtension().getConfiguration().put("fetch.max.wait.ms", "5000"); consumerProperties.getExtension().getConfiguration().put("max.poll.records", "2"); DirectChannel moduleInputChannel = createBindableChannel("input", createConsumerBindingProperties(consumerProperties)); Binding<MessageChannel> producerBinding = binder.bindProducer("c.batching", moduleOutputChannel, outputBindingProperties.getProducer()); Binding<MessageChannel> consumerBinding = binder.bindConsumer("c.batching", "testSendAndReceiveBatch", moduleInputChannel, consumerProperties); Message<?> message = org.springframework.integration.support.MessageBuilder .withPayload("foo".getBytes(StandardCharsets.UTF_8)) .setHeader(KafkaHeaders.PARTITION_ID, 0) .build(); // Let the consumer actually bind to the producer before sending a msg binderBindUnbindLatency(); moduleOutputChannel.send(message); message = MessageBuilder .withPayload("bar".getBytes(StandardCharsets.UTF_8)) .setHeader(KafkaHeaders.PARTITION_ID, 0) .build(); moduleOutputChannel.send(message); CountDownLatch latch = new CountDownLatch(1); AtomicReference<Message<List<byte[]>>> inboundMessageRef = new AtomicReference<>(); moduleInputChannel.subscribe(message1 -> { try { inboundMessageRef.compareAndSet(null, (Message<List<byte[]>>) message1); } finally { latch.countDown(); } }); Assert.isTrue(latch.await(5, TimeUnit.SECONDS), "Failed to receive message"); assertThat(inboundMessageRef.get()).isNotNull(); List<byte[]> payload = inboundMessageRef.get().getPayload(); assertThat(payload.get(0)).isEqualTo("foo".getBytes()); if (payload.size() > 1) { // it's a race as to whether we'll get them both or just one. assertThat(payload.get(1)).isEqualTo("bar".getBytes()); } producerBinding.unbind(); consumerBinding.unbind(); }
Example 18
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@Test @Override @SuppressWarnings("unchecked") public void testSendAndReceive() throws Exception { Binder binder = getBinder(); BindingProperties outputBindingProperties = createProducerBindingProperties( createProducerProperties()); DirectChannel moduleOutputChannel = createBindableChannel("output", outputBindingProperties); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); DirectChannel moduleInputChannel = createBindableChannel("input", createConsumerBindingProperties(consumerProperties)); Binding<MessageChannel> producerBinding = binder.bindProducer("foo.bar", moduleOutputChannel, outputBindingProperties.getProducer()); Binding<MessageChannel> consumerBinding = binder.bindConsumer("foo.bar", "testSendAndReceive", moduleInputChannel, consumerProperties); Message<?> message = org.springframework.integration.support.MessageBuilder .withPayload("foo".getBytes(StandardCharsets.UTF_8)) .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_OCTET_STREAM) .build(); // Let the consumer actually bind to the producer before sending a msg binderBindUnbindLatency(); moduleOutputChannel.send(message); CountDownLatch latch = new CountDownLatch(1); AtomicReference<Message<byte[]>> inboundMessageRef = new AtomicReference<>(); moduleInputChannel.subscribe(message1 -> { try { inboundMessageRef.set((Message<byte[]>) message1); } finally { latch.countDown(); } }); Assert.isTrue(latch.await(5, TimeUnit.SECONDS), "Failed to receive message"); assertThat(inboundMessageRef.get()).isNotNull(); assertThat( new String(inboundMessageRef.get().getPayload(), StandardCharsets.UTF_8)) .isEqualTo("foo"); assertThat(inboundMessageRef.get().getHeaders().get(MessageHeaders.CONTENT_TYPE)) .isEqualTo(MimeTypeUtils.APPLICATION_OCTET_STREAM); Map<String, TopicInformation> topicsInUse = ((KafkaTestBinder) binder) .getCoreBinder().getTopicsInUse(); assertThat(topicsInUse.keySet()).contains("foo.bar"); TopicInformation topic = topicsInUse.get("foo.bar"); assertThat(topic.isConsumerTopic()).isTrue(); assertThat(topic.getConsumerGroup()).isEqualTo("testSendAndReceive"); producerBinding.unbind(); consumerBinding.unbind(); }
Example 19
Source File: AbstractBinderTests.java From spring-cloud-stream with Apache License 2.0 | 4 votes |
@SuppressWarnings("rawtypes") @Test public void testSendJsonReceiveJsonWithStreamListener() throws Exception { StreamListenerMessageHandler handler = this.buildStreamListener( AbstractBinderTests.class, "echoStationString", String.class); Binder binder = getBinder(); BindingProperties producerBindingProperties = createProducerBindingProperties( createProducerProperties()); DirectChannel moduleOutputChannel = createBindableChannel("output", producerBindingProperties); BindingProperties consumerBindingProperties = createConsumerBindingProperties( createConsumerProperties()); DirectChannel moduleInputChannel = createBindableChannel("input", consumerBindingProperties); Binding<MessageChannel> producerBinding = binder.bindProducer( String.format("bad%s0e", getDestinationNameDelimiter()), moduleOutputChannel, producerBindingProperties.getProducer()); Binding<MessageChannel> consumerBinding = binder.bindConsumer( String.format("bad%s0e", getDestinationNameDelimiter()), "test-5", moduleInputChannel, consumerBindingProperties.getConsumer()); String value = "{\"readings\":[{\"stationid\":\"fgh\"," + "\"customerid\":\"12345\",\"timestamp\":null}," + "{\"stationid\":\"hjk\",\"customerid\":\"222\",\"timestamp\":null}]}"; Message<?> message = MessageBuilder.withPayload(value) .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON) .build(); moduleInputChannel.subscribe(handler); moduleOutputChannel.send(message); QueueChannel channel = (QueueChannel) handler.getOutputChannel(); Message<String> reply = (Message<String>) channel.receive(5000); assertThat(reply).isNotNull(); assertThat(reply.getPayload() instanceof String).isTrue(); producerBinding.unbind(); consumerBinding.unbind(); }
Example 20
Source File: AbstractBinderTests.java From spring-cloud-stream with Apache License 2.0 | 4 votes |
@SuppressWarnings("rawtypes") @Test public void testSendJsonReceivePojoWithStreamListener() throws Exception { StreamListenerMessageHandler handler = this.buildStreamListener( AbstractBinderTests.class, "echoStation", Station.class); Binder binder = getBinder(); BindingProperties producerBindingProperties = createProducerBindingProperties( createProducerProperties()); DirectChannel moduleOutputChannel = createBindableChannel("output", producerBindingProperties); BindingProperties consumerBindingProperties = createConsumerBindingProperties( createConsumerProperties()); DirectChannel moduleInputChannel = createBindableChannel("input", consumerBindingProperties); Binding<MessageChannel> producerBinding = binder.bindProducer( String.format("bad%s0d", getDestinationNameDelimiter()), moduleOutputChannel, producerBindingProperties.getProducer()); Binding<MessageChannel> consumerBinding = binder.bindConsumer( String.format("bad%s0d", getDestinationNameDelimiter()), "test-4", moduleInputChannel, consumerBindingProperties.getConsumer()); String value = "{\"readings\":[{\"stationid\":\"fgh\"," + "\"customerid\":\"12345\",\"timestamp\":null}," + "{\"stationid\":\"hjk\",\"customerid\":\"222\",\"timestamp\":null}]}"; Message<?> message = MessageBuilder.withPayload(value) .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON) .build(); moduleInputChannel.subscribe(handler); moduleOutputChannel.send(message); QueueChannel channel = (QueueChannel) handler.getOutputChannel(); Message<Station> reply = (Message<Station>) channel.receive(5000); assertThat(reply).isNotNull(); assertThat(reply.getPayload() instanceof Station).isTrue(); producerBinding.unbind(); consumerBinding.unbind(); }