Java Code Examples for org.springframework.cloud.stream.binding.MessageConverterConfigurer#configurePolledMessageSource()

The following examples show how to use org.springframework.cloud.stream.binding.MessageConverterConfigurer#configurePolledMessageSource() . 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: PollableConsumerTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultMessageSource() {
	TestChannelBinder binder = createBinder();
	MessageConverterConfigurer configurer = this.context
			.getBean(MessageConverterConfigurer.class);

	DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(
			this.messageConverter);
	configurer.configurePolledMessageSource(pollableSource, "foo");
	ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(
			null);
	properties.setMaxAttempts(2);
	properties.setBackOffInitialInterval(0);
	binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
	AtomicInteger count = new AtomicInteger();
	assertThat(pollableSource.poll(message -> {
		assertThat(message).isNotNull();
		count.incrementAndGet();
	})).isTrue();
	assertThat(count.get()).isOne();
}
 
Example 2
Source File: PollableConsumerTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testAutoStartupOff() {
	TestChannelBinder binder = createBinder();
	binder.setMessageSourceDelegate(new LifecycleMessageSource(
			() -> new GenericMessage<>("{\"foo\":\"bar\"}".getBytes())));
	MessageConverterConfigurer configurer = this.context
			.getBean(MessageConverterConfigurer.class);

	DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(
			this.messageConverter);
	configurer.configurePolledMessageSource(pollableSource, "foo");
	ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(
			null);
	properties.setAutoStartup(false);

	Binding<PollableSource<MessageHandler>> pollableSourceBinding = binder
			.bindPollableConsumer("foo", "bar", pollableSource, properties);

	assertThat(pollableSourceBinding.isRunning()).isFalse();
}
 
Example 3
Source File: PollableConsumerTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testAutoStartupOn() {
	TestChannelBinder binder = createBinder();
	binder.setMessageSourceDelegate(new LifecycleMessageSource(
			() -> new GenericMessage<>("{\"foo\":\"bar\"}".getBytes())));
	MessageConverterConfigurer configurer = this.context
			.getBean(MessageConverterConfigurer.class);

	DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(
			this.messageConverter);
	configurer.configurePolledMessageSource(pollableSource, "foo");
	ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(
			null);
	properties.setAutoStartup(true);

	Binding<PollableSource<MessageHandler>> pollableSourceBinding = binder
			.bindPollableConsumer("foo", "bar", pollableSource, properties);

	assertThat(pollableSourceBinding.isRunning()).isTrue();
}
 
Example 4
Source File: PollableConsumerTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimple() {
	TestChannelBinder binder = createBinder();
	MessageConverterConfigurer configurer = this.context
			.getBean(MessageConverterConfigurer.class);

	DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(
			this.messageConverter);
	configurer.configurePolledMessageSource(pollableSource, "foo");
	pollableSource.addInterceptor(new ChannelInterceptor() {

		@Override
		public Message<?> preSend(Message<?> message, MessageChannel channel) {
			return MessageBuilder
					.withPayload(((String) message.getPayload()).toUpperCase())
					.copyHeaders(message.getHeaders()).build();
		}

	});
	ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(
			null);
	properties.setMaxAttempts(2);
	properties.setBackOffInitialInterval(0);
	binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
	final AtomicInteger count = new AtomicInteger();
	assertThat(pollableSource.poll(received -> {
		assertThat(received.getPayload()).isEqualTo("POLLED DATA");
		assertThat(received.getHeaders().get(MessageHeaders.CONTENT_TYPE))
				.isEqualTo(MimeType.valueOf("text/plain"));
		if (count.incrementAndGet() == 1) {
			throw new RuntimeException("test retry");
		}
	})).isTrue();
	assertThat(count.get()).isEqualTo(2);
}
 
