Java Code Examples for org.springframework.cloud.stream.binder.Binder

The following examples show how to use org.springframework.cloud.stream.binder.Binder. 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
@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 2
@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 3
@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 4
@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 5
@Test
@SuppressWarnings("unchecked")
public void testPartitionCountNotReduced() throws Throwable {
	String testTopicName = "existing" + System.currentTimeMillis();

	KafkaBinderConfigurationProperties configurationProperties = createConfigurationProperties();

	invokeCreateTopic(testTopicName, 6, 1);
	configurationProperties.setAutoAddPartitions(true);
	Binder binder = getBinder(configurationProperties);
	GenericApplicationContext context = new GenericApplicationContext();
	context.refresh();

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

	Binding<?> binding = binder.bindConsumer(testTopicName, "test", input,
			consumerProperties);
	binding.unbind();

	assertThat(partitionSize(testTopicName)).isEqualTo(6);
}
 
Example 6
@Test
public void testNoBrokerOverride() throws Exception {
	Binder binder = getBinder();
	ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties();
	producerProperties.getExtension().getConfiguration().put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "foo");
	BindingProperties outputBindingProperties = createProducerBindingProperties(producerProperties);
	DirectChannel moduleOutputChannel = createBindableChannel("output", outputBindingProperties);
	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();
	consumerProperties.getExtension().getConfiguration().put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "foo");
	BindingProperties consumerBindingProperties = createConsumerBindingProperties(consumerProperties);
	DirectChannel moduleInputChannel = createBindableChannel("input", consumerBindingProperties);

	assertThatExceptionOfType(BinderException.class).isThrownBy(() -> binder.bindProducer("foo.bar",
				moduleOutputChannel, outputBindingProperties.getProducer()))
			.withCauseExactlyInstanceOf(IllegalStateException.class);
	assertThatExceptionOfType(BinderException.class).isThrownBy(() -> binder.bindConsumer("foo.bar",
				"testSendAndReceive", moduleInputChannel, consumerProperties))
			.withCauseExactlyInstanceOf(IllegalStateException.class);
}
 
Example 7
@Test
public void contextLoads() {
	Binder<MessageChannel, ?, ?> binder1 = binderFactory.getBinder("kafka1", MessageChannel.class);
	KafkaMessageChannelBinder kafka1 = (KafkaMessageChannelBinder) binder1;
	DirectFieldAccessor directFieldAccessor1 = new DirectFieldAccessor(kafka1);
	KafkaBinderConfigurationProperties configuration1 =
			(KafkaBinderConfigurationProperties) directFieldAccessor1.getPropertyValue("configurationProperties");
	Assert.assertThat(configuration1.getBrokers(), arrayWithSize(1));
	Assert.assertThat(configuration1.getBrokers()[0], equalTo(kafkaTestSupport1.getEmbeddedKafka().getBrokersAsString()));

	Binder<MessageChannel, ?, ?> binder2 = binderFactory.getBinder("kafka2", MessageChannel.class);
	KafkaMessageChannelBinder kafka2 = (KafkaMessageChannelBinder) binder2;
	DirectFieldAccessor directFieldAccessor2 = new DirectFieldAccessor(kafka2);
	KafkaBinderConfigurationProperties configuration2 =
			(KafkaBinderConfigurationProperties) directFieldAccessor2.getPropertyValue("configurationProperties");
	Assert.assertThat(configuration2.getBrokers(), arrayWithSize(1));
	Assert.assertThat(configuration2.getBrokers()[0], equalTo(kafkaTestSupport2.getEmbeddedKafka().getBrokersAsString()));
}
 
