Java Code Examples for org.springframework.cloud.stream.binder.ExtendedConsumerProperties#setPartitioned()

The following examples show how to use org.springframework.cloud.stream.binder.ExtendedConsumerProperties#setPartitioned() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiplexOnPartitionedConsumer() throws Exception {
	final ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties();
	RabbitTestSupport.RabbitProxy proxy = new RabbitTestSupport.RabbitProxy();
	CachingConnectionFactory cf = new CachingConnectionFactory("localhost",
			proxy.getPort());

	final RabbitExchangeQueueProvisioner rabbitExchangeQueueProvisioner = new RabbitExchangeQueueProvisioner(cf);

	consumerProperties.setMultiplex(true);
	consumerProperties.setPartitioned(true);
	consumerProperties.setInstanceIndexList(Arrays.asList(1, 2, 3));

	final ConsumerDestination consumerDestination = rabbitExchangeQueueProvisioner.provisionConsumerDestination("foo", "boo", consumerProperties);

	final String name = consumerDestination.getName();

	assertThat(name).isEqualTo("foo.boo-1,foo.boo-2,foo.boo-3");
}
 
Example 2
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiplexOnPartitionedConsumerWithMultipleDestinations() throws Exception {
	final ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties();
	RabbitTestSupport.RabbitProxy proxy = new RabbitTestSupport.RabbitProxy();
	CachingConnectionFactory cf = new CachingConnectionFactory("localhost",
			proxy.getPort());

	final RabbitExchangeQueueProvisioner rabbitExchangeQueueProvisioner = new RabbitExchangeQueueProvisioner(cf);

	consumerProperties.setMultiplex(true);
	consumerProperties.setPartitioned(true);
	consumerProperties.setInstanceIndexList(Arrays.asList(1, 2, 3));

	final ConsumerDestination consumerDestination = rabbitExchangeQueueProvisioner.provisionConsumerDestination("foo,qaa", "boo", consumerProperties);

	final String name = consumerDestination.getName();

	assertThat(name).isEqualTo("foo.boo-1,foo.boo-2,foo.boo-3,qaa.boo-1,qaa.boo-2,qaa.boo-3");
}
 
Example 3
Source File: KinesisBinderTests.java    From spring-cloud-stream-binder-aws-kinesis with Apache License 2.0 4 votes vote down vote up
@Test
@Override
public void testPartitionedModuleJava() throws Exception {
	KinesisTestBinder binder = getBinder();

	ExtendedConsumerProperties<KinesisConsumerProperties> consumerProperties = createConsumerProperties();
	consumerProperties.setConcurrency(2);
	consumerProperties.setInstanceCount(3);
	consumerProperties.setInstanceIndex(0);
	consumerProperties.setPartitioned(true);

	final List<Message<?>> results = new ArrayList<>();
	final CountDownLatch receiveLatch = new CountDownLatch(3);

	MessageHandler receivingHandler = (message) -> {
		results.add(message);
		receiveLatch.countDown();
	};

	DirectChannel input0 = createBindableChannelInternal("test.input0J", new BindingProperties(), true);
	input0.subscribe(receivingHandler);

	Binding<MessageChannel> input0Binding = binder.bindConsumer("partJ.0",
			"testPartitionedModuleJava", input0, consumerProperties);

	consumerProperties.setInstanceIndex(1);

	DirectChannel input1 = createBindableChannelInternal("test.input1J", new BindingProperties(), true);
	input1.subscribe(receivingHandler);

	Binding<MessageChannel> input1Binding = binder.bindConsumer("partJ.0",
			"testPartitionedModuleJava", input1, consumerProperties);

	consumerProperties.setInstanceIndex(2);

	DirectChannel input2 = createBindableChannelInternal("test.input2J", new BindingProperties(), true);
	input2.subscribe(receivingHandler);

	Binding<MessageChannel> input2Binding = binder.bindConsumer("partJ.0",
			"testPartitionedModuleJava", input2, consumerProperties);

	ExtendedProducerProperties<KinesisProducerProperties> producerProperties = createProducerProperties();

	producerProperties.setPartitionKeyExtractorName("partitionSupport");
	producerProperties.setPartitionSelectorName("partitionSupport");
	producerProperties.setPartitionCount(3);

	DirectChannel output = createBindableChannelInternal("test.output",
			createProducerBindingProperties(producerProperties), false);

	Binding<MessageChannel> outputBinding = binder.bindProducer("partJ.0", output,
			producerProperties);
	if (usesExplicitRouting()) {
		Object endpoint = extractEndpoint(outputBinding);
		assertThat(getEndpointRouting(endpoint))
				.contains(getExpectedRoutingBaseDestination("partJ.0",
						"testPartitionedModuleJava") + "-' + headers['"
						+ BinderHeaders.PARTITION_HEADER + "']");
	}

	output.send(new GenericMessage<>(2));
	output.send(new GenericMessage<>(1));
	output.send(new GenericMessage<>(0));

	assertThat(receiveLatch.await(20, TimeUnit.SECONDS)).isTrue();

	assertThat(results).extracting("payload").containsExactlyInAnyOrder(
			"0".getBytes(), "1".getBytes(), "2".getBytes());

	input0Binding.unbind();
	input1Binding.unbind();
	input2Binding.unbind();
	outputBinding.unbind();
}
 
