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

The following examples show how to use org.springframework.cloud.stream.binder.ExtendedProducerProperties. 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
@Override
public ProducerDestination provisionProducerDestination(String name,
		ExtendedProducerProperties<KinesisProducerProperties> properties)
		throws ProvisioningException {

	if (logger.isInfoEnabled()) {
		logger.info("Using Kinesis stream for outbound: " + name);
	}

	if (properties.getHeaderMode() == null) {
		properties.setHeaderMode(HeaderMode.embeddedHeaders);
	}

	return new KinesisProducerDestination(name,
			createOrUpdate(name, properties.getPartitionCount()));
}
 
Example 2
@Override
protected MessageHandler createProducerMessageHandler(ProducerDestination destination,
		ExtendedProducerProperties<KinesisProducerProperties> producerProperties,
		MessageChannel errorChannel) {

	FunctionExpression<Message<?>> partitionKeyExpression =
			new FunctionExpression<>((m) ->
					m.getHeaders().containsKey(BinderHeaders.PARTITION_HEADER)
							? m.getHeaders().get(BinderHeaders.PARTITION_HEADER)
							: m.getPayload().hashCode());
	final AbstractAwsMessageHandler<?> messageHandler;
	if (this.configurationProperties.isKplKclEnabled()) {
		messageHandler = createKplMessageHandler(destination, partitionKeyExpression);
	}
	else {
		messageHandler = createKinesisMessageHandler(destination, partitionKeyExpression);
	}
	messageHandler.setSync(producerProperties.getExtension().isSync());
	messageHandler.setSendTimeout(producerProperties.getExtension().getSendTimeout());
	messageHandler.setFailureChannel(errorChannel);
	messageHandler.setBeanFactory(getBeanFactory());

	this.streamsInUse.add(destination.getName());

	return messageHandler;
}
 
Example 3
@Test
void testProvisionProducerSuccessfulWithExistingStream() {
	AmazonKinesis amazonKinesisMock = mock(AmazonKinesis.class);
	KinesisBinderConfigurationProperties binderProperties = new KinesisBinderConfigurationProperties();
	KinesisStreamProvisioner provisioner = new KinesisStreamProvisioner(
			amazonKinesisMock, binderProperties);
	ExtendedProducerProperties<KinesisProducerProperties> extendedProducerProperties =
			new ExtendedProducerProperties<>(new KinesisProducerProperties());
	String name = "test-stream";

	DescribeStreamResult describeStreamResult = describeStreamResultWithShards(
			Collections.singletonList(new Shard()));

	when(amazonKinesisMock.describeStream(any(DescribeStreamRequest.class)))
			.thenReturn(describeStreamResult);

	ProducerDestination destination = provisioner.provisionProducerDestination(name,
			extendedProducerProperties);

	verify(amazonKinesisMock).describeStream(any(DescribeStreamRequest.class));

	assertThat(destination.getName()).isEqualTo(name);
}
 
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 testSendTimeoutExpressionProducerMetadata() throws Exception {
	Binder binder = getBinder(createConfigurationProperties());
	DirectChannel output = new DirectChannel();
	String testTopicName = UUID.randomUUID().toString();
	ExtendedProducerProperties<KafkaProducerProperties> properties = createProducerProperties();
	properties.getExtension().setSync(true);
	SpelExpressionParser parser = new SpelExpressionParser();
	Expression sendTimeoutExpression = parser.parseExpression("5000");
	properties.getExtension().setSendTimeoutExpression(sendTimeoutExpression);
	Binding<MessageChannel> producerBinding = binder.bindProducer(testTopicName,
			output, properties);
	DirectFieldAccessor accessor = new DirectFieldAccessor(
			extractEndpoint(producerBinding));
	KafkaProducerMessageHandler wrappedInstance = (KafkaProducerMessageHandler) accessor
			.getWrappedInstance();
	assertThat(new DirectFieldAccessor(wrappedInstance).getPropertyValue("sendTimeoutExpression")
			.equals(sendTimeoutExpression));
	producerBinding.unbind();
}
 
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
@Override
public Binding<MessageChannel> bindProducer(String name,
		MessageChannel moduleOutputChannel,
		ExtendedProducerProperties<RabbitProducerProperties> properties) {
	this.queues.add(properties.getExtension().getPrefix() + name + ".default");
	this.exchanges.add(properties.getExtension().getPrefix() + name);
	if (properties.getRequiredGroups() != null) {
		for (String group : properties.getRequiredGroups()) {
			if (properties.getExtension().isQueueNameGroupOnly()) {
				this.queues.add(properties.getExtension().getPrefix() + group);
			}
			else {
				this.queues.add(
						properties.getExtension().getPrefix() + name + "." + group);
			}
		}
	}
	this.prefixes.add(properties.getExtension().getPrefix());
	deadLetters(properties.getExtension());
	return super.bindProducer(name, moduleOutputChannel, properties);
}
 
