Reorganized handler packaging and added AbstractMessageHandlerAdapter

This commit is contained in:
Mark Fisher
2007-12-15 19:25:45 +00:00
parent 604597f0ee
commit 04f48fc665
15 changed files with 106 additions and 24 deletions

View File

@@ -24,7 +24,6 @@ import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.endpoint.annotation.MessageEndpointAnnotationPostProcessor;
/**
* Parser for the <em>annotation-driven</em> element of the integration

View File

@@ -23,8 +23,8 @@ import org.springframework.beans.factory.support.DefaultBeanNameGenerator;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.integration.endpoint.GenericMessageEndpoint;
import org.springframework.integration.endpoint.InboundMethodInvokingChannelAdapter;
import org.springframework.integration.endpoint.MessageHandlerAdapter;
import org.springframework.integration.endpoint.OutboundMethodInvokingChannelAdapter;
import org.springframework.integration.handler.DefaultMessageHandlerAdapter;
import org.springframework.util.Assert;
/**
@@ -47,7 +47,7 @@ public class ComponentConfigurer {
public String serviceActivator(String inputChannel, String outputChannel, String objectRef, String method) {
RootBeanDefinition endpointDef = new RootBeanDefinition(GenericMessageEndpoint.class);
RootBeanDefinition adapterDef = new RootBeanDefinition(MessageHandlerAdapter.class);
RootBeanDefinition adapterDef = new RootBeanDefinition(DefaultMessageHandlerAdapter.class);
adapterDef.getPropertyValues().addPropertyValue("object", new RuntimeBeanReference(objectRef));
adapterDef.getPropertyValues().addPropertyValue("method", method);
String adapterName = beanNameGenerator.generateBeanName(adapterDef, this.registry);

View File

@@ -28,7 +28,7 @@ import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.bus.ConsumerPolicy;
import org.springframework.integration.endpoint.GenericMessageEndpoint;
import org.springframework.integration.endpoint.MessageHandlerAdapter;
import org.springframework.integration.handler.DefaultMessageHandlerAdapter;
import org.springframework.util.StringUtils;
/**
@@ -82,7 +82,7 @@ public class EndpointParser implements BeanDefinitionParser {
if (StringUtils.hasText(handlerRef)) {
String handlerMethod = element.getAttribute(HANDLER_METHOD_ATTRIBUTE);
if (StringUtils.hasText(handlerMethod)) {
BeanDefinition handlerAdapterDef = new RootBeanDefinition(MessageHandlerAdapter.class);
BeanDefinition handlerAdapterDef = new RootBeanDefinition(DefaultMessageHandlerAdapter.class);
handlerAdapterDef.getPropertyValues().addPropertyValue(OBJECT_PROPERTY, new RuntimeBeanReference(handlerRef));
handlerAdapterDef.getPropertyValues().addPropertyValue(METHOD_PROPERTY, handlerMethod);
String adapterBeanName = parserContext.getReaderContext().generateBeanName(handlerAdapterDef);

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.endpoint.annotation;
package org.springframework.integration.config;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
@@ -34,8 +34,14 @@ import org.springframework.integration.bus.MessageBus;
import org.springframework.integration.endpoint.GenericMessageEndpoint;
import org.springframework.integration.endpoint.InboundMethodInvokingChannelAdapter;
import org.springframework.integration.endpoint.OutboundMethodInvokingChannelAdapter;
import org.springframework.integration.endpoint.annotation.DefaultOutput;
import org.springframework.integration.endpoint.annotation.MessageEndpoint;
import org.springframework.integration.endpoint.annotation.Polled;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.handler.MessageHandlerChain;
import org.springframework.integration.handler.annotation.AnnotationHandlerCreator;
import org.springframework.integration.handler.annotation.DefaultAnnotationHandlerCreator;
import org.springframework.integration.handler.annotation.Handler;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;

View File

@@ -28,8 +28,8 @@ import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.integration.bus.MessageBus;
import org.springframework.integration.endpoint.GenericMessageEndpoint;
import org.springframework.integration.endpoint.MessageHandlerAdapter;
import org.springframework.integration.endpoint.annotation.Subscriber;
import org.springframework.integration.handler.DefaultMessageHandlerAdapter;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
@@ -81,7 +81,7 @@ public class SubscriberAnnotationPostProcessor implements BeanPostProcessor {
Annotation annotation = method.getAnnotation(subscriberAnnotationType);
if (annotation != null) {
String channelName = (String) AnnotationUtils.getValue(annotation, channelNameAttribute);
MessageHandlerAdapter adapter = new MessageHandlerAdapter();
DefaultMessageHandlerAdapter adapter = new DefaultMessageHandlerAdapter();
adapter.setMethod(method.getName());
adapter.setObject(bean);
adapter.afterPropertiesSet();

View File

@@ -14,25 +14,27 @@
* limitations under the License.
*/
package org.springframework.integration.endpoint;
package org.springframework.integration.handler;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.Ordered;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.endpoint.SimpleMethodInvoker;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageMapper;
import org.springframework.integration.message.SimplePayloadMessageMapper;
import org.springframework.util.Assert;
/**
* An implementation of {@link MessageHandler} that invokes the specified method
* on the provided target object. It then uses a {@link MessageMapper} strategy
* for converting the object to a {@link Message}. If the method has a non-null
* return value, a reply message will be generated by the mapper.
* Base implementation of the {@link MessageHandler} interface that creates an
* invoker for the specified method and target object. It also accepts an
* implementation of the {@link MessageMapper} strategy which it exposes to
* subclasses for converting the {@link Message} to an object. Likewise, if the
* method has a non-null return value, a reply message will be generated by the
* mapper.
*
* @author Mark Fisher
*/
public class MessageHandlerAdapter<T> implements MessageHandler, Ordered, InitializingBean {
public abstract class AbstractMessageHandlerAdapter<T> implements MessageHandler, Ordered, InitializingBean {
private T object;
@@ -50,11 +52,28 @@ public class MessageHandlerAdapter<T> implements MessageHandler, Ordered, Initia
this.object = object;
}
protected Object getObject() {
return this.object;
}
public void setMethod(String method) {
Assert.notNull(method, "'method' must not be null");
this.method = method;
}
public String getMethod() {
return this.method;
}
public void setMapper(MessageMapper mapper) {
Assert.notNull(mapper, "'mapper' must not be null");
this.mapper = mapper;
}
protected MessageMapper getMapper() {
return this.mapper;
}
public void setOrder(int order) {
this.order = order;
}
@@ -68,11 +87,21 @@ public class MessageHandlerAdapter<T> implements MessageHandler, Ordered, Initia
}
public Message handle(Message message) {
Object result = this.invoker.invokeMethod(this.mapper.fromMessage(message));
Object result = this.doHandle(message, invoker);
if (result != null) {
if (result instanceof Message) {
return (Message) result;
}
return this.mapper.toMessage(result);
}
return null;
}
/**
* Subclasses must implement this method. The invoker has been created for
* the provided target object and method. May return an object of type
* {@link Message}, else rely on the message mapper to convert.
*/
protected abstract Object doHandle(Message message, SimpleMethodInvoker invoker);
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2002-2007 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.handler;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.Ordered;
import org.springframework.integration.endpoint.SimpleMethodInvoker;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageMapper;
/**
* An implementation of {@link MessageHandler} that invokes the specified method
* on the provided target object. It then uses a {@link MessageMapper} strategy
* for converting the object to a {@link Message}. If the method has a non-null
* return value, a reply message will be generated by the mapper.
*
* @author Mark Fisher
*/
public class DefaultMessageHandlerAdapter<T> extends AbstractMessageHandlerAdapter<T>
implements Ordered, InitializingBean {
public Object doHandle(Message message, SimpleMethodInvoker invoker) {
return invoker.invokeMethod(this.getMapper().fromMessage(message));
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.endpoint.annotation;
package org.springframework.integration.handler.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

View File

@@ -14,19 +14,19 @@
* limitations under the License.
*/
package org.springframework.integration.endpoint.annotation;
package org.springframework.integration.handler.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.integration.endpoint.MessageHandlerAdapter;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.handler.DefaultMessageHandlerAdapter;
/**
* Default implementation of the handler creator strategy that creates a
* {@link MessageHandlerAdapter} for the provided object and method. This
* {@link DefaultMessageHandlerAdapter} for the provided object and method. This
* version does not even consider the annotation itself. It does however
* respect an {@link Order} annotation if present.
*
@@ -35,7 +35,7 @@ import org.springframework.integration.handler.MessageHandler;
public class DefaultAnnotationHandlerCreator implements AnnotationHandlerCreator {
public MessageHandler createHandler(Object object, Method method, Annotation annotation) {
MessageHandlerAdapter<Object> adapter = new MessageHandlerAdapter<Object>();
DefaultMessageHandlerAdapter<Object> adapter = new DefaultMessageHandlerAdapter<Object>();
adapter.setObject(object);
adapter.setMethod(method.getName());
Order orderAnnotation = (Order) AnnotationUtils.getAnnotation(method, Order.class);

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.endpoint.annotation;
package org.springframework.integration.handler.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
@@ -23,6 +23,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.integration.endpoint.annotation.MessageEndpoint;
/**
* Indicates that a method is capable of handling a message or message payload.
* The method may only accept a single parameter, and the enclosing class should