Example 8
@Test
public void contextLoads() {
	Binder<MessageChannel, ?, ?> binder1 = binderFactory.getBinder("kafka1", MessageChannel.class);
	KafkaMessageChannelBinder kafka1 = (KafkaMessageChannelBinder) binder1;
	DirectFieldAccessor directFieldAccessor1 = new DirectFieldAccessor(kafka1);
	KafkaBinderConfigurationProperties configuration1 =
			(KafkaBinderConfigurationProperties) directFieldAccessor1.getPropertyValue("configurationProperties");
	Assert.assertThat(configuration1.getBrokers(), arrayWithSize(1));
	Assert.assertThat(configuration1.getBrokers()[0], equalTo(kafkaTestSupport1.getEmbeddedKafka().getBrokersAsString()));

	Binder<MessageChannel, ?, ?> binder2 = binderFactory.getBinder("kafka2", MessageChannel.class);
	KafkaMessageChannelBinder kafka2 = (KafkaMessageChannelBinder) binder2;
	DirectFieldAccessor directFieldAccessor2 = new DirectFieldAccessor(kafka2);
	KafkaBinderConfigurationProperties configuration2 =
			(KafkaBinderConfigurationProperties) directFieldAccessor2.getPropertyValue("configurationProperties");
	Assert.assertThat(configuration2.getBrokers(), arrayWithSize(1));
	Assert.assertThat(configuration2.getBrokers()[0], equalTo(kafkaTestSupport2.getEmbeddedKafka().getBrokersAsString()));
}
 
Example 9
@Test
public void testParentConnectionFactoryInheritedIfOverridden() {
	context = new SpringApplicationBuilder(SimpleProcessor.class,
			ConnectionFactoryConfiguration.class).web(WebApplicationType.NONE)
					.run("--server.port=0");
	BinderFactory binderFactory = context.getBean(BinderFactory.class);
	Binder<?, ?, ?> binder = binderFactory.getBinder(null, MessageChannel.class);
	assertThat(binder).isInstanceOf(RabbitMessageChannelBinder.class);
	DirectFieldAccessor binderFieldAccessor = new DirectFieldAccessor(binder);
	ConnectionFactory binderConnectionFactory = (ConnectionFactory) binderFieldAccessor
			.getPropertyValue("connectionFactory");
	assertThat(binderConnectionFactory).isSameAs(MOCK_CONNECTION_FACTORY);
	ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
	assertThat(binderConnectionFactory).isSameAs(connectionFactory);
	CompositeHealthContributor bindersHealthIndicator = context
			.getBean("bindersHealthContributor", CompositeHealthContributor.class);
	assertThat(bindersHealthIndicator).isNotNull();
	RabbitHealthIndicator indicator = (RabbitHealthIndicator) bindersHealthIndicator.getContributor("rabbit");
	assertThat(indicator).isNotNull();
	// mock connection factory behaves as if down
	assertThat(indicator.health().getStatus())
			.isEqualTo(Status.DOWN);
}
 
Example 10
@Test
public void testCloudProfile() {
	this.context = new SpringApplicationBuilder(SimpleProcessor.class,
			MockCloudConfiguration.class).web(WebApplicationType.NONE)
					.profiles("cloud").run();
	BinderFactory binderFactory = this.context.getBean(BinderFactory.class);
	Binder<?, ?, ?> binder = binderFactory.getBinder(null, MessageChannel.class);
	assertThat(binder).isInstanceOf(RabbitMessageChannelBinder.class);
	DirectFieldAccessor binderFieldAccessor = new DirectFieldAccessor(binder);
	ConnectionFactory binderConnectionFactory = (ConnectionFactory) binderFieldAccessor
			.getPropertyValue("connectionFactory");
	ConnectionFactory connectionFactory = this.context
			.getBean(ConnectionFactory.class);

	assertThat(binderConnectionFactory).isNotSameAs(connectionFactory);

	assertThat(TestUtils.getPropertyValue(connectionFactory, "addresses"))
			.isNotNull();
	assertThat(TestUtils.getPropertyValue(binderConnectionFactory, "addresses"))
			.isNull();

	Cloud cloud = this.context.getBean(Cloud.class);

	verify(cloud).getSingletonServiceConnector(ConnectionFactory.class, null);
}
 
Example 11
Source Project: spring-cloud-stream   Source File: GenericsUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Return the generic type of PollableSource to determine if it is appropriate for the
 * binder. e.g., with PollableMessageSource extends
 * PollableSource&lt;MessageHandler&gt; and AbstractMessageChannelBinder implements
 * PollableConsumerBinder&lt;MessageHandler, C&gt; We're checking that the the generic
 * type (MessageHandler) matches.
 * @param binderInstance the binder.
 * @param bindingTargetType the binding target type.
 * @return true if found, false otherwise.
 */
