Java Code Examples for org.springframework.cloud.stream.binder.ExtendedConsumerProperties#setMaxAttempts()

The following examples show how to use org.springframework.cloud.stream.binder.ExtendedConsumerProperties#setMaxAttempts() . 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: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 5 votes vote down vote up
@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 2
Source File: KafkaBinderTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
@Ignore
public void testDlqWithNativeSerializationEnabledOnDlqProducer() 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");
	consumerProperties.getExtension().setConfiguration(consumerConfig);

	// Setting dlq producer properties on the consumer
	consumerProperties.getExtension()
			.setDlqProducerProperties(producerProperties.getExtension());
	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-1", 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-1", null, dlqChannel,
			dlqConsumerProperties);
	binderBindUnbindLatency();

	Message<?> message = org.springframework.integration.support.MessageBuilder
			.withPayload("foo").build();

	moduleOutputChannel.send(message);

	Message<?> receivedMessage = receive(dlqChannel, 5);
	assertThat(receivedMessage).isNotNull();
	assertThat(receivedMessage.getPayload()).isEqualTo("foo".getBytes());
	assertThat(handler.getInvocationCount())
			.isEqualTo(consumerProperties.getMaxAttempts());
	assertThat(receivedMessage.getHeaders()
			.get(KafkaMessageChannelBinder.X_ORIGINAL_TOPIC))
					.isEqualTo("foo.bar".getBytes(StandardCharsets.UTF_8));
	assertThat(new String((byte[]) receivedMessage.getHeaders()
			.get(KafkaMessageChannelBinder.X_EXCEPTION_MESSAGE))).startsWith(
					"Dispatcher failed to deliver Message; nested exception is java.lang.RuntimeException: fail");
	assertThat(receivedMessage.getHeaders()
			.get(KafkaMessageChannelBinder.X_EXCEPTION_STACKTRACE)).isNotNull();
	binderBindUnbindLatency();

	dlqConsumerBinding.unbind();

	producerBinding.unbind();
	consumerBinding.unbind();
}
 
Example 3
Source File: KafkaBinderTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 4 votes vote down vote up
@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 4
Source File: KafkaBinderTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testDlqWithProducerPropertiesSetAtBinderLevel()
		throws Exception {

	KafkaBinderConfigurationProperties binderConfiguration = createConfigurationProperties();

	Map<String, String> consumerProps = new HashMap<>();
	consumerProps.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
	binderConfiguration.setConsumerProperties(consumerProps);

	Map<String, String> producerProps = new HashMap<>();
	producerProps.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
	binderConfiguration.setProducerProperties(producerProps);
	Binder binder = getBinder(binderConfiguration);

	ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties();
	producerProperties.setUseNativeEncoding(true);
	BindingProperties outputBindingProperties = createProducerBindingProperties(
			producerProperties);
	DirectChannel moduleOutputChannel = createBindableChannel("output",
			outputBindingProperties);

	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();
	consumerProperties.setUseNativeDecoding(true);
	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",
			"tdwcapsabl", 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." + "tdwcapsabl", null, dlqChannel,
			dlqConsumerProperties);
	binderBindUnbindLatency();

	Message<?> message = org.springframework.integration.support.MessageBuilder
			.withPayload("foo").build();

	moduleOutputChannel.send(message);

	Message<?> receivedMessage = dlqChannel.receive(5000);
	assertThat(receivedMessage).isNotNull();
	assertThat(receivedMessage.getPayload()).isEqualTo("foo");

	binderBindUnbindLatency();

	dlqConsumerBinding.unbind();

	producerBinding.unbind();
	consumerBinding.unbind();
}
 
