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

The following examples show how to use org.springframework.cloud.stream.binder.PollableSource. 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: BindingService.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
public <T> Binding<T> doBindPollableConsumer(T input, String inputName,
		Binder<T, ConsumerProperties, ?> binder,
		ConsumerProperties consumerProperties, String target) {
	if (this.taskScheduler == null
			|| this.bindingServiceProperties.getBindingRetryInterval() <= 0) {
		return ((PollableConsumerBinder) binder).bindPollableConsumer(target,
				this.bindingServiceProperties.getGroup(inputName),
				(PollableSource) input, consumerProperties);
	}
	else {
		try {
			return ((PollableConsumerBinder) binder).bindPollableConsumer(target,
					this.bindingServiceProperties.getGroup(inputName),
					(PollableSource) input, consumerProperties);
		}
		catch (RuntimeException e) {
			LateBinding<T> late = new LateBinding<T>(target,
					e.getCause() == null ? e.toString() : e.getCause().getMessage(), consumerProperties, true);
			reschedulePollableConsumerBinding(input, inputName, binder,
					consumerProperties, target, late, e);
			return late;
		}
	}
}
 
Example #3
Source File: BindingService.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
public <T> void reschedulePollableConsumerBinding(final T input,
		final String inputName, final Binder<T, ConsumerProperties, ?> binder,
		final ConsumerProperties consumerProperties, final String target,
		final LateBinding<T> late, RuntimeException exception) {
	assertNotIllegalException(exception);
	this.log.error("Failed to create consumer binding; retrying in "
			+ this.bindingServiceProperties.getBindingRetryInterval() + " seconds",
			exception);
	this.scheduleTask(() -> {
		try {
			late.setDelegate(((PollableConsumerBinder) binder).bindPollableConsumer(
					target, this.bindingServiceProperties.getGroup(inputName),
					(PollableSource) input, consumerProperties));
		}
		catch (RuntimeException e) {
			reschedulePollableConsumerBinding(input, inputName, binder,
					consumerProperties, target, late, e);
		}
	});
}
 
Example #4
Source File: RabbitTestBinder.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 5 votes vote down vote up
@Override
public Binding<PollableSource<MessageHandler>> bindPollableConsumer(String name,
		String group, PollableSource<MessageHandler> inboundBindTarget,
		ExtendedConsumerProperties<RabbitConsumerProperties> properties) {
	captureConsumerResources(name, group, properties);
	return super.bindPollableConsumer(name, group, inboundBindTarget, properties);
}
 
Example #5
Source File: GenericsUtils.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
private static Class<?> findPollableSourceType(Class<?>[] targetInterfaces) {
	for (Class<?> targetIntf : targetInterfaces) {
		if (PollableSource.class.isAssignableFrom(targetIntf)) {
			Type[] supers = targetIntf.getGenericInterfaces();
			for (Type type : supers) {
				ResolvableType resolvableType = ResolvableType.forType(type);
				if (resolvableType.getRawClass().equals(PollableSource.class)) {
					return resolvableType.getGeneric(0).getRawClass();
				}
			}
		}
	}
	return null;
}
 
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();
}