Added 'message-converter' attribute for <jms-source> when creating a JmsMessageDrivenSourceAdapter (INT-102).

This commit is contained in:
Mark Fisher
2008-02-13 15:56:11 +00:00
parent ecce093f6e
commit 3ae8672618
7 changed files with 172 additions and 2 deletions

View File

@@ -54,6 +54,10 @@ public class JmsSourceAdapterParser extends AbstractSingleBeanDefinitionParser {
private static final String POLL_PERIOD_PROPERTY = "period";
private static final String MESSAGE_CONVERTER_ATTRIBUTE = "message-converter";
private static final String MESSAGE_CONVERTER_PROPERTY = "messageConverter";
protected Class<?> getBeanClass(Element element) {
if (StringUtils.hasText(element.getAttribute(POLL_PERIOD_ATTRIBUTE))) {
@@ -87,6 +91,11 @@ public class JmsSourceAdapterParser extends AbstractSingleBeanDefinitionParser {
throw new BeanCreationException("'" + POLL_PERIOD_ATTRIBUTE +
"' is required for a " + JmsPollingSourceAdapter.class.getSimpleName());
}
if (StringUtils.hasText(element.getAttribute(MESSAGE_CONVERTER_ATTRIBUTE))) {
throw new BeanCreationException("The '" + MESSAGE_CONVERTER_ATTRIBUTE + "' attribute is not supported for a " +
JmsPollingSourceAdapter.class.getSimpleName() + ". Consider providing a '" + JMS_TEMPLATE_ATTRIBUTE +
"' reference where the template contains a 'messageConverter' property instead.");
}
builder.addPropertyValue(POLL_PERIOD_PROPERTY, pollPeriod);
String jmsTemplate = element.getAttribute(JMS_TEMPLATE_ATTRIBUTE);
String connectionFactory = element.getAttribute(CONNECTION_FACTORY_ATTRIBUTE);
@@ -121,9 +130,10 @@ public class JmsSourceAdapterParser extends AbstractSingleBeanDefinitionParser {
String connectionFactory = element.getAttribute(CONNECTION_FACTORY_ATTRIBUTE);
String destination = element.getAttribute(DESTINATION_ATTRIBUTE);
String destinationName = element.getAttribute(DESTINATION_NAME_ATTRIBUTE);
String messageConverter = element.getAttribute(MESSAGE_CONVERTER_ATTRIBUTE);
if (StringUtils.hasText(element.getAttribute(JMS_TEMPLATE_ATTRIBUTE))) {
throw new BeanCreationException(JmsMessageDrivenSourceAdapter.class.getSimpleName() +
" does not accept a '" + JMS_TEMPLATE_ATTRIBUTE + "' reference, both " +
" does not accept a '" + JMS_TEMPLATE_ATTRIBUTE + "' reference. Both " +
"'" + CONNECTION_FACTORY_ATTRIBUTE + "' and '" + DESTINATION_ATTRIBUTE +
"' (or '" + DESTINATION_NAME_ATTRIBUTE + "') must be provided.");
}
@@ -139,7 +149,10 @@ public class JmsSourceAdapterParser extends AbstractSingleBeanDefinitionParser {
else {
throw new BeanCreationException("Both '" + CONNECTION_FACTORY_ATTRIBUTE + "' and '" +
DESTINATION_ATTRIBUTE + "' (or '" + DESTINATION_NAME_ATTRIBUTE + "') must be provided.");
}
}
if (StringUtils.hasText(messageConverter)) {
builder.addPropertyReference(MESSAGE_CONVERTER_PROPERTY, messageConverter);
}
}
}

View File

@@ -248,6 +248,7 @@
<xsd:attribute name="destination-name" type="xsd:string"/>
<xsd:attribute name="channel" type="xsd:string" use="required"/>
<xsd:attribute name="poll-period" type="xsd:int"/>
<xsd:attribute name="message-converter" type="xsd:string"/>
</xsd:complexType>
</xsd:element>

View File

@@ -19,6 +19,10 @@ package org.springframework.integration.adapter.jms.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
@@ -28,6 +32,8 @@ import org.springframework.integration.adapter.jms.JmsMessageDrivenSourceAdapter
import org.springframework.integration.adapter.jms.JmsPollingSourceAdapter;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.Message;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;
/**
* @author Mark Fisher
@@ -104,6 +110,19 @@ public class JmsSourceAdapterParserTests {
context.stop();
}
@Test
public void testMessageDrivenAdapterWithMessageConverter() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"messageDrivenAdapterWithMessageConverter.xml", this.getClass());
JmsMessageDrivenSourceAdapter adapter = (JmsMessageDrivenSourceAdapter) context.getBean("adapter");
assertEquals(JmsMessageDrivenSourceAdapter.class, adapter.getClass());
MessageChannel channel = (MessageChannel) context.getBean("channel");
Message<?> message = channel.receive(3000);
assertNotNull("message should not be null", message);
assertEquals("converted-test-message", message.getPayload());
context.stop();
}
@Test(expected=BeanDefinitionStoreException.class)
public void testPollingAdapterWithConnectionFactoryOnly() {
try {
@@ -159,4 +178,19 @@ public class JmsSourceAdapterParserTests {
}
}
public static class TestMessageConverter implements MessageConverter {
public Object fromMessage(javax.jms.Message message) throws JMSException, MessageConversionException {
String original = ((TextMessage) message).getText();
return "converted-" + original;
}
public javax.jms.Message toMessage(Object object, Session session) throws JMSException,
MessageConversionException {
return null;
}
}
}

View File

@@ -0,0 +1,32 @@
<?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="channel"/>
<si:jms-source id="adapter"
connection-factory="connectionFactory"
destination="testDestination"
channel="channel"
message-converter="converter"/>
<bean id="converter" class="org.springframework.integration.adapter.jms.config.JmsSourceAdapterParserTests$TestMessageConverter"/>
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg>
<bean class="org.springframework.integration.adapter.jms.StubConnection">
<constructor-arg value="test-message"/>
</bean>
</constructor-arg>
</bean>
<bean id="testDestination" class="org.springframework.integration.adapter.jms.StubDestination"/>
</beans>

View File

@@ -80,6 +80,18 @@ public class EndpointParserTests {
assertEquals("test", bean.getMessage());
}
@Test
public void testHandlerChainEndpoint() throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"endpointWithHandlerChainElement.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
MessageChannel replyChannel = (MessageChannel) context.getBean("replyChannel");
channel.send(new StringMessage("test"));
Message<?> reply = replyChannel.receive(500);
assertNotNull(reply);
assertEquals("test-1-2-3", reply.getPayload());
}
@Test
public void testDefaultConcurrency() throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(

View File

@@ -0,0 +1,40 @@
/*
* 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.config;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class TestConcatenatingHandler implements MessageHandler {
private String value;
public TestConcatenatingHandler(String value) {
this.value = value;
}
public Message<?> handle(Message<?> message) {
return new StringMessage(message.getPayload() + this.value);
}
}

View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
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">
<message-bus/>
<channel id="testChannel" capacity="50"/>
<channel id="replyChannel"/>
<handler-chain id="chain">
<handler ref="handler1"/>
<handler ref="handler2"/>
<handler ref="handler3"/>
</handler-chain>
<endpoint input-channel="testChannel" handler-ref="chain" default-output-channel="replyChannel">
<schedule period="100"/>
</endpoint>
<beans:bean id="handler1" class="org.springframework.integration.config.TestConcatenatingHandler">
<beans:constructor-arg value="-1"/>
</beans:bean>
<beans:bean id="handler2" class="org.springframework.integration.config.TestConcatenatingHandler">
<beans:constructor-arg value="-2"/>
</beans:bean>
<beans:bean id="handler3" class="org.springframework.integration.config.TestConcatenatingHandler">
<beans:constructor-arg value="-3"/>
</beans:bean>
</beans:beans>