From 9f95c5a1fab93375720483281b62f7a30fc0df45 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Wed, 14 May 2008 12:56:53 +0000 Subject: [PATCH] Added the abstract MessagingGatewaySupport base class providing access to a RequestReplyTemplate. Renamed MessagingGateway to SimpleMessagingGateway which now extends MessagingGatewaySupport (INT-126). --- .../gateway/GatewayProxyFactoryBean.java | 2 +- .../gateway/MessagingGatewaySupport.java | 64 +++++++++++++++++++ ...teway.java => SimpleMessagingGateway.java} | 49 ++++---------- 3 files changed, 79 insertions(+), 36 deletions(-) create mode 100644 spring-integration-core/src/main/java/org/springframework/integration/gateway/MessagingGatewaySupport.java rename spring-integration-core/src/main/java/org/springframework/integration/gateway/{MessagingGateway.java => SimpleMessagingGateway.java} (65%) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/gateway/GatewayProxyFactoryBean.java b/spring-integration-core/src/main/java/org/springframework/integration/gateway/GatewayProxyFactoryBean.java index 59dd434362..4a1bc1173f 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/gateway/GatewayProxyFactoryBean.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/gateway/GatewayProxyFactoryBean.java @@ -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; diff --git a/spring-integration-core/src/main/java/org/springframework/integration/gateway/MessagingGatewaySupport.java b/spring-integration-core/src/main/java/org/springframework/integration/gateway/MessagingGatewaySupport.java new file mode 100644 index 0000000000..ce25a86dcf --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/gateway/MessagingGatewaySupport.java @@ -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; + } + +} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/gateway/MessagingGateway.java b/spring-integration-core/src/main/java/org/springframework/integration/gateway/SimpleMessagingGateway.java similarity index 65% rename from spring-integration-core/src/main/java/org/springframework/integration/gateway/MessagingGateway.java rename to spring-integration-core/src/main/java/org/springframework/integration/gateway/SimpleMessagingGateway.java index 094bf57f42..4f1d8b68b3 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/gateway/MessagingGateway.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/gateway/SimpleMessagingGateway.java @@ -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; }