Example 5
Source File: KafkaBinderTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 4 votes vote down vote up
@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 6
Source File: KafkaBinderTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 4 votes vote down vote up
@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 7
Source File: KafkaBinderTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 4 votes vote down vote up
@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 8
Source File: KafkaBinderTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testPolledConsumerWithDlq() throws Exception {
	KafkaTestBinder binder = getBinder();
	PollableSource<MessageHandler> inboundBindTarget = new DefaultPollableMessageSource(
			this.messageConverter);
	ExtendedConsumerProperties<KafkaConsumerProperties> properties = createConsumerProperties();
	properties.getExtension().setPollTimeout(1);
	properties.setMaxAttempts(2);
	properties.setBackOffInitialInterval(0);
	properties.getExtension().setEnableDlq(true);
	Map<String, Object> producerProps = KafkaTestUtils
			.producerProps(embeddedKafka.getEmbeddedKafka());
	Binding<PollableSource<MessageHandler>> binding = binder.bindPollableConsumer(
			"pollableDlq", "group-pcWithDlq", inboundBindTarget, properties);
	KafkaTemplate template = new KafkaTemplate(
			new DefaultKafkaProducerFactory<>(producerProps));
	template.send("pollableDlq", "testPollableDLQ");
	try {
		int n = 0;
		while (n++ < 100) {
			inboundBindTarget.poll(m -> {
				throw new RuntimeException("test DLQ");
			});
			Thread.sleep(100);
		}
	}
	catch (MessageHandlingException e) {
		assertThat(e.getCause().getMessage()).isEqualTo("test DLQ");
	}
	Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("dlq", "false",
			embeddedKafka.getEmbeddedKafka());
	consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
	ConsumerFactory cf = new DefaultKafkaConsumerFactory<>(consumerProps);
	Consumer consumer = cf.createConsumer();
	embeddedKafka.getEmbeddedKafka().consumeFromAnEmbeddedTopic(consumer,
			"error.pollableDlq.group-pcWithDlq");
	ConsumerRecord deadLetter = KafkaTestUtils.getSingleRecord(consumer,
			"error.pollableDlq.group-pcWithDlq");
	assertThat(deadLetter).isNotNull();
	assertThat(deadLetter.value()).isEqualTo("testPollableDLQ");
	binding.unbind();
	consumer.close();
}
 
