Merge pull request #117 from markfisher/INT-2170
add setMessageConverter to inbound gateway
This commit is contained in:
@@ -25,6 +25,7 @@ import org.springframework.amqp.core.MessageListener;
|
||||
import org.springframework.amqp.core.MessagePostProcessor;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer;
|
||||
import org.springframework.amqp.support.converter.MessageConverter;
|
||||
import org.springframework.amqp.support.converter.SimpleMessageConverter;
|
||||
import org.springframework.integration.amqp.support.AmqpHeaderMapper;
|
||||
import org.springframework.integration.amqp.support.DefaultAmqpHeaderMapper;
|
||||
@@ -45,7 +46,7 @@ public class AmqpInboundGateway extends MessagingGatewaySupport {
|
||||
|
||||
private final AbstractMessageListenerContainer messageListenerContainer;
|
||||
|
||||
private final SimpleMessageConverter messageConverter = new SimpleMessageConverter();
|
||||
private volatile MessageConverter amqpMessageConverter = new SimpleMessageConverter();
|
||||
|
||||
private volatile AmqpHeaderMapper headerMapper = new DefaultAmqpHeaderMapper();
|
||||
|
||||
@@ -62,6 +63,12 @@ public class AmqpInboundGateway extends MessagingGatewaySupport {
|
||||
}
|
||||
|
||||
|
||||
public void setMessageConverter(MessageConverter messageConverter) {
|
||||
Assert.notNull(messageConverter, "MessageConverter must not be null");
|
||||
this.amqpMessageConverter = messageConverter;
|
||||
this.amqpTemplate.setMessageConverter(messageConverter);
|
||||
}
|
||||
|
||||
public void setHeaderMapper(AmqpHeaderMapper headerMapper) {
|
||||
Assert.notNull(headerMapper, "headerMapper must not be null");
|
||||
this.headerMapper = headerMapper;
|
||||
@@ -71,7 +78,7 @@ public class AmqpInboundGateway extends MessagingGatewaySupport {
|
||||
protected void onInit() throws Exception {
|
||||
this.messageListenerContainer.setMessageListener(new MessageListener() {
|
||||
public void onMessage(Message message) {
|
||||
Object payload = messageConverter.fromMessage(message);
|
||||
Object payload = amqpMessageConverter.fromMessage(message);
|
||||
Map<String, ?> headers = headerMapper.toHeaders(message.getMessageProperties());
|
||||
org.springframework.integration.Message<?> request =
|
||||
MessageBuilder.withPayload(payload).copyHeaders(headers).build();
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:si-amqp="http://www.springframework.org/schema/integration/amqp"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/integration/amqp http://www.springframework.org/schema/integration/amqp/spring-integration-amqp.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<int:channel id="requests"/>
|
||||
|
||||
<si-amqp:inbound-gateway id="gateway" request-channel="requests" queue-names="test"
|
||||
connection-factory="rabbitConnectionFactory" message-converter="testConverter"/>
|
||||
|
||||
<bean id="rabbitConnectionFactory" class="org.mockito.Mockito" factory-method="mock">
|
||||
<constructor-arg value="org.springframework.amqp.rabbit.connection.ConnectionFactory"/>
|
||||
</bean>
|
||||
|
||||
<bean id="testConverter" class="org.springframework.integration.amqp.config.AmqpInboundGatewayParserTests$TestConverter"/>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2002-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.amqp.config;
|
||||
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.amqp.support.converter.MessageConverter;
|
||||
import org.springframework.amqp.support.converter.SimpleMessageConverter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @since 2.1
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class AmqpInboundGatewayParserTests {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void customMessageConverter() {
|
||||
Object gateway = context.getBean("gateway");
|
||||
MessageConverter gatewayConverter = TestUtils.getPropertyValue(gateway, "amqpMessageConverter", MessageConverter.class);
|
||||
MessageConverter templateConverter = TestUtils.getPropertyValue(gateway, "amqpTemplate.messageConverter", MessageConverter.class);
|
||||
TestConverter testConverter = context.getBean("testConverter", TestConverter.class);
|
||||
assertSame(testConverter, gatewayConverter);
|
||||
assertSame(testConverter, templateConverter);
|
||||
}
|
||||
|
||||
|
||||
private static class TestConverter extends SimpleMessageConverter {}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user