Java Code Examples for org.springframework.integration.channel.DirectChannel

The following examples show how to use org.springframework.integration.channel.DirectChannel. These examples are extracted from open source projects. 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 Project: spring-cloud-stream   Source File: BindingServiceTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testProducerPropertiesValidation() {
	BindingServiceProperties serviceProperties = new BindingServiceProperties();
	Map<String, BindingProperties> bindingProperties = new HashMap<>();
	BindingProperties props = new BindingProperties();
	ProducerProperties producerProperties = new ProducerProperties();
	producerProperties.setPartitionCount(0);
	props.setDestination("foo");
	props.setProducer(producerProperties);
	final String outputChannelName = "output";
	bindingProperties.put(outputChannelName, props);
	serviceProperties.setBindings(bindingProperties);
	DefaultBinderFactory binderFactory = createMockBinderFactory();
	BindingService service = new BindingService(serviceProperties, binderFactory);
	MessageChannel outputChannel = new DirectChannel();
	try {
		service.bindProducer(outputChannel, outputChannelName);
		fail("Producer properties should be validated.");
	}
	catch (IllegalStateException e) {
		assertThat(e)
				.hasMessageContaining("Partition count should be greater than zero.");
	}
}
 
Example 2
private DirectChannel createBindableChannelInternal(String channelName,
		BindingProperties bindingProperties, boolean inputChannel) {

	MessageConverterConfigurer messageConverterConfigurer = getBinder()
			.getApplicationContext().getBean(MessageConverterConfigurer.class);

	DirectChannel channel = new DirectChannel();
	channel.setBeanName(channelName);
	if (inputChannel) {
		messageConverterConfigurer.configureInputChannel(channel, channelName);
	}
	else {
		messageConverterConfigurer.configureOutputChannel(channel, channelName);
	}
	return channel;
}
 
Example 3
Source Project: spring-cloud-stream   Source File: BindingServiceTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testUnrecognizedBinderAllowedIfNotUsed() {
	HashMap<String, String> properties = new HashMap<>();
	properties.put("spring.cloud.stream.bindings.input.destination", "fooInput");
	properties.put("spring.cloud.stream.bindings.output.destination", "fooOutput");
	properties.put("spring.cloud.stream.defaultBinder", "mock1");
	properties.put("spring.cloud.stream.binders.mock1.type", "mock");
	properties.put("spring.cloud.stream.binders.kafka1.type", "kafka");
	BindingServiceProperties bindingServiceProperties = createBindingServiceProperties(
			properties);
	BinderFactory binderFactory = new BindingServiceConfiguration()
			.binderFactory(createMockBinderTypeRegistry(), bindingServiceProperties);
	BindingService bindingService = new BindingService(bindingServiceProperties,
			binderFactory);
	bindingService.bindConsumer(new DirectChannel(), "input");
	bindingService.bindProducer(new DirectChannel(), "output");
}
 
Example 4
@Test
@SuppressWarnings("unchecked")
public void testSyncProducerMetadata() throws Exception {
	Binder binder = getBinder(createConfigurationProperties());
	DirectChannel output = new DirectChannel();
	String testTopicName = UUID.randomUUID().toString();
	ExtendedProducerProperties<KafkaProducerProperties> properties = createProducerProperties();
	properties.getExtension().setSync(true);
	Binding<MessageChannel> producerBinding = binder.bindProducer(testTopicName,
			output, properties);
	DirectFieldAccessor accessor = new DirectFieldAccessor(
			extractEndpoint(producerBinding));
	KafkaProducerMessageHandler wrappedInstance = (KafkaProducerMessageHandler) accessor
			.getWrappedInstance();
	assertThat(new DirectFieldAccessor(wrappedInstance).getPropertyValue("sync")
			.equals(Boolean.TRUE))
					.withFailMessage("Kafka Sync Producer should have been enabled.");
	producerBinding.unbind();
}
 
