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

The following examples show how to use org.springframework.cloud.stream.binder.ExtendedConsumerProperties#setBackOffInitialInterval() . 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: 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 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")
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 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 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 4
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 5
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();
}