Example 5
Source File: PollableConsumerTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertSimple() {
	TestChannelBinder binder = createBinder();
	MessageConverterConfigurer configurer = this.context
			.getBean(MessageConverterConfigurer.class);

	binder.setMessageSourceDelegate(
			() -> new GenericMessage<>("{\"foo\":\"bar\"}".getBytes()));
	DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(
			this.messageConverter);
	configurer.configurePolledMessageSource(pollableSource, "foo");

	ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(
			null);
	properties.setMaxAttempts(1);
	properties.setBackOffInitialInterval(0);
	binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
	final AtomicReference<Object> payload = new AtomicReference<>();
	assertThat(pollableSource.poll(received -> {
		payload.set(received.getPayload());
	}, new ParameterizedTypeReference<Foo>() {
	})).isTrue();
	assertThat(payload.get()).isInstanceOf(Foo.class);
	assertThat(((Foo) payload.get()).getFoo()).isEqualTo("bar");
	// test the cache for coverage
	assertThat(pollableSource.poll(received -> {
		payload.set(received.getPayload());
	}, new ParameterizedTypeReference<Foo>() {
	})).isTrue();
	assertThat(payload.get()).isInstanceOf(Foo.class);
	assertThat(((Foo) payload.get()).getFoo()).isEqualTo("bar");
}
 
Example 6
Source File: PollableConsumerTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertSimpler() {
	TestChannelBinder binder = createBinder();
	MessageConverterConfigurer configurer = this.context
			.getBean(MessageConverterConfigurer.class);
	BindingServiceProperties bsps = this.context
			.getBean(BindingServiceProperties.class);
	BindingProperties props = new BindingProperties();
	props.setContentType("text/plain");
	bsps.setBindings(Collections.singletonMap("foo", props));

	binder.setMessageSourceDelegate(() -> new GenericMessage<>("foo".getBytes()));
	DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(
			this.messageConverter);
	configurer.configurePolledMessageSource(pollableSource, "foo");

	ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(
			null);
	properties.setMaxAttempts(1);
	properties.setBackOffInitialInterval(0);
	binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
	final AtomicReference<Object> payload = new AtomicReference<>();
	assertThat(pollableSource.poll(received -> {
		payload.set(received.getPayload());
	}, new ParameterizedTypeReference<String>() {
	})).isTrue();
	assertThat(payload.get()).isInstanceOf(String.class);
	assertThat(payload.get()).isEqualTo("foo");
	// test the cache for coverage
	assertThat(pollableSource.poll(received -> {
		payload.set(received.getPayload());
	}, new ParameterizedTypeReference<String>() {
	})).isTrue();
	assertThat(payload.get()).isInstanceOf(String.class);
	assertThat(payload.get()).isEqualTo("foo");
}
 
Example 7
Source File: PollableConsumerTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertList() {
	TestChannelBinder binder = createBinder();
	MessageConverterConfigurer configurer = this.context
			.getBean(MessageConverterConfigurer.class);

	binder.setMessageSourceDelegate(() -> new GenericMessage<>(
			"[{\"foo\":\"bar\"},{\"foo\":\"baz\"}]".getBytes()));
	DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(
			this.messageConverter);
	configurer.configurePolledMessageSource(pollableSource, "foo");

	ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(
			null);
	properties.setMaxAttempts(1);
	properties.setBackOffInitialInterval(0);

	binder.bindPollableConsumer("foo", "bar", pollableSource, properties);

	final AtomicReference<Object> payload = new AtomicReference<>();
	assertThat(pollableSource.poll(received -> {
		payload.set(received.getPayload());
	}, new ParameterizedTypeReference<List<Foo>>() {
	})).isTrue();
	@SuppressWarnings("unchecked")
	List<Foo> list = (List<Foo>) payload.get();
	assertThat(list.size()).isEqualTo(2);
	assertThat(list.get(0).getFoo()).isEqualTo("bar");
	assertThat(list.get(1).getFoo()).isEqualTo("baz");
}
 
Example 8
Source File: PollableConsumerTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertMap() {
	TestChannelBinder binder = createBinder();
	MessageConverterConfigurer configurer = this.context
			.getBean(MessageConverterConfigurer.class);

	binder.setMessageSourceDelegate(
			() -> new GenericMessage<>("{\"qux\":{\"foo\":\"bar\"}}".getBytes()));
	DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(
			this.messageConverter);
	configurer.configurePolledMessageSource(pollableSource, "foo");

	ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(
			null);
	properties.setMaxAttempts(1);
	properties.setBackOffInitialInterval(0);
	binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
	final AtomicReference<Object> payload = new AtomicReference<>();
	assertThat(pollableSource.poll(received -> {
		payload.set(received.getPayload());
	}, new ParameterizedTypeReference<Map<String, Foo>>() {
	})).isTrue();
	@SuppressWarnings("unchecked")
	Map<String, Foo> map = (Map<String, Foo>) payload.get();
	assertThat(map.size()).isEqualTo(1);
	assertThat(map.get("qux").getFoo()).isEqualTo("bar");
}
 