Example 5
@Test
@SuppressWarnings("unchecked")
public void testAutoConfigureTopicsDisabledSucceedsIfTopicExisting()
		throws Throwable {
	KafkaBinderConfigurationProperties configurationProperties = createConfigurationProperties();

	String testTopicName = "existing" + System.currentTimeMillis();
	invokeCreateTopic(testTopicName, 5, 1);
	configurationProperties.setAutoCreateTopics(false);
	Binder binder = getBinder(configurationProperties);

	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();

	DirectChannel input = createBindableChannel("input",
			createConsumerBindingProperties(consumerProperties));
	Binding<MessageChannel> binding = binder.bindConsumer(testTopicName, "test",
			input, consumerProperties);
	binding.unbind();
}
 
Example 6
@Test
@SuppressWarnings("unchecked")
public void testPartitionCountIncreasedIfAutoAddPartitionsSet() throws Throwable {
	KafkaBinderConfigurationProperties configurationProperties = createConfigurationProperties();

	String testTopicName = "existing" + System.currentTimeMillis();
	configurationProperties.setMinPartitionCount(6);
	configurationProperties.setAutoAddPartitions(true);
	Binder binder = getBinder(configurationProperties);
	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();
	DirectChannel input = createBindableChannel("input",
			createConsumerBindingProperties(consumerProperties));

	Binding<?> binding = binder.bindConsumer(testTopicName, "test", input,
			consumerProperties);
	binding.unbind();
	assertThat(invokePartitionSize(testTopicName)).isEqualTo(6);
}
 
Example 7
@Test
@SuppressWarnings("unchecked")
public void testAutoAddPartitionsDisabledSucceedsIfTopicUnderPartitionedAndAutoRebalanceEnabled()
		throws Throwable {
	KafkaBinderConfigurationProperties configurationProperties = createConfigurationProperties();

	String testTopicName = "existing" + System.currentTimeMillis();
	invokeCreateTopic(testTopicName, 1, 1);
	configurationProperties.setAutoAddPartitions(false);
	Binder binder = getBinder(configurationProperties);
	GenericApplicationContext context = new GenericApplicationContext();
	context.refresh();

	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();

	DirectChannel input = createBindableChannel("input",
			createConsumerBindingProperties(consumerProperties));

	// this consumer must consume from partition 2
	consumerProperties.setInstanceCount(3);
	consumerProperties.setInstanceIndex(2);
	Binding binding = binder.bindConsumer(testTopicName, "test", input,
			consumerProperties);
	binding.unbind();
	assertThat(invokePartitionSize(testTopicName)).isEqualTo(1);
}
 
Example 8
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void propertyPassthrough() {
	Map<String, BindingProperties> bindings = new HashMap<>();
	BindingProperties genericProperties = new BindingProperties();
	genericProperties.setContentType("text/plain");
	bindings.put("foo", genericProperties);
	this.bindingServiceProperties.setBindings(bindings);
	Binder binder = mock(Binder.class);
	Binder binder2 = mock(Binder.class);
	BinderFactory mockBinderFactory = Mockito.mock(BinderFactory.class);
	Binding<MessageChannel> fooBinding = Mockito.mock(Binding.class);
	Binding<MessageChannel> barBinding = Mockito.mock(Binding.class);
	when(binder.bindProducer(matches("foo"), any(DirectChannel.class),
			any(ProducerProperties.class))).thenReturn(fooBinding);
	when(binder2.bindProducer(matches("bar"), any(DirectChannel.class),
			any(ProducerProperties.class))).thenReturn(barBinding);
	when(mockBinderFactory.getBinder(null, DirectWithAttributesChannel.class))
			.thenReturn(binder);
	when(mockBinderFactory.getBinder("someTransport",
			DirectWithAttributesChannel.class)).thenReturn(binder2);
	BindingService bindingService = new BindingService(this.bindingServiceProperties,
			mockBinderFactory);
	BinderAwareChannelResolver resolver = new BinderAwareChannelResolver(
			bindingService, this.bindingTargetFactory,
			new DynamicDestinationsBindable());
	resolver.setBeanFactory(this.context.getBeanFactory());
	SubscribableChannel resolved = (SubscribableChannel) resolver
			.resolveDestination("foo");
	verify(binder).bindProducer(eq("foo"), any(MessageChannel.class),
			any(ProducerProperties.class));
	assertThat(resolved).isSameAs(this.context.getBean("foo"));
	this.context.close();
}
 
