Java Code Examples for org.springframework.integration.channel.DirectChannel#setBeanName()

The following examples show how to use org.springframework.integration.channel.DirectChannel#setBeanName() . 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: KinesisBinderTests.java    From spring-cloud-stream-binder-aws-kinesis with Apache License 2.0 6 votes vote down vote up
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 2
Source File: MessageBusAdapter.java    From spring-bus with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a wiretap on the output channel and binds the tap channel to
 * {@link MessageBus}'s message target.
 *
 * @param tapChannelName the name of the tap channel
 * @param localName the channel to tap
 */
private void createAndBindTapChannel(String tapChannelName, String localName) {
	logger.info("creating and binding tap channel for {}", tapChannelName);
	MessageChannel channel = this.channelResolver.resolveDestination(localName);
	if (channel instanceof ChannelInterceptorAware) {
		DirectChannel tapChannel = new DirectChannel();
		tapChannel.setBeanName(tapChannelName + ".tap.bridge");
		this.messageBus.bindPubSubProducer(tapChannelName, tapChannel, null); // TODO
		// tap
		// producer
		// props
		tapOutputChannel(tapChannel, (ChannelInterceptorAware) channel);
	}
	else {
		if (logger.isDebugEnabled()) {
			logger.debug("output channel is not interceptor aware. Tap will not be created.");
		}
	}
}
 
Example 3
Source File: KafkaBinderTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@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 4
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 5 votes vote down vote up
@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 5
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomBatchingStrategy() throws Exception {
	RabbitTestBinder binder = getBinder();
	ExtendedProducerProperties<RabbitProducerProperties> producerProperties = createProducerProperties();
	producerProperties.getExtension().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
	producerProperties.getExtension().setBatchingEnabled(true);
	producerProperties.getExtension().setBatchingStrategyBeanName("testCustomBatchingStrategy");
	producerProperties.setRequiredGroups("default");

	ConfigurableListableBeanFactory beanFactory = binder.getApplicationContext().getBeanFactory();
	beanFactory.registerSingleton("testCustomBatchingStrategy", new TestBatchingStrategy());

	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<>("0".getBytes()));
	output.send(new GenericMessage<>("1".getBytes()));
	output.send(new GenericMessage<>("2".getBytes()));
	output.send(new GenericMessage<>("3".getBytes()));
	output.send(new GenericMessage<>("4".getBytes()));

	Object out = spyOn("batching.0.default").receive(false);
	assertThat(out).isInstanceOf(byte[].class);
	assertThat(new String((byte[]) out)).isEqualTo("0\u0000\n1\u0000\n2\u0000\n3\u0000\n4\u0000\n");

	producerBinding.unbind();
}
 
Example 6
Source File: AbstractBinderTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
protected DirectChannel createBindableChannel(String channelName,
		BindingProperties bindingProperties, boolean inputChannel) throws Exception {
	MessageConverterConfigurer messageConverterConfigurer = createConverterConfigurer(
			channelName, bindingProperties);
	DirectChannel channel = new DirectChannel();
	channel.setBeanName(channelName);
	if (inputChannel) {
		messageConverterConfigurer.configureInputChannel(channel, channelName);
	}
	else {
		messageConverterConfigurer.configureOutputChannel(channel, channelName);
	}
	return channel;
}
 
Example 7
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 8
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 9
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();
}
 
Example 10
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 4 votes vote down vote up
@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 11
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 4 votes vote down vote up
@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();
}
 
Example 12
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testConsumerBatching() 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);

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

	QueueChannel input = new QueueChannel();
	input.setBeanName("batchingConsumer");
	ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties();
	consumerProperties.setBatchMode(true);
	consumerProperties.getExtension().setBatchSize(2);
	Binding<MessageChannel> consumerBinding = binder.bindConsumer("c.batching.0",
			"consumerBatching", input, consumerProperties);
	output.send(new GenericMessage<>("foo".getBytes()));
	output.send(new GenericMessage<>("bar".getBytes()));


	Message<?> in = input.receive(10000);
	assertThat(in).isNotNull();
	assertThat(in.getPayload()).isInstanceOf(List.class);
	List<byte[]> payload = (List<byte[]>) in.getPayload();
	assertThat(payload).hasSize(2);
	assertThat(payload.get(0)).isEqualTo("foo".getBytes());
	assertThat(payload.get(1)).isEqualTo("bar".getBytes());

	producerBinding.unbind();
	consumerBinding.unbind();
}
 
Example 13
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 4 votes vote down vote up
@Test
public void testRoutingKeyExpression() throws Exception {
	RabbitTestBinder binder = getBinder();
	ExtendedProducerProperties<RabbitProducerProperties> producerProperties = createProducerProperties();
	producerProperties.getExtension().setRoutingKeyExpression(
			spelExpressionParser.parseExpression("payload.field"));

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

	RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());
	Queue queue = new AnonymousQueue();
	TopicExchange exchange = new TopicExchange("rke");
	org.springframework.amqp.core.Binding binding = BindingBuilder.bind(queue)
			.to(exchange).with("rkeTest");
	admin.declareQueue(queue);
	admin.declareBinding(binding);

	output.addInterceptor(new ChannelInterceptor() {

		@Override
		public Message<?> preSend(Message<?> message, MessageChannel channel) {
			assertThat(message.getHeaders()
					.get(RabbitExpressionEvaluatingInterceptor.ROUTING_KEY_HEADER))
							.isEqualTo("rkeTest");
			return message;
		}

	});

	output.send(new GenericMessage<>(new Pojo("rkeTest")));

	Object out = spyOn(queue.getName()).receive(false);
	assertThat(out).isInstanceOf(byte[].class);
	assertThat(new String((byte[]) out, StandardCharsets.UTF_8))
			.isEqualTo("{\"field\":\"rkeTest\"}");

	producerBinding.unbind();
}
 