Example 9
Source File: PollableConsumerTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Test
public void testErrorsNoRetry() {
	TestChannelBinder binder = createBinder();
	MessageConverterConfigurer configurer = this.context
			.getBean(MessageConverterConfigurer.class);

	DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(
			this.messageConverter);
	configurer.configurePolledMessageSource(pollableSource, "foo");
	pollableSource.addInterceptor(new ChannelInterceptor() {

		@Override
		public Message<?> preSend(Message<?> message, MessageChannel channel) {
			return MessageBuilder
					.withPayload(((String) message.getPayload()).toUpperCase())
					.copyHeaders(message.getHeaders()).build();
		}

	});
	ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(
			null);
	properties.setMaxAttempts(1);
	binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
	final CountDownLatch latch = new CountDownLatch(1);
	this.context.getBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME,
			SubscribableChannel.class).subscribe(m -> {
				latch.countDown();
			});
	final AtomicInteger count = new AtomicInteger();
	assertThat(pollableSource.poll(received -> {
		count.incrementAndGet();
		throw new RuntimeException("test recoverer");
	})).isTrue();
	assertThat(count.get()).isEqualTo(1);
}
 
Example 10
Source File: PollableConsumerTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Test
public void testRequeue() {
	TestChannelBinder binder = createBinder();
	MessageConverterConfigurer configurer = this.context
			.getBean(MessageConverterConfigurer.class);

	DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(
			this.messageConverter);
	configurer.configurePolledMessageSource(pollableSource, "foo");
	AcknowledgmentCallback callback = mock(AcknowledgmentCallback.class);
	pollableSource.addInterceptor(new ChannelInterceptor() {

		@Override
		public Message<?> preSend(Message<?> message, MessageChannel channel) {
			return MessageBuilder.fromMessage(message)
					.setHeader(
							IntegrationMessageHeaderAccessor.ACKNOWLEDGMENT_CALLBACK,
							callback)
					.build();
		}

	});
	ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
	properties.setMaxAttempts(2);
	properties.setBackOffInitialInterval(0);
	binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
	final AtomicInteger count = new AtomicInteger();
	try {
		assertThat(pollableSource.poll(received -> {
			count.incrementAndGet();
			throw new RequeueCurrentMessageException("test retry");
		})).isTrue();
	}
	catch (Exception e) {
		// no op
	}
	assertThat(count.get()).isEqualTo(2);
	verify(callback).acknowledge(Status.REQUEUE);
}
 
Example 11
Source File: PollableConsumerTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Test
public void testRequeueWithNoAcknowledgementCallback() {
	TestChannelBinder binder = createBinder();
	MessageConverterConfigurer configurer = this.context
			.getBean(MessageConverterConfigurer.class);

	DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(
			this.messageConverter);
	configurer.configurePolledMessageSource(pollableSource, "foo");
	pollableSource.addInterceptor(new ChannelInterceptor() {

		@Override
		public Message<?> preSend(Message<?> message, MessageChannel channel) {
			return MessageBuilder.fromMessage(message)
					.build();
		}

	});
	ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
	properties.setMaxAttempts(2);
	properties.setBackOffInitialInterval(0);
	binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
	final AtomicInteger count = new AtomicInteger();

	assertThat(pollableSource.poll(received -> {
		count.incrementAndGet();
		throw new RequeueCurrentMessageException("test retry");
	})).isTrue();

	assertThat(count.get()).isEqualTo(2);

}
 