Example 9
@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 10
@Test
public void testAutoCreateTopicDisabledFailsOnProducerIfTopicNonExistentOnBroker()
		throws Throwable {

	KafkaProperties kafkaProperties = new TestKafkaProperties();
	kafkaProperties.setBootstrapServers(Collections
			.singletonList(embeddedKafka.getEmbeddedKafka().getBrokersAsString()));

	KafkaBinderConfigurationProperties configurationProperties = new KafkaBinderConfigurationProperties(
			kafkaProperties);
	// disable auto create topic on the binder.
	configurationProperties.setAutoCreateTopics(false);
	// reduce the wait time on the producer blocking operations.
	configurationProperties.getConfiguration().put("max.block.ms", "3000");

	KafkaTopicProvisioner provisioningProvider = new KafkaTopicProvisioner(
			configurationProperties, kafkaProperties);
	SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy(1);
	final RetryTemplate metadataRetryOperations = new RetryTemplate();
	metadataRetryOperations.setRetryPolicy(simpleRetryPolicy);
	provisioningProvider.setMetadataRetryOperations(metadataRetryOperations);

	KafkaMessageChannelBinder binder = new KafkaMessageChannelBinder(
			configurationProperties, provisioningProvider);

	final String testTopicName = "nonExistent" + System.currentTimeMillis();

	ExtendedProducerProperties<KafkaProducerProperties> properties = new ExtendedProducerProperties<>(
			new KafkaProducerProperties());

	expectedException.expect(BinderException.class);
	expectedException.expectCause(isA(UnknownTopicOrPartitionException.class));

	binder.bindProducer(testTopicName, new DirectChannel(), properties);

}
 
Example 11
@Test
public void overrideNaturalOutputChannelName() throws Exception {
	this.module.setOutputChannelName("bar");
	this.context.registerSingleton("output.queue:foo", new DirectChannel());
	refresh();
	Collection<OutputChannelBinding> channels = this.adapter.getChannelsMetadata().getOutputChannels();
	assertEquals(1, channels.size());
	assertEquals("foo.bar", channels.iterator().next().getRemoteName());
	// TODO: fix this. What should it be?
	assertEquals("tap:stream:foo.bar.module.0", channels.iterator().next().getTapChannelName());
}
 
Example 12
@Test
public void testOneRequiredGroup() throws Exception {
	B binder = getBinder();
	PP producerProperties = createProducerProperties();
	DirectChannel output = createBindableChannel("output",
			createProducerBindingProperties(producerProperties));

	String testDestination = "testDestination"
			+ UUID.randomUUID().toString().replace("-", "");

	producerProperties.setRequiredGroups("test1");
	Binding<MessageChannel> producerBinding = binder.bindProducer(testDestination,
			output, producerProperties);

	String testPayload = "foo-" + UUID.randomUUID().toString();
	output.send(MessageBuilder.withPayload(testPayload)
			.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN)
			.build());

	QueueChannel inbound1 = new QueueChannel();
	Binding<MessageChannel> consumerBinding = binder.bindConsumer(testDestination,
			"test1", inbound1, createConsumerProperties());

	Message<?> receivedMessage1 = receive(inbound1);
	assertThat(receivedMessage1).isNotNull();
	assertThat(new String((byte[]) receivedMessage1.getPayload()))
			.isEqualTo(testPayload);

	producerBinding.unbind();
	consumerBinding.unbind();
}
 
