MessageMapper now defines toMessage() and fromMessage() methods, and the MessageCreator has been removed.

This commit is contained in:
Mark Fisher
2008-09-25 18:27:52 +00:00
parent ec304b2c10
commit 8b24cd7d6b
16 changed files with 118 additions and 363 deletions

View File

@@ -159,7 +159,6 @@
<xsd:attribute name="request-timeout" type="xsd:long"/>
<xsd:attribute name="reply-timeout" type="xsd:long"/>
<xsd:attribute name="message-mapper" type="xsd:string"/>
<xsd:attribute name="message-creator" type="xsd:string"/>
</xsd:complexType>
</xsd:element>

View File

@@ -14,23 +14,35 @@
* limitations under the License.
*/
package org.springframework.integration.message;
package org.springframework.integration.gateway;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageBuilder;
/**
* A simple implementation of {@link MessageCreator} that uses the provide value
* as the {@link Message Message's} payload.
* A default implementation of both the {@link MessageMapper} and
* {@link OutboundMessageMapper} strategy interfaces.
*
* @author Mark Fisher
*/
public class DefaultMessageCreator<T> implements MessageCreator<T,T> {
public class DefaultMessageMapper<T> implements MessageMapper<T> {
@SuppressWarnings("unchecked")
public Message<T> createMessage(T object) {
//prevent nesting messages
if (object instanceof Message) {
return (Message<T>) object;
public T fromMessage(Message<?> message) {
if (message == null || message.getPayload() == null) {
return null;
}
return (object != null) ? new GenericMessage<T>(object) : null;
return (T) message.getPayload();
}
public Message<?> toMessage(T object) {
if (object == null) {
return null;
}
if (object instanceof Message) {
return (Message<?>) object;
}
return MessageBuilder.withPayload(object).build();
}
}

View File

@@ -14,18 +14,19 @@
* limitations under the License.
*/
package org.springframework.integration.message;
package org.springframework.integration.gateway;
import org.springframework.integration.message.Message;
/**
* Strategy interface for mapping from a {@link Message} to an Object.
* Strategy interface for mapping between an Object and a {@link Message}.
*
* @author Mark Fisher
*/
public interface MessageMapper<P, O> {
public interface MessageMapper<T> {
/**
* Map from the given {@link Message} to an Object.
*/
O mapMessage(Message<P> message);
Message<?> toMessage(T object);
T fromMessage(Message<?> message);
}

View File

@@ -1,63 +0,0 @@
/*
* Copyright 2002-2008 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.gateway;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.MessageChannelTemplate;
/**
* A convenient base class providing access to a {@link MessageChannelTemplate} and exposing setter methods for
* configuring request and reply {@link MessageChannel MessageChannels}. May be used as a base class for framework
* components so that the details of messaging are well-encapsulated and hidden from application code. For example,
* see {@link SimpleMessagingGateway}.
*
* @author Mark Fisher
*/
public abstract class MessagingGatewaySupport {
private final MessageChannelTemplate channelTemplate = new MessageChannelTemplate();
/**
* Set the timeout value for sending request messages. If not
* explicitly configured, the default is an indefinite timeout.
*
* @param requestTimeout the timeout value in milliseconds
*/
public void setRequestTimeout(long requestTimeout) {
this.channelTemplate.setSendTimeout(requestTimeout);
}
/**
* Set the timeout value for receiving reply messages. If not
* explicitly configured, the default is an indefinite timeout.
*
* @param replyTimeout the timeout value in milliseconds
*/
public void setReplyTimeout(long replyTimeout) {
this.channelTemplate.setReceiveTimeout(replyTimeout);
}
/**
* Retrieve the {@link MessageChannelTemplate} for performing
* send and receive operations across channels.
*/
protected final MessageChannelTemplate getChannelTemplate() {
return this.channelTemplate;
}
}

View File

@@ -21,34 +21,37 @@ import org.springframework.integration.ConfigurationException;
import org.springframework.integration.bus.MessageBus;
import org.springframework.integration.bus.MessageBusAware;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.MessageChannelTemplate;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.endpoint.MessagingGateway;
import org.springframework.integration.message.DefaultMessageCreator;
import org.springframework.integration.message.DefaultMessageMapper;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageCreator;
import org.springframework.integration.message.MessageDeliveryException;
import org.springframework.integration.message.MessageMapper;
import org.springframework.util.Assert;
/**
* A general purpose class that supports a variety of message exchanges. Useful for connecting application code to
* {@link MessageChannel MessageChannels} for sending, receiving, or request-reply operations. The sending methods
* accept any Object as the parameter value (i.e. it is not required to be a Message). A custom {@link MessageCreator}
* may be provided for creating Messages from the Objects. Likewise return values may be any Object and a custom
* implementation of the {@link MessageMapper} strategy may be provided for mapping a reply Message to an Object.
* A convenient base class for connecting application code to
* {@link MessageChannel}s for sending, receiving, or request-reply operations.
* Exposes setters for configuring request and reply {@link MessageChannel}s as
* well as the timeout values for sending and receiving Messages.
*
* <p>By default, each request Message will be created with the method
* parameter as its payload, and each reply Message's payload will be the
* return value. To provide custom behavior for object-to-request and/or
* reply-to-object conversion, implement and set a 'messageMapper'.
*
* @see MessageMapper
*
* @author Mark Fisher
*/
public class SimpleMessagingGateway extends MessagingGatewaySupport implements MessagingGateway, MessageBusAware, InitializingBean {
public class SimpleMessagingGateway implements MessagingGateway, MessageBusAware, InitializingBean {
private volatile MessageChannel requestChannel;
private volatile MessageChannel replyChannel;
private volatile MessageCreator messageCreator = new DefaultMessageCreator();
private volatile MessageMapper messageMapper = new DefaultMessageMapper<Object>();
private volatile MessageMapper messageMapper = new DefaultMessageMapper();
private final MessageChannelTemplate channelTemplate = new MessageChannelTemplate();
private volatile ReplyMessageCorrelator replyMessageCorrelator;
@@ -85,12 +88,27 @@ public class SimpleMessagingGateway extends MessagingGatewaySupport implements M
this.replyChannel = replyChannel;
}
public void setMessageCreator(MessageCreator<?, ?> messageCreator) {
Assert.notNull(messageCreator, "messageCreator must not be null");
this.messageCreator = messageCreator;
/**
* Set the timeout value for sending request messages. If not
* explicitly configured, the default is an indefinite timeout.
*
* @param requestTimeout the timeout value in milliseconds
*/
public void setRequestTimeout(long requestTimeout) {
this.channelTemplate.setSendTimeout(requestTimeout);
}
public void setMessageMapper(MessageMapper<?, ?> messageMapper) {
/**
* Set the timeout value for receiving reply messages. If not
* explicitly configured, the default is an indefinite timeout.
*
* @param replyTimeout the timeout value in milliseconds
*/
public void setReplyTimeout(long replyTimeout) {
this.channelTemplate.setReceiveTimeout(replyTimeout);
}
public void setMessageMapper(MessageMapper<?> messageMapper) {
Assert.notNull(messageMapper, "messageMapper must not be null");
this.messageMapper = messageMapper;
}
@@ -108,12 +126,10 @@ public class SimpleMessagingGateway extends MessagingGatewaySupport implements M
throw new IllegalStateException(
"send is not supported, because no request channel has been configured");
}
Message<?> message = (object instanceof Message) ? (Message) object :
this.messageCreator.createMessage(object);
if (message != null) {
if (!this.getChannelTemplate().send(message, this.requestChannel)) {
throw new MessageDeliveryException(message, "failed to send Message to channel");
}
Message<?> message = this.messageMapper.toMessage(object);
Assert.notNull(message, "message must not be null");
if (!this.channelTemplate.send(message, this.requestChannel)) {
throw new MessageDeliveryException(message, "failed to send Message to channel");
}
}
@@ -122,8 +138,8 @@ public class SimpleMessagingGateway extends MessagingGatewaySupport implements M
throw new IllegalStateException(
"no-arg receive is not supported, because no pollable reply channel has been configured");
}
Message<?> message = this.getChannelTemplate().receive((PollableChannel) this.replyChannel);
return (message != null) ? this.messageMapper.mapMessage(message) : null;
Message<?> message = this.channelTemplate.receive((PollableChannel) this.replyChannel);
return this.messageMapper.fromMessage(message);
}
public Object sendAndReceive(Object object) {
@@ -135,19 +151,16 @@ public class SimpleMessagingGateway extends MessagingGatewaySupport implements M
}
private Object sendAndReceive(Object object, boolean shouldMapMessage) {
Message<?> request = (object instanceof Message) ? (Message) object :
this.messageCreator.createMessage(object);
if (request == null) {
return null;
}
Message<?> request = this.messageMapper.toMessage(object);
Message<?> reply = this.sendAndReceiveMessage(request);
if (!shouldMapMessage) {
return reply;
}
return (reply != null) ? this.messageMapper.mapMessage(reply) : null;
return this.messageMapper.fromMessage(reply);
}
private Message<?> sendAndReceiveMessage(Message<?> message) {
Assert.notNull(message, "request message must not be null");
if (this.requestChannel == null) {
throw new MessageDeliveryException(message,
"No request channel available. Cannot send request message.");
@@ -155,7 +168,7 @@ public class SimpleMessagingGateway extends MessagingGatewaySupport implements M
if (this.replyChannel != null && this.replyMessageCorrelator == null) {
this.registerReplyMessageCorrelator();
}
return this.getChannelTemplate().sendAndReceive(message, this.requestChannel);
return this.channelTemplate.sendAndReceive(message, this.requestChannel);
}
private void registerReplyMessageCorrelator() {

View File

@@ -20,10 +20,9 @@ import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.core.Conventions;
import org.springframework.integration.config.IntegrationNamespaceUtils;
import org.springframework.integration.gateway.GatewayProxyFactoryBean;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* Parser for the &lt;gateway/&gt; element.
@@ -33,7 +32,7 @@ import org.springframework.util.StringUtils;
public class GatewayParser extends AbstractSimpleBeanDefinitionParser {
private static String[] referenceAttributes = new String[] {
"request-channel", "reply-channel", "message-mapper", "message-creator"
"request-channel", "reply-channel", "message-mapper"
};
@@ -49,13 +48,9 @@ public class GatewayParser extends AbstractSimpleBeanDefinitionParser {
}
@Override
protected void postProcess(BeanDefinitionBuilder beanDefinition, Element element) {
protected void postProcess(BeanDefinitionBuilder builder, Element element) {
for (String attributeName : referenceAttributes) {
String beanName = element.getAttribute(attributeName);
if (StringUtils.hasText(beanName)) {
beanDefinition.addPropertyReference(
Conventions.attributeNameToPropertyName(attributeName), beanName);
}
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, attributeName);
}
}

View File

@@ -1,31 +0,0 @@
/*
* Copyright 2002-2008 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.message;
/**
* A simple implementation of {@link MessageMapper} that returns the
* {@link Message Message's} payload.
*
* @author Mark Fisher
*/
public class DefaultMessageMapper<T> implements MessageMapper<T,T> {
public T mapMessage(Message<T> message) {
return (message != null) ? message.getPayload() : null;
}
}

View File

@@ -1,28 +0,0 @@
/*
* Copyright 2002-2008 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.message;
/**
* Strategy interface for creating a {@link Message} from an Object.
*
* @author Mark Fisher
*/
public interface MessageCreator<O, P> {
Message<P> createMessage(O object);
}

View File

@@ -83,8 +83,12 @@ public class MethodInvokingSource implements PollableSource<Object>, Initializin
if (result == null) {
return null;
}
if (result instanceof Message) {
return (Message) result;
}
return new GenericMessage<Object>(result);
} catch (InvocationTargetException e) {
}
catch (InvocationTargetException e) {
throw new MessagingException(
"Source method '" + this.methodName + "' threw an Exception.", e.getTargetException());
}

View File

@@ -24,6 +24,7 @@ import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.reset;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.easymock.IAnswer;
import org.junit.Before;
@@ -36,7 +37,6 @@ import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageDeliveryException;
import org.springframework.integration.message.MessageHeaders;
import org.springframework.integration.message.MessageMapper;
/**
* @author Iwein Fuld
@@ -52,19 +52,15 @@ public class SimpleMessagingGatewayTests {
private Message messageMock = createMock(Message.class);
private MessageMapper messageMapperMock = createMock(MessageMapper.class);
private MessageBus messageBusMock = createMock(MessageBus.class);
private Object[] allmocks = new Object[] {
requestChannel, replyChannel, messageMock, messageMapperMock };
private Object[] allmocks = new Object[] { requestChannel, replyChannel, messageMock };
@Before
public void initializeSample() {
this.simpleMessagingGateway = new SimpleMessagingGateway(requestChannel);
this.simpleMessagingGateway.setReplyChannel(replyChannel);
this.simpleMessagingGateway.setMessageMapper(messageMapperMock);
this.simpleMessagingGateway.setMessageBus(messageBusMock);
reset(allmocks);
}
@@ -114,11 +110,15 @@ public class SimpleMessagingGatewayTests {
verify(allmocks);
}
@Test
@Test(expected = IllegalArgumentException.class)
public void sendMessage_null() {
replay(allmocks);
this.simpleMessagingGateway.send(null);
verify(allmocks);
try {
this.simpleMessagingGateway.send(null);
}
finally {
verify(allmocks);
}
}
/* receive tests */
@@ -126,9 +126,9 @@ public class SimpleMessagingGatewayTests {
@Test
public void receiveMessage() {
expect(replyChannel.receive()).andReturn(messageMock);
expect(messageMapperMock.mapMessage(messageMock)).andReturn(messageMock);
expect(messageMock.getPayload()).andReturn("test").anyTimes();
replay(allmocks);
assertEquals(this.simpleMessagingGateway.receive(), messageMock);
assertEquals("test", this.simpleMessagingGateway.receive());
verify(allmocks);
}
@@ -136,7 +136,7 @@ public class SimpleMessagingGatewayTests {
public void receiveMessage_null() {
expect(replyChannel.receive()).andReturn(null);
replay(allmocks);
assertEquals(this.simpleMessagingGateway.receive(), null);
assertNull(this.simpleMessagingGateway.receive());
verify(allmocks);
}
@@ -172,12 +172,16 @@ public class SimpleMessagingGatewayTests {
verify(messageHeadersMock);
}
@Test
@Test(expected = IllegalArgumentException.class)
public void sendNullAndReceiveObject() {
expect(replyChannel.getName()).andReturn("replyChannel").anyTimes();
replay(allmocks);
this.simpleMessagingGateway.sendAndReceive(null);
verify(allmocks);
try {
this.simpleMessagingGateway.sendAndReceive(null);
}
finally {
verify(allmocks);
}
}
@Test
@@ -207,12 +211,16 @@ public class SimpleMessagingGatewayTests {
verify(allmocks);
}
@Test
@Test(expected = IllegalArgumentException.class)
public void sendNullAndReceiveMessage() {
expect(replyChannel.getName()).andReturn("replyChannel").anyTimes();
replay(allmocks);
this.simpleMessagingGateway.sendAndReceiveMessage(null);
verify(allmocks);
try {
this.simpleMessagingGateway.sendAndReceiveMessage(null);
}
finally {
verify(allmocks);
}
}
}

View File

@@ -75,18 +75,7 @@ public class GatewayParserTests {
this.startResponder(requestChannel, replyChannel);
TestService service = (TestService) context.getBean("requestReplyWithMessageMapper");
String result = service.requestReply("foo");
assertEquals("foo.mapped", result);
}
@Test
public void testRequestReplyWithMessageCreator() {
ApplicationContext context = new ClassPathXmlApplicationContext("gatewayParserTests.xml", this.getClass());
PollableChannel requestChannel = (PollableChannel) context.getBean("requestChannel");
MessageChannel replyChannel = (MessageChannel) context.getBean("replyChannel");
this.startResponder(requestChannel, replyChannel);
TestService service = (TestService) context.getBean("requestReplyWithMessageCreator");
String result = service.requestReply("foo");
assertEquals("created.foo", result);
assertEquals("pre.foo.post", result);
}

View File

@@ -1,32 +0,0 @@
/*
* Copyright 2002-2008 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.gateway.config;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageCreator;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class TestMessageCreator implements MessageCreator<String, String> {
public Message<String> createMessage(String s) {
return new StringMessage("created." + s);
}
}

View File

@@ -16,16 +16,21 @@
package org.springframework.integration.gateway.config;
import org.springframework.integration.gateway.MessageMapper;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageMapper;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class TestMessageMapper implements MessageMapper<String, String> {
public class TestMessageMapper implements MessageMapper<String> {
public String mapMessage(Message<String> message) {
return message.getPayload() + ".mapped";
public Message<?> toMessage(String object) {
return new StringMessage("pre." + object);
}
public String fromMessage(Message<?> message) {
return message.getPayload().toString() + ".post";
}
}

View File

@@ -37,14 +37,6 @@
reply-channel="replyChannel"
message-mapper="mapper"/>
<gateway id="requestReplyWithMessageCreator"
service-interface="org.springframework.integration.gateway.TestService"
request-channel="requestChannel"
reply-channel="replyChannel"
message-creator="creator"/>
<beans:bean id="mapper" class="org.springframework.integration.gateway.config.TestMessageMapper"/>
<beans:bean id="creator" class="org.springframework.integration.gateway.config.TestMessageCreator"/>
</beans:beans>

View File

@@ -1,59 +0,0 @@
/*
* Copyright 2002-2008 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.message;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
/**
* @author Mark Fisher
*/
public class DefaultMessageCreatorTests {
@Test
public void testStringPayload() {
DefaultMessageCreator creator = new DefaultMessageCreator();
Message<?> message = creator.createMessage("testing");
assertEquals("testing", message.getPayload());
}
@Test
public void testObjectPayload() {
DefaultMessageCreator creator = new DefaultMessageCreator();
Object test = new Object();
Message<?> message = creator.createMessage(test);
assertEquals(test, message.getPayload());
}
@Test
public void testMessage() {
DefaultMessageCreator creator = new DefaultMessageCreator();
Message<?> inputMessage = new GenericMessage<String>("testing");
Message<?> message = creator.createMessage(inputMessage);
assertEquals(inputMessage, message);
}
@Test
public void testNull() {
DefaultMessageCreator creator = new DefaultMessageCreator();
Message<?> message = creator.createMessage(null);
assertNull(message);
}
}

View File

@@ -1,50 +0,0 @@
/*
* Copyright 2002-2008 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.message;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
/**
* @author Mark Fisher
*/
public class DefaultMessageMapperTests {
@Test
public void testStringPayload() {
DefaultMessageMapper mapper = new DefaultMessageMapper();
String result = (String) mapper.mapMessage(new StringMessage("testing"));
assertEquals("testing", result);
}
@Test
public void testObjectPayload() {
DefaultMessageMapper mapper = new DefaultMessageMapper();
Object test = new Object();
Object result = mapper.mapMessage(new GenericMessage<Object>(test));
assertEquals(test, result);
}
@Test
public void testNullMessage() {
DefaultMessageMapper mapper = new DefaultMessageMapper();
Object result = mapper.mapMessage(null);
assertEquals(null, result);
}
}