Example 14
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 4 votes vote down vote up
@Test
public void testRoutingKeyExpressionPartitionedAndDelay() throws Exception {
	RabbitTestBinder binder = getBinder();
	ExtendedProducerProperties<RabbitProducerProperties> producerProperties = createProducerProperties();
	producerProperties.getExtension().setRoutingKeyExpression(
			spelExpressionParser.parseExpression("#root.getPayload().field"));
	// requires delayed message exchange plugin; tested locally
	// producerProperties.getExtension().setDelayedExchange(true);
	producerProperties.getExtension()
			.setDelayExpression(spelExpressionParser.parseExpression("1000"));
	producerProperties.setPartitionKeyExpression(new ValueExpression<>(0));

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

	RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());
	Queue queue = new AnonymousQueue();
	TopicExchange exchange = new TopicExchange("rkep");
	org.springframework.amqp.core.Binding binding = BindingBuilder.bind(queue)
			.to(exchange).with("rkepTest-0");
	admin.declareQueue(queue);
	admin.declareBinding(binding);

	output.addInterceptor(new ChannelInterceptor() {

		@Override
		public Message<?> preSend(Message<?> message, MessageChannel channel) {
			assertThat(message.getHeaders()
					.get(RabbitExpressionEvaluatingInterceptor.ROUTING_KEY_HEADER))
							.isEqualTo("rkepTest");
			assertThat(message.getHeaders()
					.get(RabbitExpressionEvaluatingInterceptor.DELAY_HEADER))
							.isEqualTo(1000);
			return message;
		}

	});

	output.send(new GenericMessage<>(new Pojo("rkepTest")));

	Object out = spyOn(queue.getName()).receive(false);
	assertThat(out).isInstanceOf(byte[].class);
	assertThat(new String((byte[]) out, StandardCharsets.UTF_8))
			.isEqualTo("{\"field\":\"rkepTest\"}");

	producerBinding.unbind();
}
 
Example 15
Source File: PartitionCapableBinderTests.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
@Test
@Ignore
public void testPartitionedModuleJava() throws Exception {
	B binder = getBinder();

	CP consumerProperties = createConsumerProperties();
	consumerProperties.setConcurrency(2);
	consumerProperties.setInstanceCount(3);
	consumerProperties.setInstanceIndex(0);
	consumerProperties.setPartitioned(true);
	QueueChannel input0 = new QueueChannel();
	input0.setBeanName("test.input0J");
	Binding<MessageChannel> input0Binding = binder.bindConsumer(
			String.format("partJ%s0", getDestinationNameDelimiter()),
			"testPartitionedModuleJava", input0, consumerProperties);
	consumerProperties.setInstanceIndex(1);
	QueueChannel input1 = new QueueChannel();
	input1.setBeanName("test.input1J");
	Binding<MessageChannel> input1Binding = binder.bindConsumer(
			String.format("partJ%s0", getDestinationNameDelimiter()),
			"testPartitionedModuleJava", input1, consumerProperties);
	consumerProperties.setInstanceIndex(2);
	QueueChannel input2 = new QueueChannel();
	input2.setBeanName("test.input2J");
	Binding<MessageChannel> input2Binding = binder.bindConsumer(
			String.format("partJ%s0", getDestinationNameDelimiter()),
			"testPartitionedModuleJava", input2, consumerProperties);

	PP producerProperties = createProducerProperties();
	producerProperties.setPartitionCount(3);
	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(
						String.format("partJ%s0", getDestinationNameDelimiter()),
						"testPartitionedModuleJava") + "-' + headers['"
						+ BinderHeaders.PARTITION_HEADER + "']");
	}

	output.send(MessageBuilder.withPayload("2")
			.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN)
			.build());
	output.send(MessageBuilder.withPayload("1")
			.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN)
			.build());
	output.send(MessageBuilder.withPayload("0")
			.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN)
			.build());

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

	if (usesExplicitRouting()) {
		assertThat(receive0.getPayload()).isEqualTo("0".getBytes());
		assertThat(receive1.getPayload()).isEqualTo("1".getBytes());
		assertThat(receive2.getPayload()).isEqualTo("2".getBytes());
	}
	else {
		List<Message<?>> receivedMessages = Arrays.asList(receive0, receive1,
				receive2);
		assertThat(receivedMessages).extracting("payload").containsExactlyInAnyOrder(
				"0".getBytes(), "1".getBytes(), "2".getBytes());
	}

	input0Binding.unbind();
	input1Binding.unbind();
	input2Binding.unbind();
	outputBinding.unbind();
}