Example 13
Source Project: spring-cloud-stream   Source File: BindingServiceTests.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testDefaultGroup() throws Exception {
	BindingServiceProperties properties = new BindingServiceProperties();
	Map<String, BindingProperties> bindingProperties = new HashMap<>();
	BindingProperties props = new BindingProperties();
	props.setDestination("foo");
	final String inputChannelName = "input";
	bindingProperties.put(inputChannelName, props);
	properties.setBindings(bindingProperties);
	DefaultBinderFactory binderFactory = createMockBinderFactory();
	Binder binder = binderFactory.getBinder("mock", MessageChannel.class);
	BindingService service = new BindingService(properties, binderFactory);
	MessageChannel inputChannel = new DirectChannel();
	Binding<MessageChannel> mockBinding = Mockito.mock(Binding.class);
	when(binder.bindConsumer(eq("foo"), isNull(), same(inputChannel),
			any(ConsumerProperties.class))).thenReturn(mockBinding);
	Collection<Binding<MessageChannel>> bindings = service.bindConsumer(inputChannel,
			inputChannelName);
	assertThat(bindings).hasSize(1);
	Binding<MessageChannel> binding = bindings.iterator().next();
	assertThat(binding).isSameAs(mockBinding);
	service.unbindConsumers(inputChannelName);
	verify(binder).bindConsumer(eq("foo"), isNull(), same(inputChannel),
			any(ConsumerProperties.class));
	verify(binding).unbind();
	binderFactory.destroy();
}
 
Example 14
@Test
public void overrideNaturalOutputChannelNamedQueueWithTopic() throws Exception {
	this.module.setOutputChannelName("queue:bar");
	this.context.registerSingleton("output.topic:foo", new DirectChannel());
	refresh();
	Collection<OutputChannelBinding> channels = this.adapter.getChannelsMetadata().getOutputChannels();
	assertEquals(1, channels.size());
	assertEquals("topic:foo.bar", channels.iterator().next().getRemoteName());
	assertEquals("tap:stream:foo.bar.module.0", channels.iterator().next().getTapChannelName());
}
 
Example 15
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testPartitionedNative() throws Exception {
	Binder binder = getBinder();
	ExtendedProducerProperties<KafkaProducerProperties> properties = createProducerProperties();
	properties.setPartitionCount(6);

	DirectChannel output = createBindableChannel("output",
			createProducerBindingProperties(properties));
	output.setBeanName("test.output");
	Binding<MessageChannel> outputBinding = binder.bindProducer("partNative.raw.0",
			output, properties);

	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();
	QueueChannel input0 = new QueueChannel();
	input0.setBeanName("test.inputNative");
	Binding<MessageChannel> inputBinding = binder.bindConsumer("partNative.raw.0",
			"test", input0, consumerProperties);

	output.send(new GenericMessage<>("foo".getBytes(),
			Collections.singletonMap(KafkaHeaders.PARTITION_ID, 5)));

	Message<?> received = receive(input0);
	assertThat(received).isNotNull();

	assertThat(received.getPayload()).isEqualTo("foo".getBytes());
	assertThat(received.getHeaders().get(KafkaHeaders.RECEIVED_PARTITION_ID))
			.isEqualTo(5);

	inputBinding.unbind();
	outputBinding.unbind();
}
 
Example 16
@Test
public void oneOutput() throws Exception {
	this.context.registerSingleton("output", new DirectChannel());
	refresh();
	Collection<OutputChannelBinding> channels = this.adapter.getChannelsMetadata().getOutputChannels();
	assertEquals(1, channels.size());
	assertEquals("group.0", channels.iterator().next().getRemoteName());
	assertEquals("tap:stream:group.module.0", channels.iterator().next().getTapChannelName());
}
 
Example 17
@Test
@SuppressWarnings("unchecked")
public void testAutoCreateTopicsEnabledSucceeds() throws Exception {
	KafkaBinderConfigurationProperties configurationProperties = createConfigurationProperties();
	configurationProperties.setAutoCreateTopics(true);
	Binder binder = getBinder(configurationProperties);
	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();
	String testTopicName = "nonexisting" + System.currentTimeMillis();
	DirectChannel moduleInputChannel = createBindableChannel("input",
			createConsumerBindingProperties(consumerProperties));

	Binding<?> binding = binder.bindConsumer(testTopicName, "test",
			moduleInputChannel, consumerProperties);
	binding.unbind();
}
 
