Added AbstractInOutEndpoint and ServiceActivatorEndpoint. SplitterEndpoint now extends AbstractInOutEndpoint as well.

This commit is contained in:
Mark Fisher
2008-08-30 17:07:58 +00:00
parent b009bb137c
commit 41b3a764ab
18 changed files with 497 additions and 222 deletions

View File

@@ -22,6 +22,7 @@ import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.ConfigurationException;
@@ -46,8 +47,12 @@ public abstract class AbstractEndpointParser extends AbstractSingleBeanDefinitio
private static final String POLLER_ELEMENT = "poller";
private static final String SELECTOR_ATTRIBUTE = "selector";
private static final String ERROR_HANDLER_ATTRIBUTE = "error-handler";
private static final String INTERCEPTORS_ELEMENT = "interceptors";
@Override
protected Class<?> getBeanClass(Element element) {
@@ -92,7 +97,14 @@ public abstract class AbstractEndpointParser extends AbstractSingleBeanDefinitio
}
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(
builder, element, OUTPUT_CHANNEL_ATTRIBUTE, "target");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, SELECTOR_ATTRIBUTE);
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, ERROR_HANDLER_ATTRIBUTE);
Element interceptorsElement = DomUtils.getChildElementByTagName(element, INTERCEPTORS_ELEMENT);
if (interceptorsElement != null) {
EndpointInterceptorParser parser = new EndpointInterceptorParser();
ManagedList interceptors = parser.parseInterceptors(interceptorsElement, parserContext);
builder.addPropertyValue("interceptors", interceptors);
}
}
private String parseAdapter(String ref, String method, Element element, ParserContext parserContext) {

View File

@@ -16,19 +16,25 @@
package org.springframework.integration.config;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.handler.DefaultMessageHandler;
import org.springframework.integration.endpoint.MessageEndpoint;
import org.springframework.integration.endpoint.ServiceActivatorEndpoint;
import org.springframework.integration.message.MessageMappingMethodInvoker;
/**
* Parser for the &lt;service-activator&gt; element.
*
* @author Mark Fisher
*/
public class ServiceActivatorParser extends AbstractMessageEndpointParser {
public class ServiceActivatorParser extends AbstractEndpointParser {
@Override
protected Class<? extends MessageHandler> getHandlerAdapterClass() {
return DefaultMessageHandler.class;
protected Class<? extends MessageEndpoint> getEndpointClass() {
return ServiceActivatorEndpoint.class;
}
@Override
protected Class<?> getMethodInvokingAdapterClass() {
return MessageMappingMethodInvoker.class;
}
}

View File

@@ -75,4 +75,8 @@ public abstract class AbstractDispatcher implements MessageDispatcher {
return this.messageExchangeTemplate.send(message, target);
}
public String toString() {
return this.getClass().getSimpleName() + " with targets: " + this.targets;
}
}

View File

@@ -0,0 +1,181 @@
/*
* 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.endpoint;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.CompositeMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.integration.message.MessageHeaders;
import org.springframework.integration.message.MessageRejectedException;
import org.springframework.integration.message.MessageTarget;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.message.selector.MessageSelector;
/**
* @author Mark Fisher
*/
public abstract class AbstractInOutEndpoint extends AbstractEndpoint {
private volatile MessageSelector selector;
private final List<EndpointInterceptor> interceptors = new CopyOnWriteArrayList<EndpointInterceptor>();
public void setSelector(MessageSelector selector) {
this.selector = selector;
}
public void addInterceptor(EndpointInterceptor interceptor) {
this.interceptors.add(interceptor);
}
public void setInterceptors(List<EndpointInterceptor> interceptors) {
this.interceptors.clear();
for (EndpointInterceptor interceptor : interceptors) {
this.addInterceptor(interceptor);
}
}
@Override
protected boolean sendInternal(Message<?> message) {
for (EndpointInterceptor interceptor : this.interceptors) {
message = interceptor.preHandle(message);
if (message == null) {
return false;
}
}
if (!this.supports(message)) {
throw new MessageRejectedException(message, "unsupported message");
}
Object result = this.handle(message);
if (result == null) {
return false;
}
Message<?> reply = buildReplyMessage(result, message.getHeaders());
MessageTarget replyTarget = this.resolveReplyTarget(message);
if (reply instanceof CompositeMessage && this.shouldSplitComposite()) {
for (Message<?> nextReply : (CompositeMessage) reply) {
this.sendReplyMessage(nextReply, replyTarget);
}
return true;
}
else {
return this.sendReplyMessage(reply, replyTarget);
}
}
protected abstract Object handle(Message<?> message);
protected boolean supports(Message<?> message) {
if (this.selector != null && !this.selector.accept(message)) {
if (logger.isDebugEnabled()) {
logger.debug("selector for endpoint '" + this + "' rejected message: " + message);
}
return false;
}
return true;
}
protected boolean shouldSplitComposite() {
return false;
}
private boolean sendReplyMessage(Message<?> replyMessage, MessageTarget replyTarget) {
for (int i = this.interceptors.size() - 1; i >= 0; i--) {
EndpointInterceptor interceptor = this.interceptors.get(i);
if (interceptor != null) {
replyMessage = interceptor.postHandle(replyMessage);
if (replyMessage == null) {
return false;
}
}
}
return this.getMessageExchangeTemplate().send(replyMessage, replyTarget);
}
private Message<?> buildReplyMessage(Object result, MessageHeaders requestHeaders) {
MessageBuilder<?> builder = null;
if (result instanceof MessageBuilder) {
builder = (MessageBuilder<?>) result;
}
else if (result instanceof CompositeMessage) {
List<Message<?>> messages = ((CompositeMessage) result).getPayload();
List<Message<?>> replies = new ArrayList<Message<?>>();
for (Message<?> message : messages) {
replies.add(this.buildReplyMessage(message, requestHeaders));
}
return new CompositeMessage(replies);
}
else if (result instanceof Message<?>) {
builder = MessageBuilder.fromMessage((Message<?>) result);
}
else {
builder = MessageBuilder.fromPayload(result);
}
return builder.copyHeadersIfAbsent(requestHeaders)
.setHeaderIfAbsent(MessageHeaders.CORRELATION_ID, requestHeaders.getId())
.build();
}
private MessageTarget resolveReplyTarget(Message<?> requestMessage) {
MessageTarget replyTarget = this.getTarget();
if (replyTarget == null) {
Object returnAddress = requestMessage.getHeaders().getReturnAddress();
if (returnAddress != null) {
if (returnAddress instanceof MessageTarget) {
replyTarget = (MessageTarget) returnAddress;
}
else if (returnAddress instanceof String) {
ChannelRegistry channelRegistry = this.getChannelRegistry();
if (channelRegistry != null) {
replyTarget = channelRegistry.lookupChannel((String) returnAddress);
}
}
}
}
if (replyTarget == null) {
throw new MessagingException("unable to resolve reply target");
}
return replyTarget;
}
// TODO: remove these methods after refactoring
private volatile String inputChannelName;
public String getInputChannelName() {
return this.inputChannelName;
}
public void setInputChannelName(String inputChannelName) {
this.inputChannelName = inputChannelName;
}
public String getOutputChannelName() {
if (this.getTarget() instanceof MessageChannel) {
return ((MessageChannel) this.getTarget()).getName();
}
return null;
}
}

View File

@@ -0,0 +1,62 @@
/*
* 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.endpoint;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageMappingMethodInvoker;
import org.springframework.util.Assert;
/**
* @author Mark Fisher
*/
public class ServiceActivatorEndpoint extends AbstractInOutEndpoint implements InitializingBean {
private final MessageMappingMethodInvoker invoker;
private final MessageHandler handler;
public ServiceActivatorEndpoint(MessageMappingMethodInvoker invoker) {
Assert.notNull(invoker, "invoker must not be null");
this.invoker = invoker;
this.handler = null;
}
public ServiceActivatorEndpoint(MessageHandler handler) {
Assert.notNull(handler, "handler must not be null");
this.handler = handler;
this.invoker = null;
}
public void afterPropertiesSet() throws Exception {
if (this.invoker != null) {
this.invoker.afterPropertiesSet();
}
}
@Override
protected Object handle(Message<?> message) {
if (this.invoker != null) {
return this.invoker.invokeMethod(message);
}
return this.handler.handle(message);
}
}

View File

@@ -18,18 +18,15 @@ package org.springframework.integration.splitter;
import java.util.List;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.endpoint.AbstractInOutEndpoint;
import org.springframework.integration.message.CompositeMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageTarget;
import org.springframework.integration.message.MessagingException;
import org.springframework.util.Assert;
/**
* @author Mark Fisher
*/
public class SplitterEndpoint extends AbstractEndpoint {
public class SplitterEndpoint extends AbstractInOutEndpoint {
private final Splitter splitter;
@@ -40,57 +37,18 @@ public class SplitterEndpoint extends AbstractEndpoint {
}
// TODO: move to superclass
private MessageTarget resolveReplyTarget(Object returnAddress) {
MessageTarget replyTarget = this.getTarget();
if (replyTarget == null && returnAddress != null) {
if (returnAddress instanceof MessageTarget) {
replyTarget = (MessageTarget) returnAddress;
}
else if (returnAddress instanceof String) {
ChannelRegistry channelRegistry = this.getChannelRegistry();
if (channelRegistry != null) {
replyTarget = channelRegistry.lookupChannel((String) returnAddress);
}
}
}
if (replyTarget == null) {
throw new MessagingException("unable to resolve reply target");
}
return replyTarget;
@Override
protected boolean shouldSplitComposite() {
return true;
}
@Override
protected boolean sendInternal(Message<?> message) {
protected Message<?> handle(Message<?> message) {
List<Message<?>> results = this.splitter.split(message);
if (results != null) {
for (Message<?> splitMessage : results) {
MessageTarget replyTarget = this.resolveReplyTarget(message.getHeaders().getReturnAddress());
this.getMessageExchangeTemplate().send(splitMessage, replyTarget);
}
return true;
if (results == null || results.isEmpty()) {
return null;
}
return false;
}
// TODO: remove these methods after refactoring
private volatile String inputChannelName;
public String getInputChannelName() {
return this.inputChannelName;
}
public void setInputChannelName(String inputChannelName) {
this.inputChannelName = inputChannelName;
}
public String getOutputChannelName() {
if (this.getTarget() instanceof MessageChannel) {
return ((MessageChannel) this.getTarget()).getName();
}
return null;
return new CompositeMessage(results);
}
}