Java Code Examples for org.springframework.cloud.stream.binder.Binder#bindProducer()
The following examples show how to use
org.springframework.cloud.stream.binder.Binder#bindProducer() .
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: BindingService.java From spring-cloud-stream with Apache License 2.0 | 6 votes |
public <T> Binding<T> doBindProducer(T output, String bindingTarget, Binder<T, ?, ProducerProperties> binder, ProducerProperties producerProperties) { if (this.taskScheduler == null || this.bindingServiceProperties.getBindingRetryInterval() <= 0) { return binder.bindProducer(bindingTarget, output, producerProperties); } else { try { return binder.bindProducer(bindingTarget, output, producerProperties); } catch (RuntimeException e) { LateBinding<T> late = new LateBinding<T>(bindingTarget, e.getCause() == null ? e.toString() : e.getCause().getMessage(), producerProperties, false); rescheduleProducerBinding(output, bindingTarget, binder, producerProperties, late, e); return late; } } }
Example 2
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("unchecked") public void testSendTimeoutExpressionProducerMetadata() throws Exception { Binder binder = getBinder(createConfigurationProperties()); DirectChannel output = new DirectChannel(); String testTopicName = UUID.randomUUID().toString(); ExtendedProducerProperties<KafkaProducerProperties> properties = createProducerProperties(); properties.getExtension().setSync(true); SpelExpressionParser parser = new SpelExpressionParser(); Expression sendTimeoutExpression = parser.parseExpression("5000"); properties.getExtension().setSendTimeoutExpression(sendTimeoutExpression); Binding<MessageChannel> producerBinding = binder.bindProducer(testTopicName, output, properties); DirectFieldAccessor accessor = new DirectFieldAccessor( extractEndpoint(producerBinding)); KafkaProducerMessageHandler wrappedInstance = (KafkaProducerMessageHandler) accessor .getWrappedInstance(); assertThat(new DirectFieldAccessor(wrappedInstance).getPropertyValue("sendTimeoutExpression") .equals(sendTimeoutExpression)); producerBinding.unbind(); }
Example 3
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@Test @SuppressWarnings("unchecked") public void testCustomPartitionCountOverridesPartitioningIfLarger() throws Exception { byte[] testPayload = new byte[2048]; Arrays.fill(testPayload, (byte) 65); KafkaBinderConfigurationProperties binderConfiguration = createConfigurationProperties(); binderConfiguration.setMinPartitionCount(4); Binder binder = getBinder(binderConfiguration); QueueChannel moduleInputChannel = new QueueChannel(); ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties(); producerProperties.setPartitionCount(5); producerProperties.setPartitionKeyExpression( spelExpressionParser.parseExpression("payload")); DirectChannel moduleOutputChannel = createBindableChannel("output", createProducerBindingProperties(producerProperties)); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); long uniqueBindingId = System.currentTimeMillis(); Binding<MessageChannel> producerBinding = binder.bindProducer( "foo" + uniqueBindingId + ".0", moduleOutputChannel, producerProperties); Binding<MessageChannel> consumerBinding = binder.bindConsumer( "foo" + uniqueBindingId + ".0", null, moduleInputChannel, consumerProperties); Message<?> message = org.springframework.integration.support.MessageBuilder .withPayload(testPayload).build(); // Let the consumer actually bind to the producer before sending a msg binderBindUnbindLatency(); moduleOutputChannel.send(message); Message<?> inbound = receive(moduleInputChannel); assertThat(inbound).isNotNull(); assertThat((byte[]) inbound.getPayload()).containsExactly(testPayload); assertThat(partitionSize("foo" + uniqueBindingId + ".0")).isEqualTo(5); producerBinding.unbind(); consumerBinding.unbind(); }
Example 4
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@Test @SuppressWarnings("unchecked") public void testCustomPartitionCountDoesNotOverridePartitioningIfSmaller() throws Exception { byte[] testPayload = new byte[2048]; Arrays.fill(testPayload, (byte) 65); KafkaBinderConfigurationProperties binderConfiguration = createConfigurationProperties(); binderConfiguration.setMinPartitionCount(6); Binder binder = getBinder(binderConfiguration); QueueChannel moduleInputChannel = new QueueChannel(); ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties(); producerProperties.setPartitionCount(5); producerProperties.setPartitionKeyExpression( spelExpressionParser.parseExpression("payload")); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); long uniqueBindingId = System.currentTimeMillis(); DirectChannel moduleOutputChannel = createBindableChannel("output", createProducerBindingProperties(producerProperties)); Binding<MessageChannel> producerBinding = binder.bindProducer( "foo" + uniqueBindingId + ".0", moduleOutputChannel, producerProperties); Binding<MessageChannel> consumerBinding = binder.bindConsumer( "foo" + uniqueBindingId + ".0", null, moduleInputChannel, consumerProperties); Thread.sleep(1000); Message<?> message = org.springframework.integration.support.MessageBuilder .withPayload(testPayload).build(); // Let the consumer actually bind to the producer before sending a msg binderBindUnbindLatency(); moduleOutputChannel.send(message); Message<?> inbound = receive(moduleInputChannel); assertThat(inbound).isNotNull(); assertThat((byte[]) inbound.getPayload()).containsExactly(testPayload); assertThat(partitionSize("foo" + uniqueBindingId + ".0")).isEqualTo(6); producerBinding.unbind(); consumerBinding.unbind(); }
Example 5
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@Test @SuppressWarnings("unchecked") public void testDynamicKeyExpression() throws Exception { Binder binder = getBinder(createConfigurationProperties()); QueueChannel moduleInputChannel = new QueueChannel(); ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties(); producerProperties.getExtension().getConfiguration().put("key.serializer", StringSerializer.class.getName()); producerProperties.getExtension().setMessageKeyExpression( spelExpressionParser.parseExpression("headers.key")); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); String uniqueBindingId = UUID.randomUUID().toString(); DirectChannel moduleOutputChannel = createBindableChannel("output", createProducerBindingProperties(producerProperties)); Binding<MessageChannel> producerBinding = binder.bindProducer( "foo" + uniqueBindingId + ".0", moduleOutputChannel, producerProperties); Binding<MessageChannel> consumerBinding = binder.bindConsumer( "foo" + uniqueBindingId + ".0", null, moduleInputChannel, consumerProperties); Thread.sleep(1000); Message<?> message = MessageBuilder.withPayload("somePayload") .setHeader("key", "myDynamicKey").build(); // Let the consumer actually bind to the producer before sending a msg binderBindUnbindLatency(); moduleOutputChannel.send(message); Message<?> inbound = receive(moduleInputChannel); assertThat(inbound).isNotNull(); String receivedKey = new String(inbound.getHeaders() .get(KafkaHeaders.RECEIVED_MESSAGE_KEY, byte[].class)); assertThat(receivedKey).isEqualTo("myDynamicKey"); producerBinding.unbind(); consumerBinding.unbind(); }
Example 6
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@Test @SuppressWarnings("unchecked") public void testCustomPartitionCountOverridesDefaultIfLarger() throws Exception { byte[] testPayload = new byte[2048]; Arrays.fill(testPayload, (byte) 65); KafkaBinderConfigurationProperties binderConfiguration = createConfigurationProperties(); binderConfiguration.setMinPartitionCount(10); Binder binder = getBinder(binderConfiguration); QueueChannel moduleInputChannel = new QueueChannel(); ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties(); producerProperties.setPartitionCount(10); producerProperties.setPartitionKeyExpression(new LiteralExpression("foo")); DirectChannel moduleOutputChannel = createBindableChannel("output", createProducerBindingProperties(producerProperties)); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); long uniqueBindingId = System.currentTimeMillis(); Binding<MessageChannel> producerBinding = binder.bindProducer( "foo" + uniqueBindingId + ".0", moduleOutputChannel, producerProperties); Binding<MessageChannel> consumerBinding = binder.bindConsumer( "foo" + uniqueBindingId + ".0", null, moduleInputChannel, consumerProperties); Message<?> message = org.springframework.integration.support.MessageBuilder .withPayload(testPayload).build(); // Let the consumer actually bind to the producer before sending a msg binderBindUnbindLatency(); moduleOutputChannel.send(message); Message<?> inbound = receive(moduleInputChannel); assertThat(inbound).isNotNull(); assertThat((byte[]) inbound.getPayload()).containsExactly(testPayload); assertThat(partitionSize("foo" + uniqueBindingId + ".0")).isEqualTo(10); producerBinding.unbind(); consumerBinding.unbind(); }
Example 7
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("unchecked") // TODO: This test needs to be rethought - sending byte[] without explicit content // type // - yet being converted by the json converter public void testCompression() throws Exception { final KafkaProducerProperties.CompressionType[] codecs = new KafkaProducerProperties.CompressionType[] { KafkaProducerProperties.CompressionType.none, KafkaProducerProperties.CompressionType.gzip, KafkaProducerProperties.CompressionType.snappy }; byte[] testPayload = new byte[2048]; Arrays.fill(testPayload, (byte) 65); Binder binder = getBinder(); for (KafkaProducerProperties.CompressionType codec : codecs) { ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties(); producerProperties.getExtension().setCompressionType( KafkaProducerProperties.CompressionType.valueOf(codec.toString())); DirectChannel moduleOutputChannel = createBindableChannel("output", createProducerBindingProperties(producerProperties)); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); DirectChannel moduleInputChannel = createBindableChannel("input", createConsumerBindingProperties(consumerProperties)); Binding<MessageChannel> producerBinding = binder.bindProducer( "testCompression", moduleOutputChannel, producerProperties); Binding<MessageChannel> consumerBinding = binder.bindConsumer( "testCompression", "test", moduleInputChannel, consumerProperties); Message<?> message = org.springframework.integration.support.MessageBuilder .withPayload(testPayload).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(inboundMessageRef.get().getPayload()).containsExactly(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 @Override @SuppressWarnings("unchecked") public void testAnonymousGroup() throws Exception { Binder binder = getBinder(); BindingProperties producerBindingProperties = createProducerBindingProperties( createProducerProperties()); DirectChannel output = createBindableChannel("output", producerBindingProperties); Binding<MessageChannel> producerBinding = binder.bindProducer("defaultGroup.0", output, producerBindingProperties.getProducer()); QueueChannel input1 = new QueueChannel(); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); Binding<MessageChannel> binding1 = binder.bindConsumer("defaultGroup.0", null, input1, consumerProperties); QueueChannel input2 = new QueueChannel(); Binding<MessageChannel> binding2 = binder.bindConsumer("defaultGroup.0", null, input2, consumerProperties); // Since we don't provide any topic info, let Kafka bind the consumer successfully Thread.sleep(1000); String testPayload1 = "foo-" + UUID.randomUUID().toString(); output.send(new GenericMessage<>(testPayload1.getBytes())); Message<byte[]> receivedMessage1 = (Message<byte[]>) receive(input1); assertThat(receivedMessage1).isNotNull(); assertThat(new String(receivedMessage1.getPayload(), StandardCharsets.UTF_8)) .isEqualTo(testPayload1); Message<byte[]> receivedMessage2 = (Message<byte[]>) receive(input2); assertThat(receivedMessage2).isNotNull(); assertThat(new String(receivedMessage2.getPayload(), StandardCharsets.UTF_8)) .isEqualTo(testPayload1); binding2.unbind(); String testPayload2 = "foo-" + UUID.randomUUID().toString(); output.send(new GenericMessage<>(testPayload2.getBytes())); binding2 = binder.bindConsumer("defaultGroup.0", null, input2, consumerProperties); // Since we don't provide any topic info, let Kafka bind the consumer successfully Thread.sleep(1000); String testPayload3 = "foo-" + UUID.randomUUID().toString(); output.send(new GenericMessage<>(testPayload3.getBytes())); receivedMessage1 = (Message<byte[]>) receive(input1); assertThat(receivedMessage1).isNotNull(); assertThat(new String(receivedMessage1.getPayload(), StandardCharsets.UTF_8)) .isEqualTo(testPayload2); receivedMessage1 = (Message<byte[]>) receive(input1); assertThat(receivedMessage1).isNotNull(); assertThat(new String(receivedMessage1.getPayload(), StandardCharsets.UTF_8)) .isNotNull(); receivedMessage2 = (Message<byte[]>) receive(input2); assertThat(receivedMessage2).isNotNull(); assertThat(new String(receivedMessage2.getPayload(), StandardCharsets.UTF_8)) .isEqualTo(testPayload3); Map<String, TopicInformation> topicsInUse = ((KafkaTestBinder) binder) .getCoreBinder().getTopicsInUse(); assertThat(topicsInUse.keySet()).contains("defaultGroup.0"); TopicInformation topic = topicsInUse.get("defaultGroup.0"); assertThat(topic.isConsumerTopic()).isTrue(); assertThat(topic.getConsumerGroup()).startsWith("anonymous"); producerBinding.unbind(); binding1.unbind(); binding2.unbind(); }
Example 9
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void testNativeSerializationWithCustomSerializerDeserializer() throws Exception { Binding<?> producerBinding = null; Binding<?> consumerBinding = null; try { Integer testPayload = 10; Message<?> message = MessageBuilder.withPayload(testPayload).build(); SubscribableChannel moduleOutputChannel = new DirectChannel(); String testTopicName = "existing" + System.currentTimeMillis(); KafkaBinderConfigurationProperties configurationProperties = createConfigurationProperties(); configurationProperties.setAutoAddPartitions(true); Binder binder = getBinder(configurationProperties); QueueChannel moduleInputChannel = new QueueChannel(); ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties(); producerProperties.setUseNativeEncoding(true); producerProperties.getExtension().getConfiguration().put("value.serializer", "org.apache.kafka.common.serialization.IntegerSerializer"); producerBinding = binder.bindProducer(testTopicName, moduleOutputChannel, producerProperties); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); consumerProperties.getExtension().setAutoRebalanceEnabled(false); consumerProperties.getExtension().getConfiguration().put("value.deserializer", "org.apache.kafka.common.serialization.IntegerDeserializer"); consumerProperties.getExtension() .setStandardHeaders(KafkaConsumerProperties.StandardHeaders.both); consumerBinding = binder.bindConsumer(testTopicName, "test", moduleInputChannel, consumerProperties); // Let the consumer actually bind to the producer before sending a msg binderBindUnbindLatency(); moduleOutputChannel.send(message); Message<?> inbound = receive(moduleInputChannel, 500); assertThat(inbound).isNotNull(); assertThat(inbound.getPayload()).isEqualTo(10); assertThat(inbound.getHeaders()).doesNotContainKey("contentType"); assertThat(inbound.getHeaders().getId()).isNotNull(); assertThat(inbound.getHeaders().getTimestamp()).isNotNull(); } finally { if (producerBinding != null) { producerBinding.unbind(); } if (consumerBinding != null) { consumerBinding.unbind(); } } }
Example 10
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@Test @Override @SuppressWarnings("unchecked") public void testSendAndReceiveMultipleTopics() throws Exception { Binder binder = getBinder(); DirectChannel moduleOutputChannel1 = createBindableChannel("output1", createProducerBindingProperties(createProducerProperties())); DirectChannel moduleOutputChannel2 = createBindableChannel("output2", createProducerBindingProperties(createProducerProperties())); QueueChannel moduleInputChannel = new QueueChannel(); ExtendedProducerProperties<KafkaProducerProperties> producer1Props = createProducerProperties(); producer1Props.getExtension().setUseTopicHeader(true); Binding<MessageChannel> producerBinding1 = binder.bindProducer("foo.x", moduleOutputChannel1, producer1Props); Binding<MessageChannel> producerBinding2 = binder.bindProducer("foo.y", moduleOutputChannel2, createProducerProperties()); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); consumerProperties.getExtension().setAutoRebalanceEnabled(false); Binding<MessageChannel> consumerBinding1 = binder.bindConsumer("foo.x", "test1", moduleInputChannel, consumerProperties); Binding<MessageChannel> consumerBinding2 = binder.bindConsumer("foo.y", "test2", moduleInputChannel, consumerProperties); String testPayload1 = "foo1"; Message<?> message1 = org.springframework.integration.support.MessageBuilder .withPayload(testPayload1.getBytes()).build(); String testPayload2 = "foo2"; Message<?> message2 = org.springframework.integration.support.MessageBuilder .withPayload(testPayload2.getBytes()).build(); String testPayload3 = "foo3"; Message<?> message3 = org.springframework.integration.support.MessageBuilder .withPayload(testPayload3.getBytes()) .setHeader(KafkaHeaders.TOPIC, "foo.y") .build(); // Let the consumer actually bind to the producer before sending a msg binderBindUnbindLatency(); moduleOutputChannel1.send(message1); moduleOutputChannel2.send(message2); moduleOutputChannel1.send(message3); Message<?>[] messages = new Message[3]; messages[0] = receive(moduleInputChannel); messages[1] = receive(moduleInputChannel); messages[2] = receive(moduleInputChannel); assertThat(messages[0]).isNotNull(); assertThat(messages[1]).isNotNull(); assertThat(messages[1]).isNotNull(); assertThat(messages).extracting("payload").containsExactlyInAnyOrder( testPayload1.getBytes(), testPayload2.getBytes(), testPayload3.getBytes()); Arrays.asList(messages).forEach(message -> { if (new String((byte[]) message.getPayload()).equals("foo1")) { assertThat(message.getHeaders().get(KafkaHeaders.RECEIVED_TOPIC)).isEqualTo("foo.x"); } else { assertThat(message.getHeaders().get(KafkaHeaders.RECEIVED_TOPIC)).isEqualTo("foo.y"); } }); producerBinding1.unbind(); producerBinding2.unbind(); consumerBinding1.unbind(); consumerBinding2.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: 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 13
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 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") 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 18
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 19
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 20
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@Test @Override @SuppressWarnings("unchecked") public void testSendAndReceiveNoOriginalContentType() throws Exception { Binder binder = getBinder(); BindingProperties producerBindingProperties = createProducerBindingProperties( createProducerProperties()); DirectChannel moduleOutputChannel = createBindableChannel("output", producerBindingProperties); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); DirectChannel moduleInputChannel = createBindableChannel("input", createConsumerBindingProperties(consumerProperties)); Binding<MessageChannel> producerBinding = binder.bindProducer("bar.0", moduleOutputChannel, producerBindingProperties.getProducer()); consumerProperties.getExtension() .setTrustedPackages(new String[] { "org.springframework.util" }); Binding<MessageChannel> consumerBinding = binder.bindConsumer("bar.0", "testSendAndReceiveNoOriginalContentType", moduleInputChannel, consumerProperties); binderBindUnbindLatency(); // TODO: Will have to fix the MimeType to convert to byte array once this issue // has been resolved: // https://github.com/spring-projects/spring-kafka/issues/424 Message<?> message = org.springframework.integration.support.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<>(); 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("foo".getBytes()); assertThat(inboundMessageRef.get().getHeaders().get(MessageHeaders.CONTENT_TYPE)) .isEqualTo(MimeTypeUtils.TEXT_PLAIN); producerBinding.unbind(); consumerBinding.unbind(); }