@SuppressWarnings("rawtypes")
public static boolean checkCompatiblePollableBinder(Binder binderInstance,
		Class<?> bindingTargetType) {
	Class<?>[] binderInterfaces = ClassUtils.getAllInterfaces(binderInstance);
	for (Class<?> intf : binderInterfaces) {
		if (PollableConsumerBinder.class.isAssignableFrom(intf)) {
			Class<?>[] targetInterfaces = ClassUtils
					.getAllInterfacesForClass(bindingTargetType);
			Class<?> psType = findPollableSourceType(targetInterfaces);
			if (psType != null) {
				return getParameterType(binderInstance.getClass(), intf, 0)
						.isAssignableFrom(psType);
			}
		}
	}
	return false;
}
 
Example 12
Source Project: spring-cloud-stream   Source File: BindingService.java    License: Apache License 2.0 6 votes vote down vote up
public <T> Binding<T> doBindConsumer(T input, String inputName,
		Binder<T, ConsumerProperties, ?> binder,
		ConsumerProperties consumerProperties, String target) {
	if (this.taskScheduler == null
			|| this.bindingServiceProperties.getBindingRetryInterval() <= 0) {
		return binder.bindConsumer(target,
				this.bindingServiceProperties.getGroup(inputName), input,
				consumerProperties);
	}
	else {
		try {
			return binder.bindConsumer(target,
					this.bindingServiceProperties.getGroup(inputName), input,
					consumerProperties);
		}
		catch (RuntimeException e) {
			LateBinding<T> late = new LateBinding<T>(target,
					e.getCause() == null ? e.toString() : e.getCause().getMessage(), consumerProperties, true);
			rescheduleConsumerBinding(input, inputName, binder, consumerProperties,
					target, late, e);
			this.consumerBindings.put(inputName, Collections.singletonList(late));
			return late;
		}
	}
}
 
Example 13
Source Project: spring-cloud-stream   Source File: BindingService.java    License: Apache License 2.0 6 votes vote down vote up
public <T> void rescheduleConsumerBinding(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(binder.bindConsumer(target,
					this.bindingServiceProperties.getGroup(inputName), input,
					consumerProperties));
		}
		catch (RuntimeException e) {
			rescheduleConsumerBinding(input, inputName, binder, consumerProperties,
					target, late, e);
		}
	});
}
 
Example 14
Source Project: spring-cloud-stream   Source File: BindingService.java    License: 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 15
Source Project: spring-cloud-stream   Source File: BindingService.java    License: 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 16
Source Project: spring-cloud-stream   Source File: BindingService.java    License: Apache License 2.0 6 votes vote down vote up
public <T> Binding<T> doBindProducer(T output, String bindingTarget,
		Binder<T, ?, ProducerProperties> binder,
		ProducerProperties producerProperties) {
	if (this.taskScheduler == null
			|| this.bindingServiceProperties.getBindingRetryInterval() <= 0) {
		return binder.bindProducer(bindingTarget, output, producerProperties);
	}
	else {
		try {
			return binder.bindProducer(bindingTarget, output, producerProperties);
		}
		catch (RuntimeException e) {
			LateBinding<T> late = new LateBinding<T>(bindingTarget,
					e.getCause() == null ? e.toString() : e.getCause().getMessage(), producerProperties, false);
			rescheduleProducerBinding(output, bindingTarget, binder,
					producerProperties, late, e);
			return late;
		}
	}
}
 
Example 17
Source Project: spring-cloud-stream   Source File: BindingService.java    License: Apache License 2.0 6 votes vote down vote up
public <T> void rescheduleProducerBinding(final T output, final String bindingTarget,
		final Binder<T, ?, ProducerProperties> binder,
		final ProducerProperties producerProperties, final LateBinding<T> late,
		final RuntimeException exception) {
	assertNotIllegalException(exception);
	this.log.error("Failed to create producer binding; retrying in "
			+ this.bindingServiceProperties.getBindingRetryInterval() + " seconds",
			exception);
	this.scheduleTask(() -> {
		try {
			late.setDelegate(
					binder.bindProducer(bindingTarget, output, producerProperties));
		}
		catch (RuntimeException e) {
			rescheduleProducerBinding(output, bindingTarget, binder,
					producerProperties, late, e);
		}
	});
}
 
