GH-2268: Add RedisHeaders.MESSAGE_SOURCE header

Resolves: /spring-projects/spring-integration#2268

* To indicate the source the Redis message in the `RedisInboundChannelAdapter`
populate the `RedisHeaders.MESSAGE_SOURCE` header to the messages to produce
* Fix the `SimpleMessageConverter` to populate the provided `MessageHeaders`
to the message to produce

* Add `toMessage(T object, @Nullable Map<String, Object> headers)`
to the `InboundMessageMapper` to propagate additional header
to the message to create
* Rework all the out-of-the-box `InboundMessageMapper` implementations
to properly propagate additional headers via `toMessage()`
from the `SimpleMessageConverter`
* Provide optimizations in the `InboundMessageMapper` implementations
do not re-create messages
* Refactor `MutableMessage.toString()` to align with the `GenericMessage`

* Add more `@Nullable` to method arguments
* Increase latch wait timeout in the `EndpointParserTests`
* Address `redis.adoc` PR comment
This commit is contained in:
Artem Bilan
2017-11-07 13:25:33 -05:00
committed by Gary Russell
parent 70c166fbfd
commit 7ca20e53f7
23 changed files with 249 additions and 104 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.integration.redis.inbound;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;
@@ -30,10 +31,13 @@ import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.integration.endpoint.MessageProducerSupport;
import org.springframework.integration.support.converter.SimpleMessageConverter;
import org.springframework.integration.redis.support.RedisHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.converter.SimpleMessageConverter;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* @author Mark Fisher
@@ -78,6 +82,12 @@ public class RedisInboundChannelAdapter extends MessageProducerSupport {
this.messageConverter = messageConverter;
}
/**
* Specify an {@link Executor} used for running the message listeners when messages are received.
* @param taskExecutor the Executor to use for listener container.
* @since 4.3.13
* @see RedisMessageListenerContainer#setTaskExecutor(Executor)
*/
public void setTaskExecutor(Executor taskExecutor) {
this.container.setTaskExecutor(taskExecutor);
}
@@ -138,8 +148,13 @@ public class RedisInboundChannelAdapter extends MessageProducerSupport {
this.container.stop();
}
private Message<?> convertMessage(Object object) {
return this.messageConverter.toMessage(object, null);
private Message<?> convertMessage(Object object, String source) {
MessageHeaders messageHeaders = null;
if (StringUtils.hasText(source)) {
messageHeaders = new MessageHeaders(Collections.singletonMap(RedisHeaders.MESSAGE_SOURCE, source));
}
return this.messageConverter.toMessage(object, messageHeaders);
}
@@ -150,9 +165,10 @@ public class RedisInboundChannelAdapter extends MessageProducerSupport {
}
@SuppressWarnings("unused")
public void handleMessage(Object object) {
sendMessage(convertMessage(object));
public void handleMessage(Object message, String source) {
sendMessage(convertMessage(message, source));
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@ package org.springframework.integration.redis.support;
* @author Oleg Zhurakousky
* @author Gary Russell
* @author Artem Bilan
*
* @since 2.2
*/
public final class RedisHeaders {
@@ -43,4 +44,6 @@ public final class RedisHeaders {
public static final String COMMAND = PREFIX + "command";
public static final String MESSAGE_SOURCE = PREFIX + "messageSource";
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2007-2016 the original author or authors.
* Copyright 2007-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,10 +16,10 @@
package org.springframework.integration.redis.inbound;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import org.hamcrest.Matchers;
@@ -33,6 +33,7 @@ import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.redis.rules.RedisAvailable;
import org.springframework.integration.redis.rules.RedisAvailableTests;
import org.springframework.integration.redis.support.RedisHeaders;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.Message;
@@ -40,6 +41,7 @@ import org.springframework.messaging.Message;
* @author Mark Fisher
* @author Artem Bilan
* @author Gary Russell
*
* @since 2.1
*/
public class RedisInboundChannelAdapterTests extends RedisAvailableTests {
@@ -83,7 +85,8 @@ public class RedisInboundChannelAdapterTests extends RedisAvailableTests {
throw new RuntimeException("Failed to receive message # " + i + " iteration " + iteration);
}
assertNotNull(message);
assertTrue(message.getPayload().toString().startsWith("test-"));
assertThat(message.getPayload().toString(), startsWith("test-"));
assertEquals("testRedisInboundChannelAdapterChannel", message.getHeaders().get(RedisHeaders.MESSAGE_SOURCE));
counter++;
}
assertEquals(numToTest, counter);
@@ -119,7 +122,7 @@ public class RedisInboundChannelAdapterTests extends RedisAvailableTests {
Object payload = message.getPayload();
assertThat(payload, Matchers.instanceOf(byte[].class));
assertTrue(new String((byte[]) payload).startsWith("test-"));
assertThat(new String((byte[]) payload), startsWith("test-"));
counter++;
}