Java Code Examples for org.springframework.integration.channel.DirectChannel#addInterceptor()
The following examples show how to use
org.springframework.integration.channel.DirectChannel#addInterceptor() .
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: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@Test @SuppressWarnings("unchecked") public void testMessageKeyInPayload() throws Exception { Binding<?> producerBinding = null; try { String testPayload = "test"; ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties(); producerProperties.getExtension() .setMessageKeyExpression(spelExpressionParser.parseExpression("payload.field.bytes")); DirectChannel moduleOutputChannel = createBindableChannel("output", createProducerBindingProperties(producerProperties)); String testTopicName = "existing" + System.currentTimeMillis(); KafkaTestBinder binder = getBinder(); producerBinding = binder.bindProducer(testTopicName, moduleOutputChannel, producerProperties); moduleOutputChannel.addInterceptor(new ChannelInterceptor() { @Override public Message<?> preSend(Message<?> message, MessageChannel channel) { assertThat(message.getHeaders() .get(KafkaExpressionEvaluatingInterceptor.MESSAGE_KEY_HEADER)) .isEqualTo("foo".getBytes()); return message; } }); moduleOutputChannel.send( new GenericMessage<>(new Pojo("foo"), Collections.singletonMap(KafkaHeaders.PARTITION_ID, 0))); } finally { if (producerBinding != null) { producerBinding.unbind(); } } }
Example 2
Source File: RabbitBinderTests.java From spring-cloud-stream-binder-rabbit with Apache License 2.0 | 4 votes |
@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 3
Source File: RabbitBinderTests.java From spring-cloud-stream-binder-rabbit with Apache License 2.0 | 4 votes |
@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(); }