Example 18
@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 19
@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 20
@Bean
@SuppressWarnings("unchecked")
public BinderFactory binderFactory(final Binder<MessageChannel, ?, ?> binder) {
	return new BinderFactory() {
		@Override
		public <T> Binder<T, ? extends ConsumerProperties, ? extends ProducerProperties> getBinder(
				String configurationName, Class<? extends T> bindableType) {
			return (Binder<T, ? extends ConsumerProperties, ? extends ProducerProperties>) binder;
		}
	};
}
 
Example 21
@Test
@SuppressWarnings("unchecked")
public void testCustomPartitionCountDoesNotOverridePartitioningIfSmaller()
		throws Exception {
	byte[] testPayload = new byte[2048];
	Arrays.fill(testPayload, (byte) 65);
	KafkaBinderConfigurationProperties binderConfiguration = createConfigurationProperties();
	binderConfiguration.setMinPartitionCount(6);
	Binder binder = getBinder(binderConfiguration);
	QueueChannel moduleInputChannel = new QueueChannel();
	ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties();
	producerProperties.setPartitionCount(5);
	producerProperties.setPartitionKeyExpression(
			spelExpressionParser.parseExpression("payload"));
	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();
	long uniqueBindingId = System.currentTimeMillis();
	DirectChannel moduleOutputChannel = createBindableChannel("output",
			createProducerBindingProperties(producerProperties));
	Binding<MessageChannel> producerBinding = binder.bindProducer(
			"foo" + uniqueBindingId + ".0", moduleOutputChannel, producerProperties);
	Binding<MessageChannel> consumerBinding = binder.bindConsumer(
			"foo" + uniqueBindingId + ".0", null, moduleInputChannel,
			consumerProperties);
	Thread.sleep(1000);
	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(6);
	producerBinding.unbind();
	consumerBinding.unbind();
}
 
Example 22
@Test
@SuppressWarnings("unchecked")
public void testDynamicKeyExpression() throws Exception {
	Binder binder = getBinder(createConfigurationProperties());
	QueueChannel moduleInputChannel = new QueueChannel();
	ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties();
	producerProperties.getExtension().getConfiguration().put("key.serializer",
			StringSerializer.class.getName());
	producerProperties.getExtension().setMessageKeyExpression(
			spelExpressionParser.parseExpression("headers.key"));
	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();
	String uniqueBindingId = UUID.randomUUID().toString();
	DirectChannel moduleOutputChannel = createBindableChannel("output",
			createProducerBindingProperties(producerProperties));
	Binding<MessageChannel> producerBinding = binder.bindProducer(
			"foo" + uniqueBindingId + ".0", moduleOutputChannel, producerProperties);
	Binding<MessageChannel> consumerBinding = binder.bindConsumer(
			"foo" + uniqueBindingId + ".0", null, moduleInputChannel,
			consumerProperties);
	Thread.sleep(1000);
	Message<?> message = MessageBuilder.withPayload("somePayload")
			.setHeader("key", "myDynamicKey").build();
	// Let the consumer actually bind to the producer before sending a msg
	binderBindUnbindLatency();
	moduleOutputChannel.send(message);
	Message<?> inbound = receive(moduleInputChannel);
	assertThat(inbound).isNotNull();
	String receivedKey = new String(inbound.getHeaders()
			.get(KafkaHeaders.RECEIVED_MESSAGE_KEY, byte[].class));
	assertThat(receivedKey).isEqualTo("myDynamicKey");
	producerBinding.unbind();
	consumerBinding.unbind();
}
 
Example 23
@Test
@SuppressWarnings("unchecked")
public void testCustomPartitionCountOverridesPartitioningIfLarger() throws Exception {
	byte[] testPayload = new byte[2048];
	Arrays.fill(testPayload, (byte) 65);
	KafkaBinderConfigurationProperties binderConfiguration = createConfigurationProperties();
	binderConfiguration.setMinPartitionCount(4);
	Binder binder = getBinder(binderConfiguration);

	QueueChannel moduleInputChannel = new QueueChannel();
	ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties();
	producerProperties.setPartitionCount(5);
	producerProperties.setPartitionKeyExpression(
			spelExpressionParser.parseExpression("payload"));
	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(5);
	producerBinding.unbind();
	consumerBinding.unbind();
}
 