Example 12
Source File: PollableConsumerTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Test
public void testRequeueFromErrorFlow() {
	TestChannelBinder binder = createBinder();
	MessageConverterConfigurer configurer = this.context
			.getBean(MessageConverterConfigurer.class);

	DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(
			this.messageConverter);
	configurer.configurePolledMessageSource(pollableSource, "foo");
	AcknowledgmentCallback callback = mock(AcknowledgmentCallback.class);
	pollableSource.addInterceptor(new ChannelInterceptor() {

		@Override
		public Message<?> preSend(Message<?> message, MessageChannel channel) {
			return MessageBuilder.fromMessage(message)
					.setHeader(
							IntegrationMessageHeaderAccessor.ACKNOWLEDGMENT_CALLBACK,
							callback)
					.build();
		}

	});
	ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
	properties.setMaxAttempts(1);
	binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
	SubscribableChannel errorChannel = new DirectChannel();
	errorChannel.subscribe(msg -> {
		throw new RequeueCurrentMessageException((Throwable) msg.getPayload());
	});
	pollableSource.setErrorChannel(errorChannel);
	try {
		pollableSource.poll(received -> {
			throw new RuntimeException("test requeue from error flow");
		});
	}
	catch (Exception e) {
		// no op
	}
	verify(callback).acknowledge(Status.REQUEUE);
}
 
Example 13
Source File: PollableConsumerTests.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
@Test
public void testEmbedded() {
	TestChannelBinder binder = createBinder();
	MessageConverterConfigurer configurer = this.context
			.getBean(MessageConverterConfigurer.class);

	binder.setMessageSourceDelegate(() -> {
		MessageValues original = new MessageValues("foo".getBytes(),
				Collections.singletonMap(MessageHeaders.CONTENT_TYPE,
						"application/octet-stream"));
		byte[] payload = new byte[0];
		try {
			payload = EmbeddedHeaderUtils.embedHeaders(original,
					MessageHeaders.CONTENT_TYPE);
		}
		catch (Exception e) {
			fail(e.getMessage());
		}
		return new GenericMessage<>(payload);
	});
	ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(
			null);
	properties.setHeaderMode(HeaderMode.embeddedHeaders);
	DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(
			this.messageConverter);
	configurer.configurePolledMessageSource(pollableSource, "foo");
	pollableSource.addInterceptor(new ChannelInterceptor() {

		@Override
		public Message<?> preSend(Message<?> message, MessageChannel channel) {
			return MessageBuilder
					.withPayload(
							new String((byte[]) message.getPayload()).toUpperCase())
					.copyHeaders(message.getHeaders()).build();
		}

	});
	binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
	assertThat(pollableSource.poll(received -> {
		assertThat(received.getPayload()).isEqualTo("FOO");
		assertThat(received.getHeaders().get(MessageHeaders.CONTENT_TYPE))
				.isEqualTo("application/octet-stream");
	})).isTrue();
}
 
Example 14
Source File: PollableConsumerTests.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
@Test
public void testErrors() {
	TestChannelBinder binder = createBinder();
	MessageConverterConfigurer configurer = this.context
			.getBean(MessageConverterConfigurer.class);

	DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(
			this.messageConverter);
	configurer.configurePolledMessageSource(pollableSource, "foo");
	pollableSource.addInterceptor(new ChannelInterceptor() {

		@Override
		public Message<?> preSend(Message<?> message, MessageChannel channel) {
			return MessageBuilder
					.withPayload(((String) message.getPayload()).toUpperCase())
					.copyHeaders(message.getHeaders()).build();
		}

	});
	ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(
			null);
	properties.setMaxAttempts(2);
	properties.setBackOffInitialInterval(0);
	properties.getRetryableExceptions().put(IllegalStateException.class, false);
	binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
	final CountDownLatch latch = new CountDownLatch(1);
	this.context.getBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME,
			SubscribableChannel.class).subscribe(m -> {
				latch.countDown();
			});
	final AtomicInteger count = new AtomicInteger();
	assertThat(pollableSource.poll(received -> {
		count.incrementAndGet();
		throw new RuntimeException("test recoverer");
	})).isTrue();
	assertThat(count.get()).isEqualTo(2);
	Message<?> lastError = binder.getLastError();
	assertThat(lastError).isNotNull();
	assertThat(((Exception) lastError.getPayload()).getCause().getMessage())
			.isEqualTo("test recoverer");
	assertThat(pollableSource.poll(received -> {
		count.incrementAndGet();
		throw new IllegalStateException("no retries");
	})).isTrue();
	assertThat(count.get()).isEqualTo(3);
	lastError = binder.getLastError();
	assertThat(lastError).isNotNull();
	assertThat(((Exception) lastError.getPayload()).getCause().getMessage())
			.isEqualTo("no retries");
}