Example 8
public RocketMQMessageHandler(RocketMQTemplate rocketMQTemplate, String destination,
		String groupName, Boolean transactional,
		InstrumentationManager instrumentationManager,
		ExtendedProducerProperties<RocketMQProducerProperties> producerProperties,
		MessageConverterConfigurer.PartitioningInterceptor partitioningInterceptor) {
	this.rocketMQTemplate = rocketMQTemplate;
	this.destination = destination;
	this.groupName = groupName;
	this.transactional = transactional;
	this.instrumentationManager = instrumentationManager;
	this.producerProperties = producerProperties;
	this.partitioningInterceptor = partitioningInterceptor;
}
 
Example 9
@Override
public ProducerDestination provisionProducerDestination(String name,
		ExtendedProducerProperties<RocketMQProducerProperties> properties)
		throws ProvisioningException {
	checkTopic(name);
	return new RocketProducerDestination(name);
}
 
Example 10
@Override
protected MessageHandler createProducerMessageHandler(ProducerDestination destination,
		ExtendedProducerProperties<RocketMQProducerProperties> producerProperties,
		MessageChannel errorChannel) throws Exception {
	throw new UnsupportedOperationException(
			"The abstract binder should not call this method");
}
 
Example 11
@Test
void testProvisionProducerSuccessfulWithNewStream() {
	AmazonKinesis amazonKinesisMock = mock(AmazonKinesis.class);
	KinesisBinderConfigurationProperties binderProperties = new KinesisBinderConfigurationProperties();
	KinesisStreamProvisioner provisioner = new KinesisStreamProvisioner(
			amazonKinesisMock, binderProperties);
	ExtendedProducerProperties<KinesisProducerProperties> extendedProducerProperties =
			new ExtendedProducerProperties<>(
			new KinesisProducerProperties());

	String name = "test-stream";
	Integer shards = 1;

	DescribeStreamResult describeStreamResult = describeStreamResultWithShards(
			Collections.singletonList(new Shard()));

	when(amazonKinesisMock.describeStream(any(DescribeStreamRequest.class)))
			.thenThrow(new ResourceNotFoundException("I got nothing"))
			.thenReturn(describeStreamResult);

	when(amazonKinesisMock.createStream(name, shards))
			.thenReturn(new CreateStreamResult());

	ProducerDestination destination = provisioner.provisionProducerDestination(name,
			extendedProducerProperties);

	verify(amazonKinesisMock, times(2))
			.describeStream(any(DescribeStreamRequest.class));

	verify(amazonKinesisMock).createStream(name, shards);

	assertThat(destination.getName()).isEqualTo(name);
}
 
Example 12
@Test
@SuppressWarnings("unchecked")
public void testBatchListener() throws Exception {
	KinesisTestBinder binder = getBinder();
	ExtendedProducerProperties<KinesisProducerProperties> producerProperties = createProducerProperties();
	DirectChannel output = createBindableChannel("output",
			createProducerBindingProperties(producerProperties));

	Binding<MessageChannel> outputBinding = binder.bindProducer("testBatchListener",
			output, producerProperties);

	for (int i = 0; i < 3; i++) {
		output.send(new GenericMessage<>(i));
	}

	ExtendedConsumerProperties<KinesisConsumerProperties> consumerProperties = createConsumerProperties();
	consumerProperties.getExtension().setListenerMode(ListenerMode.batch);
	consumerProperties.setUseNativeDecoding(true);

	QueueChannel input = new QueueChannel();
	Binding<MessageChannel> inputBinding = binder.bindConsumer("testBatchListener",
			null, input, consumerProperties);

	Message<List<?>> receivedMessage = (Message<List<?>>) receive(input);
	assertThat(receivedMessage).isNotNull();
	assertThat(receivedMessage.getPayload().size()).isEqualTo(3);

	receivedMessage.getPayload().forEach((r) -> {
		assertThat(r).isInstanceOf(Record.class);
	});

	outputBinding.unbind();
	inputBinding.unbind();
}
 
Example 13
@Override
protected ExtendedProducerProperties<KinesisProducerProperties> createProducerProperties() {
	ExtendedProducerProperties<KinesisProducerProperties> producerProperties = new ExtendedProducerProperties<>(
			new KinesisProducerProperties());
	producerProperties.setPartitionKeyExpression(new LiteralExpression("1"));
	producerProperties.getExtension().setSync(true);
	return producerProperties;
}
 
