Added the abstract MessagingGatewaySupport base class providing access to a RequestReplyTemplate. Renamed MessagingGateway to SimpleMessagingGateway which now extends MessagingGatewaySupport (INT-126).

This commit is contained in:
Mark Fisher
2008-05-14 12:56:53 +00:00
parent 1f57305cae
commit 9f95c5a1fa
3 changed files with 79 additions and 36 deletions

View File

@@ -39,7 +39,7 @@ import org.springframework.util.ClassUtils;
*
* @author Mark Fisher
*/
public class GatewayProxyFactoryBean extends MessagingGateway
public class GatewayProxyFactoryBean extends SimpleMessagingGateway
implements FactoryBean, MethodInterceptor, InitializingBean, BeanClassLoaderAware, BeanFactoryAware {
private Class<?> serviceInterface;

View File

@@ -0,0 +1,64 @@
/*
* 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.RequestReplyTemplate;
/**
* A convenient base class providing access to a {@link RequestReplyTemplate} 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 RequestReplyTemplate requestReplyTemplate = new RequestReplyTemplate();
public MessagingGatewaySupport(MessageChannel requestChannel) {
this.requestReplyTemplate.setRequestChannel(requestChannel);
}
public MessagingGatewaySupport() {
super();
}
public void setRequestChannel(MessageChannel requestChannel) {
this.requestReplyTemplate.setRequestChannel(requestChannel);
}
public void setReplyChannel(MessageChannel replyChannel) {
this.requestReplyTemplate.setReplyChannel(replyChannel);
}
public void setRequestTimeout(long requestTimeout) {
this.requestReplyTemplate.setRequestTimeout(requestTimeout);
}
public void setReplyTimeout(long replyTimeout) {
this.requestReplyTemplate.setReplyTimeout(replyTimeout);
}
protected final RequestReplyTemplate getRequestReplyTemplate() {
return this.requestReplyTemplate;
}
}

View File

@@ -17,7 +17,6 @@
package org.springframework.integration.gateway;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.RequestReplyTemplate;
import org.springframework.integration.message.DefaultMessageCreator;
import org.springframework.integration.message.DefaultMessageMapper;
import org.springframework.integration.message.Message;
@@ -27,46 +26,29 @@ 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. 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 GatewayProxyFactoryBean}.
* {@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.
*
* @author Mark Fisher
*/
public class MessagingGateway {
private final RequestReplyTemplate requestReplyTemplate = new RequestReplyTemplate();
public class SimpleMessagingGateway extends MessagingGatewaySupport {
private MessageCreator messageCreator = new DefaultMessageCreator();
private MessageMapper messageMapper = new DefaultMessageMapper();
public MessagingGateway(MessageChannel requestChannel) {
this.requestReplyTemplate.setRequestChannel(requestChannel);
public SimpleMessagingGateway(MessageChannel requestChannel) {
super(requestChannel);
}
public MessagingGateway() {
public SimpleMessagingGateway() {
super();
}
public void setRequestChannel(MessageChannel requestChannel) {
this.requestReplyTemplate.setRequestChannel(requestChannel);
}
public void setReplyChannel(MessageChannel replyChannel) {
this.requestReplyTemplate.setReplyChannel(replyChannel);
}
public void setRequestTimeout(long requestTimeout) {
this.requestReplyTemplate.setRequestTimeout(requestTimeout);
}
public void setReplyTimeout(long replyTimeout) {
this.requestReplyTemplate.setReplyTimeout(replyTimeout);
}
public void setMessageCreator(MessageCreator<?, ?> messageCreator) {
Assert.notNull(messageCreator, "messageCreator must not be null");
this.messageCreator = messageCreator;
@@ -77,20 +59,17 @@ public class MessagingGateway {
this.messageMapper = messageMapper;
}
protected RequestReplyTemplate getRequestReplyTemplate() {
return this.requestReplyTemplate;
}
public void send(Object object) {
public boolean send(Object object) {
Message<?> message = (object instanceof Message) ? (Message) object :
this.messageCreator.createMessage(object);
if (message != null) {
this.requestReplyTemplate.send(message);
if (message == null) {
return false;
}
return this.getRequestReplyTemplate().send(message);
}
public Object receive() {
Message<?> message = this.requestReplyTemplate.receive();
Message<?> message = this.getRequestReplyTemplate().receive();
return (message != null) ? this.messageMapper.mapMessage(message) : null;
}
@@ -108,7 +87,7 @@ public class MessagingGateway {
if (request == null) {
return null;
}
Message<?> reply = this.requestReplyTemplate.request(request);
Message<?> reply = this.getRequestReplyTemplate().request(request);
if (!shouldMapMessage) {
return reply;
}