MessageMapper now defines toMessage() and fromMessage() methods, and the MessageCreator has been removed.
This commit is contained in:
@@ -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>
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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() {
|
||||
|
||||
@@ -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 <gateway/> 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user