DefaultMessageHandlerAdapter now supports methods expecting a Message-typed parameter.
This commit is contained in:
@@ -16,25 +16,41 @@
|
||||
|
||||
package org.springframework.integration.handler;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageMapper;
|
||||
import org.springframework.integration.util.SimpleMethodInvoker;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* on the provided target object. If {@link #shouldUseMapperOnInvocation} is set
|
||||
* to <code>true</code> (the default), it will use the provided
|
||||
* {@link org.springframework.integration.message.MessageMapper} strategy to
|
||||
* convert the inbound {@link Message} to an object that will be passed as the
|
||||
* method parameter. 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 class DefaultMessageHandlerAdapter<T> extends AbstractMessageHandlerAdapter<T> implements Ordered {
|
||||
|
||||
private boolean shouldUseMapperOnInvocation = true;
|
||||
|
||||
|
||||
/**
|
||||
* Specify whether the handler method should use the
|
||||
* {@link org.springframework.integration.message.MessageMapper} when
|
||||
* invoking the target method. Default is <code>true</code>. To force
|
||||
* passing the {@link Message} directly, set this to <code>false</code>.
|
||||
*/
|
||||
public void setShouldUseMapperOnInvocation(boolean shouldUseMapperOnInvocation) {
|
||||
this.shouldUseMapperOnInvocation = shouldUseMapperOnInvocation;
|
||||
}
|
||||
|
||||
public Object doHandle(Message message, SimpleMethodInvoker invoker) {
|
||||
return invoker.invokeMethod(this.getMapper().fromMessage(message));
|
||||
if (this.shouldUseMapperOnInvocation) {
|
||||
return invoker.invokeMethod(this.getMapper().fromMessage(message));
|
||||
}
|
||||
return invoker.invokeMethod(message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,8 +19,10 @@ package org.springframework.integration.handler.config;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.integration.MessagingConfigurationException;
|
||||
import org.springframework.integration.handler.AbstractMessageHandlerAdapter;
|
||||
import org.springframework.integration.handler.MessageHandler;
|
||||
|
||||
@@ -43,6 +45,14 @@ public abstract class AbstractMessageHandlerCreator implements MessageHandlerCre
|
||||
adapter.setOrder(orderAnnotation.value());
|
||||
}
|
||||
}
|
||||
if (handler instanceof InitializingBean) {
|
||||
try {
|
||||
((InitializingBean) handler).afterPropertiesSet();
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new MessagingConfigurationException("failed to initialize handler", e);
|
||||
}
|
||||
}
|
||||
return handler;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,8 +20,10 @@ import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.integration.MessagingConfigurationException;
|
||||
import org.springframework.integration.handler.DefaultMessageHandlerAdapter;
|
||||
import org.springframework.integration.handler.MessageHandler;
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
/**
|
||||
* Default implementation of the handler creator strategy that creates a
|
||||
@@ -34,7 +36,14 @@ import org.springframework.integration.handler.MessageHandler;
|
||||
public class DefaultMessageHandlerCreator extends AbstractMessageHandlerCreator {
|
||||
|
||||
public MessageHandler doCreateHandler(Object object, Method method, Map<String, ?> attributes) {
|
||||
return new DefaultMessageHandlerAdapter<Object>();
|
||||
Class<?>[] types = method.getParameterTypes();
|
||||
if (types.length != 1) {
|
||||
throw new MessagingConfigurationException("exactly one method parameter is required");
|
||||
}
|
||||
DefaultMessageHandlerAdapter<Object> adapter = new DefaultMessageHandlerAdapter<Object>();
|
||||
boolean expectsMessage = (Message.class.isAssignableFrom(types[0]));
|
||||
adapter.setShouldUseMapperOnInvocation(!expectsMessage);
|
||||
return adapter;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user