Skip to content

AMQP: Add bindSourceMessage property (inbound) #2959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public class AmqpInboundChannelAdapter extends MessageProducerSupport implements

private BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(0, 0, 0L);

private boolean bindSourceMessage;

public AmqpInboundChannelAdapter(AbstractMessageListenerContainer listenerContainer) {
Assert.notNull(listenerContainer, "listenerContainer must not be null");
Assert.isNull(listenerContainer.getMessageListener(),
Expand Down Expand Up @@ -132,6 +134,16 @@ public void setBatchingStrategy(BatchingStrategy batchingStrategy) {
this.batchingStrategy = batchingStrategy;
}

/**
* Set to true to bind the source message in the header named
* {@link IntegrationMessageHeaderAccessor#SOURCE_DATA}.
* @param bindSourceMessage true to bind.
* @since 5.1.6
*/
public void setBindSourceMessage(boolean bindSourceMessage) {
this.bindSourceMessage = bindSourceMessage;
}

@Override
public String getComponentType() {
return "amqp:inbound-channel-adapter";
Expand Down Expand Up @@ -274,6 +286,9 @@ private org.springframework.messaging.Message<Object> createMessage(Message mess
if (AmqpInboundChannelAdapter.this.retryTemplate != null) {
headers.put(IntegrationMessageHeaderAccessor.DELIVERY_ATTEMPT, new AtomicInteger());
}
if (AmqpInboundChannelAdapter.this.bindSourceMessage) {
headers.put(IntegrationMessageHeaderAccessor.SOURCE_DATA, message);
}
final org.springframework.messaging.Message<Object> messagingMessage = getMessageBuilderFactory()
.withPayload(payload)
.copyHeaders(headers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public class AmqpInboundGateway extends MessagingGatewaySupport {

private BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(0, 0, 0L);

private boolean bindSourceMessage;

public AmqpInboundGateway(AbstractMessageListenerContainer listenerContainer) {
this(listenerContainer, new RabbitTemplate(listenerContainer.getConnectionFactory()), false);
}
Expand Down Expand Up @@ -192,6 +194,16 @@ public void setBatchingStrategy(BatchingStrategy batchingStrategy) {
this.batchingStrategy = batchingStrategy;
}

/**
* Set to true to bind the source message in the header named
* {@link IntegrationMessageHeaderAccessor#SOURCE_DATA}.
* @param bindSourceMessage true to bind.
* @since 5.1.6
*/
public void setBindSourceMessage(boolean bindSourceMessage) {
this.bindSourceMessage = bindSourceMessage;
}

@Override
public String getComponentType() {
return "amqp:inbound-gateway";
Expand Down Expand Up @@ -320,6 +332,9 @@ private org.springframework.messaging.Message<Object> convert(Message message, C
if (AmqpInboundGateway.this.retryTemplate != null) {
headers.put(IntegrationMessageHeaderAccessor.DELIVERY_ATTEMPT, new AtomicInteger());
}
if (AmqpInboundGateway.this.bindSourceMessage) {
headers.put(IntegrationMessageHeaderAccessor.SOURCE_DATA, message);
}
}
catch (RuntimeException e) {
MessageChannel errorChannel = getErrorChannel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,11 @@ protected boolean isRawMessageHeader() {
}

/**
* Set to true to include the raw spring-amqp message as a header
* with key {@link AmqpMessageHeaderErrorMessageStrategy#AMQP_RAW_MESSAGE},
* enabling callers to have access to the message to process errors.
* @param rawMessageHeader true to include the header.
* Set to true to include the raw spring-amqp message as a header with key
* {@link AmqpMessageHeaderErrorMessageStrategy#AMQP_RAW_MESSAGE}, enabling callers to
* have access to the message to process errors. The raw message is also added to the
* common header {@link IntegrationMessageHeaderAccessor#SOURCE_DATA}.
* @param rawMessageHeader true to include the headers.
*/
public void setRawMessageHeader(boolean rawMessageHeader) {
this.rawMessageHeader = rawMessageHeader;
Expand Down Expand Up @@ -210,6 +211,7 @@ protected AbstractIntegrationMessageBuilder<Object> doReceive() {
.setHeader(IntegrationMessageHeaderAccessor.ACKNOWLEDGMENT_CALLBACK, callback);
if (this.rawMessageHeader) {
builder.setHeader(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE, amqpMessage);
builder.setHeader(IntegrationMessageHeaderAccessor.SOURCE_DATA, amqpMessage);
}
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.amqp.rabbit.batch.SimpleBatchingStrategy;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.integration.IntegrationMessageHeaderAccessor;
import org.springframework.integration.StaticMessageHeaderAccessor;
import org.springframework.integration.acks.AcknowledgmentCallback.Status;
import org.springframework.integration.amqp.support.AmqpMessageHeaderErrorMessageStrategy;
Expand Down Expand Up @@ -74,6 +75,8 @@ public void testAck() throws Exception {
Message<?> received = source.receive();
assertThat(received.getHeaders().get(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE))
.isInstanceOf(org.springframework.amqp.core.Message.class);
assertThat(received.getHeaders().get(IntegrationMessageHeaderAccessor.SOURCE_DATA))
.isSameAs(received.getHeaders().get(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE));
assertThat(received.getHeaders().get(AmqpHeaders.CONSUMER_QUEUE)).isEqualTo("foo");
// make sure channel is not cached
org.springframework.amqp.rabbit.connection.Connection conn = ccf.createConnection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public void testInt2809JavaTypePropertiesToAmqp() throws Exception {

adapter.setOutputChannel(channel);
adapter.setBeanFactory(mock(BeanFactory.class));
adapter.setBindSourceMessage(true);
adapter.afterPropertiesSet();

Object payload = new Foo("bar1");
Expand All @@ -120,6 +121,8 @@ public void testInt2809JavaTypePropertiesToAmqp() throws Exception {

assertThat(result.getHeaders().get(AmqpHeaders.CHANNEL)).isSameAs(rabbitChannel);
assertThat(result.getHeaders().get(AmqpHeaders.DELIVERY_TAG)).isEqualTo(123L);
org.springframework.amqp.core.Message sourceData = StaticMessageHeaderAccessor.getSourceData(result);
assertThat(sourceData).isSameAs(amqpMessage);
}

@Test
Expand Down Expand Up @@ -153,6 +156,8 @@ public void testInt2809JavaTypePropertiesFromAmqp() throws Exception {
Message<?> result = new JsonToObjectTransformer().transform(receive);

assertThat(result.getPayload()).isEqualTo(payload);
org.springframework.amqp.core.Message sourceData = StaticMessageHeaderAccessor.getSourceData(result);
assertThat(sourceData).isNull();
}

@Test
Expand Down Expand Up @@ -409,10 +414,11 @@ public void testBatchdAdapter() throws Exception {
public void testBatchGateway() throws Exception {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(mock(ConnectionFactory.class));
container.setDeBatchingEnabled(false);
AmqpInboundGateway adapter = new AmqpInboundGateway(container);
AmqpInboundGateway gateway = new AmqpInboundGateway(container);
QueueChannel out = new QueueChannel();
adapter.setRequestChannel(out);
adapter.afterPropertiesSet();
gateway.setRequestChannel(out);
gateway.setBindSourceMessage(true);
gateway.afterPropertiesSet();
ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
SimpleBatchingStrategy bs = new SimpleBatchingStrategy(2, 10_000, 10_000L);
MessageProperties messageProperties = new MessageProperties();
Expand All @@ -426,6 +432,8 @@ public void testBatchGateway() throws Exception {
Message<?> received = out.receive();
assertThat(received).isNotNull();
assertThat(((List<String>) received.getPayload())).contains("test1", "test2");
org.springframework.amqp.core.Message sourceData = StaticMessageHeaderAccessor.getSourceData(received);
assertThat(sourceData).isSameAs(batched.getMessage());
}

public static class Foo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public class IntegrationMessageHeaderAccessor extends MessageHeaderAccessor {

public static final String ACKNOWLEDGMENT_CALLBACK = "acknowledgmentCallback";

/**
* Raw source message.
*/
public static final String SOURCE_DATA = "sourceData";

private static final BiFunction<String, String, String> TYPE_VERIFY_MESSAGE_FUNCTION =
(name, trailer) -> "The '" + name + trailer;

Expand Down Expand Up @@ -153,6 +158,18 @@ public AtomicInteger getDeliveryAttempt() {
return getHeader(DELIVERY_ATTEMPT, AtomicInteger.class);
}

/**
* Get the source data header, if present.
* @param <T> the data type.
* @return the source header.
* @since 5.1.6
*/
@SuppressWarnings("unchecked")
@Nullable
public <T> T getSourceData() {
return (T) getHeader(SOURCE_DATA);
}

@SuppressWarnings("unchecked")
@Nullable
public <T> T getHeader(String key, Class<T> type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,10 @@ public static AcknowledgmentCallback getAcknowledgmentCallback(Message<?> messag
AcknowledgmentCallback.class);
}

@SuppressWarnings("unchecked")
@Nullable
public static <T> T getSourceData(Message<?> message) {
return (T) message.getHeaders().get(IntegrationMessageHeaderAccessor.SOURCE_DATA);
}

}