Example 24
@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 25
@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 26
@Test
@SuppressWarnings("unchecked")
public void testConsumerCustomDeserializer() throws Exception {
	Binding<?> binding = null;
	try {
		KafkaBinderConfigurationProperties configurationProperties = createConfigurationProperties();
		Map<String, String> propertiesToOverride = configurationProperties
				.getConfiguration();
		propertiesToOverride.put("key.deserializer",
				"org.apache.kafka.common.serialization.StringDeserializer");
		propertiesToOverride.put("value.deserializer",
				"org.apache.kafka.common.serialization.LongDeserializer");
		configurationProperties.setConfiguration(propertiesToOverride);
		String testTopicName = "existing" + System.currentTimeMillis();
		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 StringDeserializer)
						.isTrue();
		assertThat(consumerAccessor
				.getPropertyValue("valueDeserializer") instanceof LongDeserializer)
						.isTrue();
	}
	finally {
		if (binding != null) {
			binding.unbind();
		}
	}
}
 
Example 27
@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 28
@Test
public void testExtendedProperties() {
	context = new SpringApplicationBuilder(SimpleProcessor.class)
			.web(WebApplicationType.NONE).run("--server.port=0",
					"--spring.cloud.stream.rabbit.default.producer.routing-key-expression=fooRoutingKey",
					"--spring.cloud.stream.rabbit.default.consumer.exchange-type=direct",
					"--spring.cloud.stream.rabbit.bindings.output.producer.batch-size=512",
					"--spring.cloud.stream.rabbit.default.consumer.max-concurrency=4",
					"--spring.cloud.stream.rabbit.bindings.input.consumer.exchange-type=fanout");
	BinderFactory binderFactory = context.getBean(BinderFactory.class);
	Binder<?, ?, ?> rabbitBinder = binderFactory.getBinder(null,
			MessageChannel.class);

	RabbitProducerProperties rabbitProducerProperties = (RabbitProducerProperties) ((ExtendedPropertiesBinder) rabbitBinder)
			.getExtendedProducerProperties("output");

	assertThat(
			rabbitProducerProperties.getRoutingKeyExpression().getExpressionString())
					.isEqualTo("fooRoutingKey");
	assertThat(rabbitProducerProperties.getBatchSize()).isEqualTo(512);

	RabbitConsumerProperties rabbitConsumerProperties = (RabbitConsumerProperties) ((ExtendedPropertiesBinder) rabbitBinder)
			.getExtendedConsumerProperties("input");

	assertThat(rabbitConsumerProperties.getExchangeType())
			.isEqualTo(ExchangeTypes.FANOUT);
	assertThat(rabbitConsumerProperties.getMaxConcurrency()).isEqualTo(4);
}
 
Example 29
Source Project: spring-cloud-stream   Source File: BindingService.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> Binding<T> bindProducer(T output, String outputName, boolean cache) {
	String bindingTarget = this.bindingServiceProperties
			.getBindingDestination(outputName);
	Class<?> outputClass = output.getClass();
	if (output instanceof Advised) {
		outputClass = Stream.of(((Advised) output).getProxiedInterfaces()).filter(c -> !c.getName().contains("org.springframework")).findFirst()
				.orElse(outputClass);
	}
	Binder<T, ?, ProducerProperties> binder = (Binder<T, ?, ProducerProperties>) getBinder(
			outputName, outputClass);
	ProducerProperties producerProperties = this.bindingServiceProperties
			.getProducerProperties(outputName);
	if (binder instanceof ExtendedPropertiesBinder) {
		Object extension = ((ExtendedPropertiesBinder) binder)
				.getExtendedProducerProperties(outputName);
		ExtendedProducerProperties extendedProducerProperties = new ExtendedProducerProperties<>(
				extension);
		BeanUtils.copyProperties(producerProperties, extendedProducerProperties);

		producerProperties = extendedProducerProperties;
	}
	validate(producerProperties);
	Binding<T> binding = doBindProducer(output, bindingTarget, binder,
			producerProperties);
	if (cache) {
		this.producerBindings.put(outputName, binding);
	}
	return binding;
}
 
Example 30
Source Project: spring-cloud-stream   Source File: BindingService.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
public Object getExtendedProducerProperties(Object output, String outputName) {
	Binder binder = getBinder(outputName, output.getClass());
	if (binder instanceof ExtendedPropertiesBinder) {
		return ((ExtendedPropertiesBinder) binder)
				.getExtendedProducerProperties(outputName);
	}
	return null;
}