Example 4
Source File: KafkaBinderTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 4 votes vote down vote up
@Test
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testPartitionedModuleJava() throws Exception {
	Binder binder = getBinder();

	KafkaBinderConfigurationProperties configurationProperties = createConfigurationProperties();

	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();
	consumerProperties.setConcurrency(2);
	consumerProperties.setInstanceCount(4);
	consumerProperties.setInstanceIndex(0);
	consumerProperties.setPartitioned(true);
	consumerProperties.getExtension().setAutoRebalanceEnabled(false);
	QueueChannel input0 = new QueueChannel();
	input0.setBeanName("test.input0J");
	Binding<MessageChannel> input0Binding = binder.bindConsumer("partJ.0", "test",
			input0, consumerProperties);
	consumerProperties.setInstanceIndex(1);
	QueueChannel input1 = new QueueChannel();
	input1.setBeanName("test.input1J");
	Binding<MessageChannel> input1Binding = binder.bindConsumer("partJ.0", "test",
			input1, consumerProperties);
	consumerProperties.setInstanceIndex(2);
	QueueChannel input2 = new QueueChannel();
	input2.setBeanName("test.input2J");
	Binding<MessageChannel> input2Binding = binder.bindConsumer("partJ.0", "test",
			input2, consumerProperties);
	consumerProperties.setInstanceIndex(3);
	QueueChannel input3 = new QueueChannel();
	input3.setBeanName("test.input3J");
	Binding<MessageChannel> input3Binding = binder.bindConsumer("partJ.0", "test",
			input3, consumerProperties);

	ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties();

	this.applicationContext.registerBean("pkExtractor",
			PartitionTestSupport.class, () -> new PartitionTestSupport());
	this.applicationContext.registerBean("pkSelector",
			PartitionTestSupport.class, () -> new PartitionTestSupport());
	producerProperties.setPartitionKeyExtractorName("pkExtractor");
	producerProperties.setPartitionSelectorName("pkSelector");
	producerProperties.setPartitionCount(3); // overridden to 8 on the actual topic
	DirectChannel output = createBindableChannel("output",
			createProducerBindingProperties(producerProperties));
	output.setBeanName("test.output");
	Binding<MessageChannel> outputBinding = binder.bindProducer("partJ.0", output,
			producerProperties);
	if (usesExplicitRouting()) {
		Object endpoint = extractEndpoint(outputBinding);
		assertThat(getEndpointRouting(endpoint))
				.contains(getExpectedRoutingBaseDestination("partJ.0", "test")
						+ "-' + headers['partition']");
	}

	output.send(new GenericMessage<>(2));
	output.send(new GenericMessage<>(1));
	output.send(new GenericMessage<>(0));
	output.send(new GenericMessage<>(3));

	Message<?> receive0 = receive(input0);
	assertThat(receive0).isNotNull();
	Message<?> receive1 = receive(input1);
	assertThat(receive1).isNotNull();
	Message<?> receive2 = receive(input2);
	assertThat(receive2).isNotNull();
	Message<?> receive3 = receive(input3);
	assertThat(receive3).isNotNull();
	ObjectMapper om = new ObjectMapper();

	assertThat(om.readValue((byte[]) receive0.getPayload(), Integer.class))
			.isEqualTo(0);
	assertThat(om.readValue((byte[]) receive1.getPayload(), Integer.class))
			.isEqualTo(1);
	assertThat(om.readValue((byte[]) receive2.getPayload(), Integer.class))
			.isEqualTo(2);
	assertThat(om.readValue((byte[]) receive3.getPayload(), Integer.class))
			.isEqualTo(3);

	input0Binding.unbind();
	input1Binding.unbind();
	input2Binding.unbind();
	input3Binding.unbind();
	outputBinding.unbind();
}
 