Example 18
@Test
@SuppressWarnings("unchecked")
public void testCustomPartitionCountOverridesDefaultIfLarger() throws Exception {
	byte[] testPayload = new byte[2048];
	Arrays.fill(testPayload, (byte) 65);
	KafkaBinderConfigurationProperties binderConfiguration = createConfigurationProperties();
	binderConfiguration.setMinPartitionCount(10);
	Binder binder = getBinder(binderConfiguration);
	QueueChannel moduleInputChannel = new QueueChannel();
	ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties();
	producerProperties.setPartitionCount(10);
	producerProperties.setPartitionKeyExpression(new LiteralExpression("foo"));

	DirectChannel moduleOutputChannel = createBindableChannel("output",
			createProducerBindingProperties(producerProperties));

	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();
	long uniqueBindingId = System.currentTimeMillis();
	Binding<MessageChannel> producerBinding = binder.bindProducer(
			"foo" + uniqueBindingId + ".0", moduleOutputChannel, producerProperties);
	Binding<MessageChannel> consumerBinding = binder.bindConsumer(
			"foo" + uniqueBindingId + ".0", null, moduleInputChannel,
			consumerProperties);
	Message<?> message = org.springframework.integration.support.MessageBuilder
			.withPayload(testPayload).build();
	// Let the consumer actually bind to the producer before sending a msg
	binderBindUnbindLatency();
	moduleOutputChannel.send(message);
	Message<?> inbound = receive(moduleInputChannel);
	assertThat(inbound).isNotNull();
	assertThat((byte[]) inbound.getPayload()).containsExactly(testPayload);

	assertThat(partitionSize("foo" + uniqueBindingId + ".0")).isEqualTo(10);
	producerBinding.unbind();
	consumerBinding.unbind();
}
 
Example 19
Source Project: spring-cloud-stream   Source File: BindingServiceTests.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testExplicitGroup() throws Exception {
	BindingServiceProperties properties = new BindingServiceProperties();
	Map<String, BindingProperties> bindingProperties = new HashMap<>();
	BindingProperties props = new BindingProperties();
	props.setDestination("foo");
	props.setGroup("fooGroup");
	final String inputChannelName = "input";
	bindingProperties.put(inputChannelName, props);
	properties.setBindings(bindingProperties);
	DefaultBinderFactory binderFactory = createMockBinderFactory();
	Binder binder = binderFactory.getBinder("mock", MessageChannel.class);
	BindingService service = new BindingService(properties, binderFactory);
	MessageChannel inputChannel = new DirectChannel();
	Binding<MessageChannel> mockBinding = Mockito.mock(Binding.class);
	when(binder.bindConsumer(eq("foo"), eq("fooGroup"), same(inputChannel),
			any(ConsumerProperties.class))).thenReturn(mockBinding);
	Collection<Binding<MessageChannel>> bindings = service.bindConsumer(inputChannel,
			inputChannelName);
	assertThat(bindings).hasSize(1);
	Binding<MessageChannel> binding = bindings.iterator().next();
	assertThat(binding).isSameAs(mockBinding);

	service.unbindConsumers(inputChannelName);
	verify(binder).bindConsumer(eq("foo"), eq(props.getGroup()), same(inputChannel),
			any(ConsumerProperties.class));
	verify(binding).unbind();
	binderFactory.destroy();
}
 
Example 20
@Test
@SuppressWarnings("unchecked")
public void testAutoAddPartitionsDisabledFailsIfTopicUnderPartitionedAndAutoRebalanceDisabled()
		throws Throwable {
	KafkaBinderConfigurationProperties configurationProperties = createConfigurationProperties();

	String testTopicName = "existing" + System.currentTimeMillis();
	invokeCreateTopic(testTopicName, 1, 1);
	configurationProperties.setAutoAddPartitions(false);
	Binder binder = getBinder(configurationProperties);
	GenericApplicationContext context = new GenericApplicationContext();
	context.refresh();

	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();
	DirectChannel output = createBindableChannel("output",
			createConsumerBindingProperties(consumerProperties));
	// this consumer must consume from partition 2
	consumerProperties.setInstanceCount(3);
	consumerProperties.setInstanceIndex(2);
	consumerProperties.getExtension().setAutoRebalanceEnabled(false);
	expectedProvisioningException.expect(ProvisioningException.class);
	expectedProvisioningException.expectMessage(
			"The number of expected partitions was: 3, but 1 has been found instead");
	Binding binding = binder.bindConsumer(testTopicName, "test", output,
			consumerProperties);
	if (binding != null) {
		binding.unbind();
	}
}
 
