INT-1011, INT-1022, INT-1023 added overwrite attributes for JMS header-enricher, added support for SpEL expression in the header enricher elements, and refactored header-mapping so that numeric types convert to Strings for jmsCorrelationId

This commit is contained in:
Mark Fisher
2010-05-05 17:06:00 +00:00
parent 3415ad7ab8
commit 341a5c02f8
5 changed files with 66 additions and 17 deletions

View File

@@ -63,15 +63,18 @@ public class DefaultJmsHeaderMapper implements JmsHeaderMapper {
public void fromHeaders(MessageHeaders headers, javax.jms.Message jmsMessage) {
try {
Object jmsCorrelationId = headers.get(JmsHeaders.CORRELATION_ID);
if (jmsCorrelationId != null && (jmsCorrelationId instanceof String)) {
if (jmsCorrelationId instanceof Number) {
jmsCorrelationId = ((Number) jmsCorrelationId).toString();
}
if (jmsCorrelationId instanceof String) {
jmsMessage.setJMSCorrelationID((String) jmsCorrelationId);
}
Object jmsReplyTo = headers.get(JmsHeaders.REPLY_TO);
if (jmsReplyTo != null && (jmsReplyTo instanceof Destination)) {
if (jmsReplyTo instanceof Destination) {
jmsMessage.setJMSReplyTo((Destination) jmsReplyTo);
}
Object jmsType = headers.get(JmsHeaders.TYPE);
if (jmsType != null && (jmsType instanceof String)) {
if (jmsType instanceof String) {
jmsMessage.setJMSType((String) jmsType);
}
Set<String> attributeNames = headers.keySet();

View File

@@ -808,9 +808,18 @@
</xsd:annotation>
</xsd:element>
</xsd:choice>
<!--
<xsd:attribute name="overwrite" type="xsd:string"/>
-->
<xsd:attribute name="default-overwrite">
<xsd:annotation>
<xsd:documentation>
Specify the default boolean value for whether to overwrite existing header values. This will only take effect for
sub-elements that do not provide their own 'overwrite' attribute. If the 'default-overwrite' attribute is not
provided, then the specified header values will NOT overwrite any existing ones with the same header names.
</xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:union memberTypes="xsd:boolean xsd:string"/>
</xsd:simpleType>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
@@ -820,14 +829,23 @@
<xsd:complexContent>
<xsd:extension base="refOnlyHeaderType">
<xsd:attribute name="value" type="xsd:string" />
<!-- xsd:attribute name="expression" type="xsd:string" / -->
<xsd:attribute name="expression" type="xsd:string" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="refOnlyHeaderType">
<xsd:attribute name="ref" type="xsd:string" />
<!-- TODO add 'overwrite' -->
<xsd:attribute name="overwrite">
<xsd:annotation>
<xsd:documentation>
Boolean value to indicate whether this header value should overwrite an existing header value for the same name.
</xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:union memberTypes="xsd:boolean xsd:string"/>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
<xsd:complexType name="jmsAdapterType">

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2010 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.
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import java.util.Date;
import java.util.Map;
import javax.jms.Destination;
@@ -73,12 +74,22 @@ public class DefaultJmsHeaderMapperTests {
}
@Test
public void testJmsCorrelationIdIgnoredIfIncorrectType() throws JMSException {
public void testJmsCorrelationIdNumberConvertsToString() throws JMSException {
Message<String> message = MessageBuilder.withPayload("test")
.setHeader(JmsHeaders.CORRELATION_ID, new Integer(123)).build();
DefaultJmsHeaderMapper mapper = new DefaultJmsHeaderMapper();
javax.jms.Message jmsMessage = new StubTextMessage();
mapper.fromHeaders(message.getHeaders(), jmsMessage);
assertEquals("123", jmsMessage.getJMSCorrelationID());
}
@Test
public void testJmsCorrelationIdIgnoredIfIncorrectType() throws JMSException {
Message<String> message = MessageBuilder.withPayload("test")
.setHeader(JmsHeaders.CORRELATION_ID, new Date()).build();
DefaultJmsHeaderMapper mapper = new DefaultJmsHeaderMapper();
javax.jms.Message jmsMessage = new StubTextMessage();
mapper.fromHeaders(message.getHeaders(), jmsMessage);
assertNull(jmsMessage.getJMSCorrelationID());
}

View File

@@ -10,11 +10,15 @@
http://www.springframework.org/schema/integration/jms
http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd">
<jms:header-enricher input-channel="input" output-channel="output">
<jms:header-enricher input-channel="valueTestInput" output-channel="output">
<jms:reply-to ref="testDestination"/>
<jms:correlation-id value="ABC"/>
</jms:header-enricher>
<jms:header-enricher input-channel="expressionTestInput" output-channel="output">
<jms:correlation-id expression="100 + (10 * 2) + 3"/>
</jms:header-enricher>
<bean id="testDestination" class="org.springframework.integration.jms.StubDestination"/>
<integration:channel id="output">

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 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.
@@ -42,7 +42,10 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
public class JmsHeaderEnricherTests {
@Autowired
private MessageChannel input;
private MessageChannel valueTestInput;
@Autowired
private MessageChannel expressionTestInput;
@Autowired
private PollableChannel output;
@@ -51,8 +54,8 @@ public class JmsHeaderEnricherTests {
private Destination testDestination;
@Test // INT-804
public void verifyReplyTo() throws Exception {
input.send(new StringMessage("test"));
public void verifyReplyToValue() throws Exception {
valueTestInput.send(new StringMessage("test"));
Message<?> result = output.receive(0);
assertEquals(testDestination, result.getHeaders().get(JmsHeaders.REPLY_TO));
HeaderMappingMessageConverter converter = new HeaderMappingMessageConverter();
@@ -61,8 +64,8 @@ public class JmsHeaderEnricherTests {
}
@Test
public void verifyCorrelationId() throws Exception {
input.send(new StringMessage("test"));
public void verifyCorrelationIdValue() throws Exception {
valueTestInput.send(new StringMessage("test"));
Message<?> result = output.receive(0);
assertEquals("ABC", result.getHeaders().get(JmsHeaders.CORRELATION_ID));
HeaderMappingMessageConverter converter = new HeaderMappingMessageConverter();
@@ -70,4 +73,14 @@ public class JmsHeaderEnricherTests {
assertEquals("ABC", jmsMessage.getJMSCorrelationID());
}
@Test // see INT-1122 and INT-1123
public void verifyCorrelationIdExpression() throws Exception {
expressionTestInput.send(new StringMessage("test"));
Message<?> result = output.receive(0);
assertEquals(123, result.getHeaders().get(JmsHeaders.CORRELATION_ID));
HeaderMappingMessageConverter converter = new HeaderMappingMessageConverter();
javax.jms.Message jmsMessage = converter.toMessage(result, new StubSession("foo"));
assertEquals("123", jmsMessage.getJMSCorrelationID());
}
}