Example 14
Source Project: spring-cloud-gcp   Source File: PubSubChannelProvisioner.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ProducerDestination provisionProducerDestination(String topic,
		ExtendedProducerProperties<PubSubProducerProperties> properties)
		throws ProvisioningException {
	makeSureTopicExists(topic, properties.getExtension().isAutoCreateResources());

	return new PubSubProducerDestination(topic);
}
 
Example 15
@Override
protected MessageHandler createProducerMessageHandler(ProducerDestination destination,
		ExtendedProducerProperties<PubSubProducerProperties> producerProperties,
		MessageChannel errorChannel) {

	PubSubMessageHandler messageHandler = new PubSubMessageHandler(this.pubSubTemplate, destination.getName());
	messageHandler.setBeanFactory(getBeanFactory());
	return messageHandler;
}
 
Example 16
@Override
public ProducerDestination provisionProducerDestination(final String name,
		ExtendedProducerProperties<KafkaProducerProperties> properties) {

	if (logger.isInfoEnabled()) {
		logger.info("Using kafka topic for outbound: " + name);
	}
	KafkaTopicUtils.validateTopicName(name);
	try (AdminClient adminClient = AdminClient.create(this.adminClientProperties)) {
		createTopic(adminClient, name, properties.getPartitionCount(), false,
				properties.getExtension().getTopic());
		int partitions = 0;
		Map<String, TopicDescription> topicDescriptions = new HashMap<>();
		if (this.configurationProperties.isAutoCreateTopics()) {
			this.metadataRetryOperations.execute(context -> {
				try {
					if (logger.isDebugEnabled()) {
						logger.debug("Attempting to retrieve the description for the topic: " + name);
					}
					DescribeTopicsResult describeTopicsResult = adminClient
							.describeTopics(Collections.singletonList(name));
					KafkaFuture<Map<String, TopicDescription>> all = describeTopicsResult
							.all();
					topicDescriptions.putAll(all.get(this.operationTimeout, TimeUnit.SECONDS));
				}
				catch (Exception ex) {
					throw new ProvisioningException("Problems encountered with partitions finding", ex);
				}
				return null;
			});
		}
		TopicDescription topicDescription = topicDescriptions.get(name);
		if (topicDescription != null) {
			partitions = topicDescription.partitions().size();
		}
		return new KafkaProducerDestination(name, partitions);
	}
}
 
Example 17
@Override
@SuppressWarnings("unchecked")
protected Binding<KStream<Object, Object>> doBindProducer(String name,
		KStream<Object, Object> outboundBindTarget,
		ExtendedProducerProperties<KafkaStreamsProducerProperties> properties) {

	ExtendedProducerProperties<KafkaProducerProperties> extendedProducerProperties =
			(ExtendedProducerProperties) properties;

	this.kafkaTopicProvisioner.provisionProducerDestination(name, extendedProducerProperties);
	Serde<?> keySerde = this.keyValueSerdeResolver
			.getOuboundKeySerde(properties.getExtension(), kafkaStreamsBindingInformationCatalogue.getOutboundKStreamResolvable());
	LOG.info("Key Serde used for (outbound) " + name + ": " + keySerde.getClass().getName());

	Serde<?> valueSerde;
	if (properties.isUseNativeEncoding()) {
		valueSerde = this.keyValueSerdeResolver.getOutboundValueSerde(properties,
				properties.getExtension(), kafkaStreamsBindingInformationCatalogue.getOutboundKStreamResolvable());
	}
	else {
		valueSerde = Serdes.ByteArray();
	}
	LOG.info("Value Serde used for (outbound) " + name + ": " + valueSerde.getClass().getName());

	to(properties.isUseNativeEncoding(), name, outboundBindTarget,
			(Serde<Object>) keySerde, (Serde<Object>) valueSerde, properties.getExtension());
	return new DefaultBinding<>(name, null, outboundBindTarget, null);
}
 
Example 18
@Override
protected Binding<GlobalKTable<Object, Object>> doBindProducer(String name,
		GlobalKTable<Object, Object> outboundBindTarget,
		ExtendedProducerProperties<KafkaStreamsProducerProperties> properties) {
	throw new UnsupportedOperationException(
			"No producer level binding is allowed for GlobalKTable");
}
 
Example 19
@Override
protected Binding<KTable<Object, Object>> doBindProducer(String name,
		KTable<Object, Object> outboundBindTarget,
		// @checkstyle:off
		ExtendedProducerProperties<KafkaStreamsProducerProperties> properties) {
	// @checkstyle:on
	throw new UnsupportedOperationException(
			"No producer level binding is allowed for KTable");
}
 