Example 21
public void testCustomPartitionedProducerMultipleInstances() {
	ApplicationContext context = SpringApplication.run(
			CustomPartitionedProducerTest.TestSourceMultipleStrategies.class,
			"--spring.jmx.enabled=false", "--spring.main.web-application-type=none",
			"--spring.cloud.stream.bindings.output.producer.partitionKeyExtractorName=customPartitionKeyExtractorOne",
			"--spring.cloud.stream.bindings.output.producer.partitionSelectorName=customPartitionSelectorTwo",
			"--spring.cloud.stream.default-binder=mock");
	Source testSource = context.getBean(Source.class);
	DirectChannel messageChannel = (DirectChannel) testSource.output();
	for (ChannelInterceptor channelInterceptor : messageChannel
			.getInterceptors()) {
		if (channelInterceptor instanceof MessageConverterConfigurer.PartitioningInterceptor) {
			Field partitionHandlerField = ReflectionUtils.findField(
					MessageConverterConfigurer.PartitioningInterceptor.class,
					"partitionHandler");
			ReflectionUtils.makeAccessible(partitionHandlerField);
			PartitionHandler partitionHandler = (PartitionHandler) ReflectionUtils
					.getField(partitionHandlerField, channelInterceptor);
			Field partitonKeyExtractorField = ReflectionUtils.findField(
					PartitionHandler.class, "partitionKeyExtractorStrategy");
			ReflectionUtils.makeAccessible(partitonKeyExtractorField);
			Field partitonSelectorField = ReflectionUtils
					.findField(PartitionHandler.class, "partitionSelectorStrategy");
			ReflectionUtils.makeAccessible(partitonSelectorField);
			assertThat(((PartitionKeyExtractorStrategy) ReflectionUtils
					.getField(partitonKeyExtractorField, partitionHandler)).getClass()
							.equals(CustomPartitionKeyExtractorClass.class)).isTrue();
			assertThat(((PartitionSelectorStrategy) ReflectionUtils
					.getField(partitonSelectorField, partitionHandler)).getClass()
							.equals(CustomPartitionSelectorClass.class)).isTrue();
		}
	}
}
 
Example 22
@Test
@SuppressWarnings("unchecked")
public void testConsumerDefaultDeserializer() throws Throwable {
	Binding<?> binding = null;
	try {
		KafkaBinderConfigurationProperties configurationProperties = createConfigurationProperties();
		String testTopicName = "existing" + System.currentTimeMillis();
		invokeCreateTopic(testTopicName, 5, 1);
		configurationProperties.setAutoCreateTopics(false);
		Binder binder = getBinder(configurationProperties);

		ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();
		DirectChannel input = createBindableChannel("input",
				createConsumerBindingProperties(consumerProperties));

		binding = binder.bindConsumer(testTopicName, "test", input,
				consumerProperties);
		DirectFieldAccessor consumerAccessor = new DirectFieldAccessor(
				getKafkaConsumer(binding));
		assertThat(consumerAccessor
				.getPropertyValue("keyDeserializer") instanceof ByteArrayDeserializer)
						.isTrue();
		assertThat(consumerAccessor.getPropertyValue(
				"valueDeserializer") instanceof ByteArrayDeserializer).isTrue();
	}
	finally {
		if (binding != null) {
			binding.unbind();
		}
	}
}
 
Example 23
@Test
public void twoOutputsWithQueue() throws Exception {
	this.context.registerSingleton("output", new DirectChannel());
	this.context.registerSingleton("output.queue:foo", new DirectChannel());
	refresh();
	Collection<OutputChannelBinding> channels = this.adapter.getChannelsMetadata().getOutputChannels();
	List<String> names = getChannelNames(channels);
	assertEquals(2, channels.size());
	assertTrue(names.contains("group.0"));
	assertTrue(names.contains("foo.group.0"));
}
 