Example 5
Source File: KafkaBinderTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testPartitionedModuleJavaWithRawMode() throws Exception {
	Binder binder = getBinder();
	ExtendedProducerProperties<KafkaProducerProperties> properties = createProducerProperties();
	properties.setHeaderMode(HeaderMode.none);
	this.applicationContext.registerBean("pkExtractor",
			RawKafkaPartitionTestSupport.class, () -> new RawKafkaPartitionTestSupport());
	this.applicationContext.registerBean("pkSelector",
			RawKafkaPartitionTestSupport.class, () -> new RawKafkaPartitionTestSupport());
	properties.setPartitionKeyExtractorName("pkExtractor");
	properties.setPartitionSelectorName("pkSelector");
	properties.setPartitionCount(6);

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

	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();
	consumerProperties.setConcurrency(2);
	consumerProperties.setInstanceCount(3);
	consumerProperties.setInstanceIndex(0);
	consumerProperties.setPartitioned(true);
	consumerProperties.setHeaderMode(HeaderMode.none);
	consumerProperties.getExtension().setAutoRebalanceEnabled(false);
	QueueChannel input0 = new QueueChannel();
	input0.setBeanName("test.input0J");
	Binding<MessageChannel> input0Binding = binder.bindConsumer("partJ.raw.0", "test",
			input0, consumerProperties);
	consumerProperties.setInstanceIndex(1);
	QueueChannel input1 = new QueueChannel();
	input1.setBeanName("test.input1J");
	Binding<MessageChannel> input1Binding = binder.bindConsumer("partJ.raw.0", "test",
			input1, consumerProperties);
	consumerProperties.setInstanceIndex(2);
	QueueChannel input2 = new QueueChannel();
	input2.setBeanName("test.input2J");
	Binding<MessageChannel> input2Binding = binder.bindConsumer("partJ.raw.0", "test",
			input2, consumerProperties);

	output.send(new GenericMessage<>(new byte[] { (byte) 0 }));
	output.send(new GenericMessage<>(new byte[] { (byte) 1 }));
	output.send(new GenericMessage<>(new byte[] { (byte) 2 }));

	Message<?> receive0 = receive(input0);
	assertThat(receive0).isNotNull();
	Message<?> receive1 = receive(input1);
	assertThat(receive1).isNotNull();
	Message<?> receive2 = receive(input2);
	assertThat(receive2).isNotNull();

	assertThat(Arrays.asList(((byte[]) receive0.getPayload())[0],
			((byte[]) receive1.getPayload())[0], ((byte[]) receive2.getPayload())[0]))
					.containsExactlyInAnyOrder((byte) 0, (byte) 1, (byte) 2);

	input0Binding.unbind();
	input1Binding.unbind();
	input2Binding.unbind();
	outputBinding.unbind();
}
 
Example 6
Source File: KafkaBinderTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testPartitionedModuleSpELWithRawMode() throws Exception {
	Binder binder = getBinder();
	ExtendedProducerProperties<KafkaProducerProperties> properties = createProducerProperties();
	properties.setPartitionKeyExpression(
			spelExpressionParser.parseExpression("payload[0]"));
	properties.setPartitionSelectorExpression(
			spelExpressionParser.parseExpression("hashCode()"));
	properties.setPartitionCount(6);
	properties.setHeaderMode(HeaderMode.none);

	DirectChannel output = createBindableChannel("output",
			createProducerBindingProperties(properties));
	output.setBeanName("test.output");
	Binding<MessageChannel> outputBinding = binder.bindProducer("part.raw.0", output,
			properties);
	try {
		Object endpoint = extractEndpoint(outputBinding);
		assertThat(getEndpointRouting(endpoint))
				.contains(getExpectedRoutingBaseDestination("part.raw.0", "test")
						+ "-' + headers['partition']");
	}
	catch (UnsupportedOperationException ignored) {
	}

	ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties();
	consumerProperties.setConcurrency(2);
	consumerProperties.setInstanceIndex(0);
	consumerProperties.setInstanceCount(3);
	consumerProperties.setPartitioned(true);
	consumerProperties.setHeaderMode(HeaderMode.none);
	consumerProperties.getExtension().setAutoRebalanceEnabled(false);
	QueueChannel input0 = new QueueChannel();
	input0.setBeanName("test.input0S");
	Binding<MessageChannel> input0Binding = binder.bindConsumer("part.raw.0", "test",
			input0, consumerProperties);
	consumerProperties.setInstanceIndex(1);
	QueueChannel input1 = new QueueChannel();
	input1.setBeanName("test.input1S");
	Binding<MessageChannel> input1Binding = binder.bindConsumer("part.raw.0", "test",
			input1, consumerProperties);
	consumerProperties.setInstanceIndex(2);
	QueueChannel input2 = new QueueChannel();
	input2.setBeanName("test.input2S");
	Binding<MessageChannel> input2Binding = binder.bindConsumer("part.raw.0", "test",
			input2, consumerProperties);

	Message<byte[]> message2 = org.springframework.integration.support.MessageBuilder
			.withPayload(new byte[] { 2 })
			.setHeader(IntegrationMessageHeaderAccessor.CORRELATION_ID,
					"kafkaBinderTestCommonsDelegate")
			.setHeader(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER, 42)
			.setHeader(IntegrationMessageHeaderAccessor.SEQUENCE_SIZE, 43).build();
	output.send(message2);
	output.send(new GenericMessage<>(new byte[] { 1 }));
	output.send(new GenericMessage<>(new byte[] { 0 }));
	Message<?> receive0 = receive(input0);
	assertThat(receive0).isNotNull();
	Message<?> receive1 = receive(input1);
	assertThat(receive1).isNotNull();
	Message<?> receive2 = receive(input2);
	assertThat(receive2).isNotNull();
	assertThat(Arrays.asList(((byte[]) receive0.getPayload())[0],
			((byte[]) receive1.getPayload())[0], ((byte[]) receive2.getPayload())[0]))
					.containsExactlyInAnyOrder((byte) 0, (byte) 1, (byte) 2);
	input0Binding.unbind();
	input1Binding.unbind();
	input2Binding.unbind();
	outputBinding.unbind();
}