Example 9
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 4 votes vote down vote up
@Test
public void testConsumerProperties() throws Exception {
	RabbitTestBinder binder = getBinder();
	ExtendedConsumerProperties<RabbitConsumerProperties> properties = createConsumerProperties();
	properties.getExtension().setRequeueRejected(true);
	properties.getExtension().setTransacted(true);
	properties.getExtension().setExclusive(true);
	properties.getExtension().setMissingQueuesFatal(true);
	properties.getExtension().setFailedDeclarationRetryInterval(1500L);
	properties.getExtension().setQueueDeclarationRetries(23);
	Binding<MessageChannel> consumerBinding = binder.bindConsumer("props.0", null,
			createBindableChannel("input", new BindingProperties()), properties);
	Lifecycle endpoint = extractEndpoint(consumerBinding);
	SimpleMessageListenerContainer container = TestUtils.getPropertyValue(endpoint,
			"messageListenerContainer", SimpleMessageListenerContainer.class);
	assertThat(container.getAcknowledgeMode()).isEqualTo(AcknowledgeMode.AUTO);
	assertThat(container.getQueueNames()[0])
			.startsWith(properties.getExtension().getPrefix());
	assertThat(TestUtils.getPropertyValue(container, "transactional", Boolean.class))
			.isTrue();
	assertThat(TestUtils.getPropertyValue(container, "exclusive", Boolean.class))
			.isTrue();
	assertThat(TestUtils.getPropertyValue(container, "concurrentConsumers"))
			.isEqualTo(1);
	assertThat(TestUtils.getPropertyValue(container, "maxConcurrentConsumers"))
			.isNull();
	assertThat(TestUtils.getPropertyValue(container, "defaultRequeueRejected",
			Boolean.class)).isTrue();
	assertThat(TestUtils.getPropertyValue(container, "prefetchCount")).isEqualTo(1);
	assertThat(TestUtils.getPropertyValue(container, "batchSize")).isEqualTo(1);
	assertThat(TestUtils.getPropertyValue(container, "missingQueuesFatal",
			Boolean.class)).isTrue();
	assertThat(
			TestUtils.getPropertyValue(container, "failedDeclarationRetryInterval"))
					.isEqualTo(1500L);
	assertThat(TestUtils.getPropertyValue(container, "declarationRetries"))
			.isEqualTo(23);
	RetryTemplate retry = TestUtils.getPropertyValue(endpoint, "retryTemplate",
			RetryTemplate.class);
	assertThat(TestUtils.getPropertyValue(retry, "retryPolicy.maxAttempts"))
			.isEqualTo(3);
	assertThat(TestUtils.getPropertyValue(retry, "backOffPolicy.initialInterval"))
			.isEqualTo(1000L);
	assertThat(TestUtils.getPropertyValue(retry, "backOffPolicy.maxInterval"))
			.isEqualTo(10000L);
	assertThat(TestUtils.getPropertyValue(retry, "backOffPolicy.multiplier"))
			.isEqualTo(2.0);
	consumerBinding.unbind();
	assertThat(endpoint.isRunning()).isFalse();

	properties = createConsumerProperties();
	properties.getExtension().setAcknowledgeMode(AcknowledgeMode.NONE);
	properties.setBackOffInitialInterval(2000);
	properties.setBackOffMaxInterval(20000);
	properties.setBackOffMultiplier(5.0);
	properties.setConcurrency(2);
	properties.setMaxAttempts(23);
	properties.getExtension().setMaxConcurrency(3);
	properties.getExtension().setPrefix("foo.");
	properties.getExtension().setPrefetch(20);
	properties.getExtension().setHeaderPatterns(new String[] { "foo" });
	properties.getExtension().setTxSize(10);
	QuorumConfig quorum = properties.getExtension().getQuorum();
	quorum.setEnabled(true);
	quorum.setDeliveryLimit(10);
	quorum.setInitialGroupSize(1);
	properties.setInstanceIndex(0);
	consumerBinding = binder.bindConsumer("props.0", "test",
			createBindableChannel("input", new BindingProperties()), properties);

	endpoint = extractEndpoint(consumerBinding);
	container = verifyContainer(endpoint);

	assertThat(container.getQueueNames()[0]).isEqualTo("foo.props.0.test");

	consumerBinding.unbind();
	assertThat(endpoint.isRunning()).isFalse();
}
 
Example 10
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 4 votes vote down vote up
@Test
public void testDurablePubSubWithAutoBindDLQ() 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(true);
	consumerProperties.setMaxAttempts(1); // disable retry
	DirectChannel moduleInputChannel = createBindableChannel("input",
			createConsumerBindingProperties(consumerProperties));
	moduleInputChannel.setBeanName("durableTest");
	moduleInputChannel.subscribe(new MessageHandler() {

		@Override
		public void handleMessage(Message<?> message) throws MessagingException {
			throw new RuntimeException("foo");
		}

	});
	Binding<MessageChannel> consumerBinding = binder.bindConsumer("durabletest.0",
			"tgroup", moduleInputChannel, consumerProperties);

	RabbitTemplate template = new RabbitTemplate(
			this.rabbitAvailableRule.getResource());
	template.convertAndSend(TEST_PREFIX + "durabletest.0", "", "foo");

	int n = 0;
	while (n++ < 100) {
		Object deadLetter = template
				.receiveAndConvert(TEST_PREFIX + "durabletest.0.tgroup.dlq");
		if (deadLetter != null) {
			assertThat(deadLetter).isEqualTo("foo");
			break;
		}
		Thread.sleep(100);
	}
	assertThat(n).isLessThan(100);

	consumerBinding.unbind();
	assertThat(admin.getQueueProperties(TEST_PREFIX + "durabletest.0.tgroup.dlq"))
			.isNotNull();
}