Example 24
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testTopicPatterns() throws Exception {
	try (AdminClient admin = AdminClient.create(
			Collections.singletonMap(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,
					embeddedKafka.getEmbeddedKafka().getBrokersAsString()))) {
		admin.createTopics(Collections
				.singletonList(new NewTopic("topicPatterns.1", 1, (short) 1))).all()
				.get();
		Binder binder = getBinder();
		ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();
		consumerProperties.getExtension().setDestinationIsPattern(true);
		DirectChannel moduleInputChannel = createBindableChannel("input",
				createConsumerBindingProperties(consumerProperties));
		final CountDownLatch latch = new CountDownLatch(1);
		final AtomicReference<String> topic = new AtomicReference<>();
		moduleInputChannel.subscribe(m -> {
			topic.set(m.getHeaders().get(KafkaHeaders.RECEIVED_TOPIC, String.class));
			latch.countDown();
		});
		Binding<MessageChannel> consumerBinding = binder.bindConsumer(
				"topicPatterns\\..*", "testTopicPatterns", moduleInputChannel,
				consumerProperties);
		DefaultKafkaProducerFactory pf = new DefaultKafkaProducerFactory(
				KafkaTestUtils.producerProps(embeddedKafka.getEmbeddedKafka()));
		KafkaTemplate template = new KafkaTemplate(pf);
		template.send("topicPatterns.1", "foo");
		assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
		assertThat(topic.get()).isEqualTo("topicPatterns.1");
		consumerBinding.unbind();
		pf.destroy();
	}
}
 
Example 25
@Test
public void testCustomPartitionedProducerAsSingletons() {
	ApplicationContext context = SpringApplication.run(
			CustomPartitionedProducerTest.TestSource.class,
			"--spring.jmx.enabled=false", "--spring.main.web-application-type=none",
			"--spring.cloud.stream.default-binder=mock");
	Source testSource = context.getBean(Source.class);
	DirectChannel messageChannel = (DirectChannel) testSource.output();
	for (ChannelInterceptor channelInterceptor : messageChannel
			.getInterceptors()) {
		if (channelInterceptor instanceof MessageConverterConfigurer.PartitioningInterceptor) {
			Field partitionHandlerField = ReflectionUtils.findField(
					MessageConverterConfigurer.PartitioningInterceptor.class,
					"partitionHandler");
			ReflectionUtils.makeAccessible(partitionHandlerField);
			PartitionHandler partitionHandler = (PartitionHandler) ReflectionUtils
					.getField(partitionHandlerField, channelInterceptor);
			Field partitonKeyExtractorField = ReflectionUtils.findField(
					PartitionHandler.class, "partitionKeyExtractorStrategy");
			ReflectionUtils.makeAccessible(partitonKeyExtractorField);
			Field partitonSelectorField = ReflectionUtils
					.findField(PartitionHandler.class, "partitionSelectorStrategy");
			ReflectionUtils.makeAccessible(partitonSelectorField);
			assertThat(((PartitionKeyExtractorStrategy) ReflectionUtils
					.getField(partitonKeyExtractorField, partitionHandler)).getClass()
							.equals(CustomPartitionKeyExtractorClass.class)).isTrue();
			assertThat(((PartitionSelectorStrategy) ReflectionUtils
					.getField(partitonSelectorField, partitionHandler)).getClass()
							.equals(CustomPartitionSelectorClass.class)).isTrue();
		}
	}
}
 
Example 26
@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();
}
 
Example 27
@Bean(TransformerApplication.TRANSFORMER_INPUT)
public MessageChannel successChannel() {
	return new DirectChannel();
}
 
Example 28
@Bean
DirectChannel directChannel() {
	return new DirectChannel();
}
 
