org.springframework.cloud.stream.binder.DefaultPollableMessageSource Java Examples

The following examples show how to use org.springframework.cloud.stream.binder.DefaultPollableMessageSource. 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 6 votes vote down vote up
@Test
public void testPolledConsumer() throws Exception {
	RabbitTestBinder binder = getBinder();
	PollableSource<MessageHandler> inboundBindTarget = new DefaultPollableMessageSource(
			this.messageConverter);
	Binding<PollableSource<MessageHandler>> binding = binder.bindPollableConsumer(
			"pollable", "group", inboundBindTarget, createConsumerProperties());
	RabbitTemplate template = new RabbitTemplate(
			this.rabbitAvailableRule.getResource());
	template.convertAndSend("pollable.group", "testPollable");
	boolean polled = inboundBindTarget.poll(m -> {
		assertThat(m.getPayload()).isEqualTo("testPollable");
	});
	int n = 0;
	while (n++ < 100 && !polled) {
		polled = inboundBindTarget.poll(m -> {
			assertThat(m.getPayload()).isEqualTo("testPollable");
		});
	}
	assertThat(polled).isTrue();
	binding.unbind();
}
 
Example #2
Source File: KafkaMessageChannelBinder.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@Override
protected void postProcessPollableSource(DefaultPollableMessageSource bindingTarget) {
	bindingTarget.setAttributesProvider((accessor, message) -> {
		Object rawMessage = message.getHeaders().get(KafkaHeaders.RAW_DATA);
		if (rawMessage != null) {
			accessor.setAttribute(KafkaHeaders.RAW_DATA, rawMessage);
		}
	});
}
 
Example #3
Source File: RabbitMessageChannelBinder.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 5 votes vote down vote up
@Override
protected void postProcessPollableSource(DefaultPollableMessageSource bindingTarget) {
	bindingTarget.setAttributesProvider((accessor, message) -> {
		Object rawMessage = message.getHeaders()
				.get(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE);
		if (rawMessage != null) {
			accessor.setAttribute(
					AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE,
					rawMessage);
		}
	});
}
 
Example #4
Source File: MessageSourceBindingTargetFactory.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Override
public PollableMessageSource createInput(String name) {
	DefaultPollableMessageSource binding = new DefaultPollableMessageSource(
			this.messageConverter);
	this.messageSourceConfigurer.configurePolledMessageSource(binding, name);
	return binding;
}
 
Example #5
Source File: MessageConverterConfigurer.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Override
public void configurePolledMessageSource(PollableMessageSource binding, String name) {
	BindingProperties bindingProperties = this.bindingServiceProperties
			.getBindingProperties(name);
	String contentType = bindingProperties.getContentType();
	ConsumerProperties consumerProperties = bindingProperties.getConsumer();
	if ((consumerProperties == null || !consumerProperties.isUseNativeDecoding())
			&& binding instanceof DefaultPollableMessageSource) {
		((DefaultPollableMessageSource) binding).addInterceptor(
				new InboundContentTypeEnhancingInterceptor(contentType));
	}
}
 
Example #6
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 testPolledConsumer() throws Exception {
	KafkaTestBinder binder = getBinder();
	PollableSource<MessageHandler> inboundBindTarget = new DefaultPollableMessageSource(
			this.messageConverter);
	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProps = createConsumerProperties();
	consumerProps.setMultiplex(true);
	consumerProps.getExtension().setPollTimeout(1);
	Binding<PollableSource<MessageHandler>> binding = binder.bindPollableConsumer(
			"pollable,anotherOne", "group-polledConsumer", inboundBindTarget,
			consumerProps);
	Map<String, Object> producerProps = KafkaTestUtils
			.producerProps(embeddedKafka.getEmbeddedKafka());
	KafkaTemplate template = new KafkaTemplate(
			new DefaultKafkaProducerFactory<>(producerProps));
	template.send("pollable", "testPollable");
	boolean polled = inboundBindTarget.poll(m -> {
		assertThat(m.getPayload()).isEqualTo("testPollable");
	});
	int n = 0;
	while (n++ < 100 && !polled) {
		polled = inboundBindTarget.poll(m -> {
			assertThat(m.getPayload()).isEqualTo("testPollable".getBytes());
		});
		Thread.sleep(100);
	}
	assertThat(polled).isTrue();

	template.send("anotherOne", "testPollable2");
	polled = inboundBindTarget.poll(m -> {
		assertThat(m.getPayload()).isEqualTo("testPollable2");
	});
	n = 0;
	while (n++ < 100 && !polled) {
		polled = inboundBindTarget.poll(m -> {
			assertThat(m.getPayload()).isEqualTo("testPollable2".getBytes());
		});
		Thread.sleep(100);
	}
	assertThat(polled).isTrue();
	// Bind a second pollable consumer GH-521
	consumerProps.getExtension().getConfiguration()
			.put(ConsumerConfig.CLIENT_ID_CONFIG, "pollable2");
	PollableSource<MessageHandler> second = new DefaultPollableMessageSource(
			this.messageConverter);
	Binding<PollableSource<MessageHandler>> binding2 = binder.bindPollableConsumer(
			"pollable2", "group-polledConsumer2", second, consumerProps);
	second.poll(m -> {
	});
	binding.unbind();
	binding2.unbind();
}
 
Example #7
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();
}