JmsTargetAdapter now passes any JMS attributes from the Spring Integration MessageHeader to the JMS Message prior to sending to its Destination (INT-97). JmsTargetAdapter also supports the 'destinationName' property now in addition to 'destination'. The 'jms-target' element accepts a 'destination-name' attribute as well (INT-96).

This commit is contained in:
Mark Fisher
2008-02-05 00:09:40 +00:00
parent 76c19438ff
commit 65abee9eb6
9 changed files with 306 additions and 18 deletions

View File

@@ -0,0 +1,102 @@
/*
* Copyright 2002-2007 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.adapter.jms;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import javax.jms.Destination;
import javax.jms.JMSException;
import org.junit.Test;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class DefaultJmsMessagePostProcessorTests {
@Test
public void testJmsReplyTo() throws JMSException {
StringMessage message = new StringMessage("test1");
Destination replyTo = new Destination() {};
message.getHeader().setAttribute(JmsTargetAdapter.JMS_REPLY_TO, replyTo);
DefaultJmsMessagePostProcessor postProcessor = new DefaultJmsMessagePostProcessor();
javax.jms.Message jmsMessage = new StubTextMessage();
postProcessor.postProcessJmsMessage(jmsMessage, message.getHeader());
assertNotNull(jmsMessage.getJMSReplyTo());
assertSame(replyTo, jmsMessage.getJMSReplyTo());
}
@Test
public void testJmsReplyToIgnoredIfIncorrectType() throws JMSException {
StringMessage message = new StringMessage("test1");
message.getHeader().setAttribute(JmsTargetAdapter.JMS_REPLY_TO, "not-a-destination");
DefaultJmsMessagePostProcessor postProcessor = new DefaultJmsMessagePostProcessor();
javax.jms.Message jmsMessage = new StubTextMessage();
postProcessor.postProcessJmsMessage(jmsMessage, message.getHeader());
assertNull(jmsMessage.getJMSReplyTo());
}
@Test
public void testJmsCorrelationId() throws JMSException {
StringMessage message = new StringMessage("test1");
String jmsCorrelationId = "ABC-123";
message.getHeader().setAttribute(JmsTargetAdapter.JMS_CORRELATION_ID, jmsCorrelationId);
DefaultJmsMessagePostProcessor postProcessor = new DefaultJmsMessagePostProcessor();
javax.jms.Message jmsMessage = new StubTextMessage();
postProcessor.postProcessJmsMessage(jmsMessage, message.getHeader());
assertNotNull(jmsMessage.getJMSCorrelationID());
assertEquals(jmsCorrelationId, jmsMessage.getJMSCorrelationID());
}
@Test
public void testJmsCorrelationIdIgnoredIfIncorrectType() throws JMSException {
StringMessage message = new StringMessage("test1");
message.getHeader().setAttribute(JmsTargetAdapter.JMS_CORRELATION_ID, new Integer(123));
DefaultJmsMessagePostProcessor postProcessor = new DefaultJmsMessagePostProcessor();
javax.jms.Message jmsMessage = new StubTextMessage();
postProcessor.postProcessJmsMessage(jmsMessage, message.getHeader());
assertNull(jmsMessage.getJMSCorrelationID());
}
@Test
public void testJmsType() throws JMSException {
StringMessage message = new StringMessage("test1");
String jmsType = "testing";
message.getHeader().setAttribute(JmsTargetAdapter.JMS_TYPE, jmsType);
DefaultJmsMessagePostProcessor postProcessor = new DefaultJmsMessagePostProcessor();
javax.jms.Message jmsMessage = new StubTextMessage();
postProcessor.postProcessJmsMessage(jmsMessage, message.getHeader());
assertNotNull(jmsMessage.getJMSType());
assertEquals(jmsType, jmsMessage.getJMSType());
}
@Test
public void testJmsTypeIgnoredIfIncorrectType() throws JMSException {
StringMessage message = new StringMessage("test1");
message.getHeader().setAttribute(JmsTargetAdapter.JMS_TYPE, new Integer(123));
DefaultJmsMessagePostProcessor postProcessor = new DefaultJmsMessagePostProcessor();
javax.jms.Message jmsMessage = new StubTextMessage();
postProcessor.postProcessJmsMessage(jmsMessage, message.getHeader());
assertNull(jmsMessage.getJMSType());
}
}

View File

@@ -10,6 +10,13 @@ public class StubTextMessage implements TextMessage {
private String text;
private Destination replyTo;
private String correlationID;
private String type;
public String getText() throws JMSException {
return this.text;
}
@@ -48,7 +55,7 @@ public class StubTextMessage implements TextMessage {
}
public String getJMSCorrelationID() throws JMSException {
return null;
return this.correlationID;
}
public byte[] getJMSCorrelationIDAsBytes() throws JMSException {
@@ -80,7 +87,7 @@ public class StubTextMessage implements TextMessage {
}
public Destination getJMSReplyTo() throws JMSException {
return null;
return this.replyTo;
}
public long getJMSTimestamp() throws JMSException {
@@ -88,7 +95,7 @@ public class StubTextMessage implements TextMessage {
}
public String getJMSType() throws JMSException {
return null;
return this.type;
}
public long getLongProperty(String name) throws JMSException {
@@ -131,6 +138,7 @@ public class StubTextMessage implements TextMessage {
}
public void setJMSCorrelationID(String correlationID) throws JMSException {
this.correlationID = correlationID;
}
public void setJMSCorrelationIDAsBytes(byte[] correlationID) throws JMSException {
@@ -155,12 +163,14 @@ public class StubTextMessage implements TextMessage {
}
public void setJMSReplyTo(Destination replyTo) throws JMSException {
this.replyTo = replyTo;
}
public void setJMSTimestamp(long timestamp) throws JMSException {
}
public void setJMSType(String type) throws JMSException {
this.type = type;
}
public void setLongProperty(String name, long value) throws JMSException {

View File

@@ -39,4 +39,14 @@ public class JmsTargetAdapterParserTests {
assertEquals("testChannel", endpoint.getSubscription().getChannelName());
}
@Test
public void testTargetAdapterWithConnectionFactoryAndDestinationName() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"targetAdapterWithConnectionFactoryAndDestinationName.xml", this.getClass());
DefaultMessageEndpoint endpoint = (DefaultMessageEndpoint) context.getBean("adapter");
assertEquals(JmsTargetAdapter.class, endpoint.getHandler().getClass());
assertEquals("adapter", endpoint.getName());
assertEquals("testChannel", endpoint.getSubscription().getChannelName());
}
}

View File

@@ -0,0 +1,27 @@
<?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="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<si:message-bus/>
<si:channel id="testChannel"/>
<si:jms-target id="adapter"
connection-factory="connectionFactory"
destination-name="queue.test"
channel="testChannel"/>
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg>
<bean class="org.springframework.integration.adapter.jms.StubConnection">
<constructor-arg value="target-test"/>
</bean>
</constructor-arg>
</bean>
</beans>