Example 20
public KafkaMessageChannelBinder(
		KafkaBinderConfigurationProperties configurationProperties,
		KafkaTopicProvisioner provisioningProvider,
		ListenerContainerCustomizer<AbstractMessageListenerContainer<?, ?>> containerCustomizer,
		MessageSourceCustomizer<KafkaMessageSource<?, ?>> sourceCustomizer,
		KafkaBindingRebalanceListener rebalanceListener,
		DlqPartitionFunction dlqPartitionFunction) {

	super(headersToMap(configurationProperties), provisioningProvider,
			containerCustomizer, sourceCustomizer);
	this.configurationProperties = configurationProperties;
	String txId = configurationProperties.getTransaction().getTransactionIdPrefix();
	if (StringUtils.hasText(txId)) {
		this.transactionManager = new KafkaTransactionManager<>(getProducerFactory(
				txId, new ExtendedProducerProperties<>(configurationProperties
						.getTransaction().getProducer().getExtension()), txId + ".producer"));
		this.transactionTemplate = new TransactionTemplate(this.transactionManager);
	}
	else {
		this.transactionManager = null;
		this.transactionTemplate = null;
	}
	this.rebalanceListener = rebalanceListener;
	this.dlqPartitionFunction = dlqPartitionFunction != null
			? dlqPartitionFunction
			: null;
}
 
Example 21
@Override
protected MessageHandler createProducerMessageHandler(
		final ProducerDestination destination,
		ExtendedProducerProperties<KafkaProducerProperties> producerProperties,
		MessageChannel errorChannel) throws Exception {
	throw new IllegalStateException(
			"The abstract binder should not call this method");
}
 
Example 22
@Override
protected void postProcessOutputChannel(MessageChannel outputChannel,
		ExtendedProducerProperties<KafkaProducerProperties> producerProperties) {

	if (expressionInterceptorNeeded(producerProperties)) {
		((AbstractMessageChannel) outputChannel).addInterceptor(0, new KafkaExpressionEvaluatingInterceptor(
				producerProperties.getExtension().getMessageKeyExpression(), getEvaluationContext()));
	}
}
 
Example 23
private boolean expressionInterceptorNeeded(
		ExtendedProducerProperties<KafkaProducerProperties> producerProperties) {
	if (producerProperties.isUseNativeEncoding()) {
		return false; // payload will be intact when it reaches the adapter
	}
	else {
		Expression messageKeyExpression = producerProperties.getExtension().getMessageKeyExpression();
		return messageKeyExpression != null
				&& interceptorNeededPattern.matcher(messageKeyExpression.getExpressionString()).find();
	}
}
 
Example 24
@Override
protected boolean useNativeEncoding(
		ExtendedProducerProperties<KafkaProducerProperties> producerProperties) {
	if (transactionManager(producerProperties.getExtension().getTransactionManager()) != null) {
		return this.configurationProperties.getTransaction().getProducer()
				.isUseNativeEncoding();
	}
	return super.useNativeEncoding(producerProperties);
}
 
Example 25
ProducerConfigurationMessageHandler(KafkaTemplate<byte[], byte[]> kafkaTemplate,
		String topic,
		ExtendedProducerProperties<KafkaProducerProperties> producerProperties,
		ProducerFactory<byte[], byte[]> producerFactory) {

	super(kafkaTemplate);
	if (producerProperties.getExtension().isUseTopicHeader()) {
		setTopicExpression(PARSER.parseExpression("headers['" + KafkaHeaders.TOPIC + "'] ?: '" + topic + "'"));
	}
	else {
		setTopicExpression(new LiteralExpression(topic));
	}
	Expression messageKeyExpression = producerProperties.getExtension().getMessageKeyExpression();
	if (expressionInterceptorNeeded(producerProperties)) {
		messageKeyExpression = PARSER.parseExpression("headers['"
				+ KafkaExpressionEvaluatingInterceptor.MESSAGE_KEY_HEADER
				+ "']");
	}
	setMessageKeyExpression(messageKeyExpression);
	setBeanFactory(KafkaMessageChannelBinder.this.getBeanFactory());
	if (producerProperties.isPartitioned()) {
		setPartitionIdExpression(PARSER.parseExpression(
				"headers['" + BinderHeaders.PARTITION_HEADER + "']"));
	}
	if (producerProperties.getExtension().isSync()) {
		setSync(true);
	}
	if (producerProperties.getExtension().getSendTimeoutExpression() != null) {
		setSendTimeoutExpression(producerProperties.getExtension().getSendTimeoutExpression());
	}
	this.producerFactory = producerFactory;
}
 
Example 26
@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 27
@Override
protected ExtendedProducerProperties<KafkaProducerProperties> createProducerProperties() {
	ExtendedProducerProperties<KafkaProducerProperties> producerProperties = new ExtendedProducerProperties<>(
			new KafkaProducerProperties());
	producerProperties.getExtension().setSync(true);
	return producerProperties;
}
 
Example 28
@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 29
@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 30
@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();
}