Example 29
Source Project: spring-cloud-stream   Source File: AbstractBinderTests.java    License: Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
@Test
public void testSendJsonReceiveJsonWithStreamListener() throws Exception {
	StreamListenerMessageHandler handler = this.buildStreamListener(
			AbstractBinderTests.class, "echoStationString", String.class);
	Binder binder = getBinder();

	BindingProperties producerBindingProperties = createProducerBindingProperties(
			createProducerProperties());

	DirectChannel moduleOutputChannel = createBindableChannel("output",
			producerBindingProperties);

	BindingProperties consumerBindingProperties = createConsumerBindingProperties(
			createConsumerProperties());

	DirectChannel moduleInputChannel = createBindableChannel("input",
			consumerBindingProperties);

	Binding<MessageChannel> producerBinding = binder.bindProducer(
			String.format("bad%s0e", getDestinationNameDelimiter()),
			moduleOutputChannel, producerBindingProperties.getProducer());

	Binding<MessageChannel> consumerBinding = binder.bindConsumer(
			String.format("bad%s0e", getDestinationNameDelimiter()), "test-5",
			moduleInputChannel, consumerBindingProperties.getConsumer());

	String value = "{\"readings\":[{\"stationid\":\"fgh\","
			+ "\"customerid\":\"12345\",\"timestamp\":null},"
			+ "{\"stationid\":\"hjk\",\"customerid\":\"222\",\"timestamp\":null}]}";

	Message<?> message = MessageBuilder.withPayload(value)
			.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
			.build();
	moduleInputChannel.subscribe(handler);
	moduleOutputChannel.send(message);

	QueueChannel channel = (QueueChannel) handler.getOutputChannel();

	Message<String> reply = (Message<String>) channel.receive(5000);

	assertThat(reply).isNotNull();
	assertThat(reply.getPayload() instanceof String).isTrue();
	producerBinding.unbind();
	consumerBinding.unbind();
}
 
Example 30
@SuppressWarnings("unchecked")
@Test
public void testBatchingAndCompression() throws Exception {
	RabbitTestBinder binder = getBinder();
	ExtendedProducerProperties<RabbitProducerProperties> producerProperties = createProducerProperties();
	producerProperties.getExtension()
			.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
	producerProperties.getExtension().setBatchingEnabled(true);
	producerProperties.getExtension().setBatchSize(2);
	producerProperties.getExtension().setBatchBufferLimit(100000);
	producerProperties.getExtension().setBatchTimeout(30000);
	producerProperties.getExtension().setCompress(true);
	producerProperties.setRequiredGroups("default");

	DirectChannel output = createBindableChannel("output",
			createProducerBindingProperties(producerProperties));
	output.setBeanName("batchingProducer");
	Binding<MessageChannel> producerBinding = binder.bindProducer("batching.0",
			output, producerProperties);

	Log logger = spy(TestUtils.getPropertyValue(binder,
			"binder.compressingPostProcessor.logger", Log.class));
	new DirectFieldAccessor(
			TestUtils.getPropertyValue(binder, "binder.compressingPostProcessor"))
					.setPropertyValue("logger", logger);
	when(logger.isTraceEnabled()).thenReturn(true);

	assertThat(TestUtils.getPropertyValue(binder,
			"binder.compressingPostProcessor.level")).isEqualTo(Deflater.BEST_SPEED);

	output.send(new GenericMessage<>("foo".getBytes()));
	output.send(new GenericMessage<>("bar".getBytes()));

	Object out = spyOn("batching.0.default").receive(false);
	assertThat(out).isInstanceOf(byte[].class);
	assertThat(new String((byte[]) out))
			.isEqualTo("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar");

	ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class);
	verify(logger).trace(captor.capture());
	assertThat(captor.getValue().toString()).contains(("Compressed 14 to "));

	QueueChannel input = new QueueChannel();
	input.setBeanName("batchingConsumer");
	Binding<MessageChannel> consumerBinding = binder.bindConsumer("batching.0",
			"test", input, createConsumerProperties());

	output.send(new GenericMessage<>("foo".getBytes()));
	output.send(new GenericMessage<>("bar".getBytes()));

	Message<byte[]> in = (Message<byte[]>) input.receive(10000);
	assertThat(in).isNotNull();
	assertThat(new String(in.getPayload())).isEqualTo("foo");
	in = (Message<byte[]>) input.receive(10000);
	assertThat(in).isNotNull();
	assertThat(new String(in.getPayload())).isEqualTo("bar");
	assertThat(in.getHeaders().get(AmqpHeaders.DELIVERY_MODE)).isNull();

	producerBinding.unbind();
	consumerBinding.unbind();
}