ServiceActivatorEndpoint no longer provides a MessageHandler-accepting constructor. It does now provide a version that takes an Object and 'methodName' as well as a version that takes an Object and expects a default handler method name.

This commit is contained in:
Mark Fisher
2008-09-05 02:22:52 +00:00
parent e22bf81927
commit eb6f0848a4
4 changed files with 66 additions and 51 deletions

View File

@@ -17,9 +17,9 @@
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.MessageHandlingException;
import org.springframework.integration.message.MessageMappingMethodInvoker;
import org.springframework.integration.util.MethodInvoker;
import org.springframework.util.Assert;
@@ -28,40 +28,43 @@ import org.springframework.util.Assert;
*/
public class ServiceActivatorEndpoint extends AbstractInOutEndpoint implements InitializingBean {
private final MethodInvoker invoker;
public static final String DEFAULT_LISTENER_METHOD = "handle";
private final MessageHandler handler;
private final MethodInvoker invoker;
public ServiceActivatorEndpoint(MethodInvoker 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 ServiceActivatorEndpoint(Object object) {
this(object, DEFAULT_LISTENER_METHOD);
}
public ServiceActivatorEndpoint(Object object, String methodName) {
this(new MessageMappingMethodInvoker(object, methodName));
}
public void afterPropertiesSet() throws Exception {
if (this.invoker != null && (this.invoker instanceof InitializingBean)) {
if (this.invoker instanceof InitializingBean) {
((InitializingBean) this.invoker).afterPropertiesSet();
}
}
@Override
protected Object handle(Message<?> message) {
if (this.invoker != null) {
try {
return this.invoker.invokeMethod(message);
} catch (Exception e) {
throw new MessageHandlingException(message, "failure occurred in endpoint '" + this.getName() + "'", e);
}
try {
return this.invoker.invokeMethod(message);
}
catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new MessageHandlingException(message, "failure occurred in endpoint '" + this.getName() + "'", e);
}
return this.handler.handle(message);
}
}

View File

@@ -162,10 +162,16 @@ public class MessageMappingMethodInvoker implements MethodInvoker, InitializingB
return result;
}
catch (InvocationTargetException e) {
if (e.getCause() != null && e.getCause() instanceof RuntimeException) {
throw (RuntimeException) e.getCause();
}
throw new MessageHandlingException(message, "Handler method '"
+ this.methodName + "' threw an Exception.", e.getTargetException());
+ this.methodName + "' threw an Exception.", e.getCause());
}
catch (Throwable e) {
catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new MessageHandlingException(message, "Failed to invoke handler method '"
+ this.methodName + "' with arguments: " + ObjectUtils.nullSafeToString(args), e);
}

View File

@@ -32,7 +32,6 @@ import org.springframework.integration.bus.DefaultMessageBus;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.DefaultChannelRegistry;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.handler.TestHandlers;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageBuilder;
@@ -52,8 +51,7 @@ public class ServiceActivatorEndpointTests {
@Test
public void outputChannel() {
QueueChannel channel = new QueueChannel(1);
MessageHandler handler = new TestHandler();
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(handler);
ServiceActivatorEndpoint endpoint = this.createEndpoint();
endpoint.setTarget(channel);
Message<?> message = MessageBuilder.fromPayload("foo").build();
endpoint.send(message);
@@ -66,7 +64,7 @@ public class ServiceActivatorEndpointTests {
public void outputChannelTakesPrecedence() {
QueueChannel channel1 = new QueueChannel(1);
QueueChannel channel2 = new QueueChannel(1);
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(new TestHandler());
ServiceActivatorEndpoint endpoint = this.createEndpoint();
endpoint.setTarget(channel1);
Message<?> message = MessageBuilder.fromPayload("foo").setReturnAddress(channel2).build();
endpoint.send(message);
@@ -80,8 +78,7 @@ public class ServiceActivatorEndpointTests {
@Test
public void returnAddressHeader() {
QueueChannel channel = new QueueChannel(1);
MessageHandler handler = new TestHandler();
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(handler);
ServiceActivatorEndpoint endpoint = this.createEndpoint();
Message<?> message = MessageBuilder.fromPayload("foo").setReturnAddress(channel).build();
endpoint.send(message);
Message<?> reply = channel.receive(0);
@@ -95,8 +92,7 @@ public class ServiceActivatorEndpointTests {
channel.setBeanName("testChannel");
ChannelRegistry channelRegistry = new DefaultMessageBus();
channelRegistry.registerChannel(channel);
MessageHandler handler = new TestHandler();
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(handler);
ServiceActivatorEndpoint endpoint = this.createEndpoint();
endpoint.setChannelRegistry(channelRegistry);
Message<?> message = MessageBuilder.fromPayload("foo").setReturnAddress("testChannel").build();
endpoint.send(message);
@@ -112,12 +108,13 @@ public class ServiceActivatorEndpointTests {
replyChannel2.setBeanName("replyChannel2");
ChannelRegistry channelRegistry = new DefaultChannelRegistry();
channelRegistry.registerChannel(replyChannel2);
MessageHandler handler = new MessageHandler() {
Object handler = new Object() {
@SuppressWarnings("unused")
public Message<?> handle(Message<?> message) {
return new StringMessage("foo" + message.getPayload());
}
};
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(handler);
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(handler, "handle");
endpoint.setChannelRegistry(channelRegistry);
Message<String> testMessage1 = MessageBuilder.fromPayload("bar")
.setReturnAddress(replyChannel1).build();
@@ -140,8 +137,7 @@ public class ServiceActivatorEndpointTests {
@Test
public void noOutputChannelFallsBackToReturnAddress() {
QueueChannel channel = new QueueChannel(1);
MessageHandler handler = new TestHandler();
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(handler);
ServiceActivatorEndpoint endpoint = this.createEndpoint();
Message<?> message = MessageBuilder.fromPayload("foo").setReturnAddress(channel).build();
endpoint.send(message);
Message<?> reply = channel.receive(0);
@@ -151,8 +147,7 @@ public class ServiceActivatorEndpointTests {
@Test(expected = MessagingException.class)
public void noReplyTarget() {
MessageHandler handler = new TestHandler();
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(handler);
ServiceActivatorEndpoint endpoint = this.createEndpoint();
Message<?> message = MessageBuilder.fromPayload("foo").build();
endpoint.send(message);
}
@@ -160,8 +155,8 @@ public class ServiceActivatorEndpointTests {
@Test
public void noReplyMessage() {
QueueChannel channel = new QueueChannel(1);
MessageHandler handler = new TestNullReplyHandler();
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(handler);
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(
new TestNullReplyBean(), "handle");
endpoint.setTarget(channel);
Message<?> message = MessageBuilder.fromPayload("foo").build();
endpoint.send(message);
@@ -171,8 +166,8 @@ public class ServiceActivatorEndpointTests {
@Test(expected = MessageHandlingException.class)
public void noReplyMessageWithRequiresReply() {
QueueChannel channel = new QueueChannel(1);
MessageHandler handler = new TestNullReplyHandler();
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(handler);
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(
new TestNullReplyBean(), "handle");
endpoint.setRequiresReply(true);
endpoint.setTarget(channel);
Message<?> message = MessageBuilder.fromPayload("foo").build();
@@ -181,7 +176,8 @@ public class ServiceActivatorEndpointTests {
@Test(expected=MessageRejectedException.class)
public void endpointWithSelectorRejecting() {
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(TestHandlers.nullHandler());
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(
TestHandlers.nullHandler(), "handle");
endpoint.setSelector(new MessageSelector() {
public boolean accept(Message<?> message) {
return false;
@@ -193,7 +189,8 @@ public class ServiceActivatorEndpointTests {
@Test
public void endpointWithSelectorAccepting() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(TestHandlers.countDownHandler(latch));
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(
TestHandlers.countDownHandler(latch), "handle");
endpoint.setSelector(new MessageSelector() {
public boolean accept(Message<?> message) {
return true;
@@ -207,7 +204,8 @@ public class ServiceActivatorEndpointTests {
@Test
public void endpointWithMultipleSelectorsAndFirstRejects() {
final AtomicInteger counter = new AtomicInteger();
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(TestHandlers.countingHandler(counter));
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(
TestHandlers.countingHandler(counter), "handle");
MessageSelectorChain selectorChain = new MessageSelectorChain();
selectorChain.add(new MessageSelector() {
public boolean accept(Message<?> message) {
@@ -237,7 +235,8 @@ public class ServiceActivatorEndpointTests {
public void endpointWithMultipleSelectorsAndFirstAccepts() {
final AtomicInteger selectorCounter = new AtomicInteger();
AtomicInteger handlerCounter = new AtomicInteger();
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(TestHandlers.countingHandler(handlerCounter));
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(
TestHandlers.countingHandler(handlerCounter), "handle");
MessageSelectorChain selectorChain = new MessageSelectorChain();
selectorChain.add(new MessageSelector() {
public boolean accept(Message<?> message) {
@@ -267,7 +266,8 @@ public class ServiceActivatorEndpointTests {
@Test
public void endpointWithMultipleSelectorsAndBothAccept() {
final AtomicInteger counter = new AtomicInteger();
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(TestHandlers.countingHandler(counter));
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(
TestHandlers.countingHandler(counter), "handle");
MessageSelectorChain selectorChain = new MessageSelectorChain();
selectorChain.add(new MessageSelector() {
public boolean accept(Message<?> message) {
@@ -289,11 +289,12 @@ public class ServiceActivatorEndpointTests {
@Test
public void correlationId() {
QueueChannel replyChannel = new QueueChannel(1);
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(new MessageHandler() {
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(new Object() {
@SuppressWarnings("unused")
public Message<?> handle(Message<?> message) {
return message;
}
});
}, "handle");
Message<String> message = MessageBuilder.fromPayload("test")
.setReturnAddress(replyChannel).build();
endpoint.send(message);
@@ -304,12 +305,13 @@ public class ServiceActivatorEndpointTests {
@Test
public void correlationIdSetByHandlerTakesPrecedence() {
QueueChannel replyChannel = new QueueChannel(1);
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(new MessageHandler() {
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(new Object() {
@SuppressWarnings("unused")
public Message<?> handle(Message<?> message) {
return MessageBuilder.fromMessage(message)
.setCorrelationId("ABC-123").build();
}
});
}, "handle");
Message<String> message = MessageBuilder.fromPayload("test")
.setReturnAddress(replyChannel).build();
endpoint.send(message);
@@ -320,15 +322,20 @@ public class ServiceActivatorEndpointTests {
}
private static class TestHandler implements MessageHandler {
private ServiceActivatorEndpoint createEndpoint() {
return new ServiceActivatorEndpoint(new TestBean(), "handle");
}
private static class TestBean {
public Message<?> handle(Message<?> message) {
return new StringMessage(message.getPayload().toString().toUpperCase());
}
}
private static class TestNullReplyHandler implements MessageHandler {
private static class TestNullReplyBean {
public Message<?> handle(Message<?> message) {
return null;

View File

@@ -31,7 +31,6 @@ import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.integration.message.MessageHandlingException;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.message.StringMessage;
/**
@@ -77,7 +76,7 @@ public class MethodInvokingTransformerTests {
assertEquals("123!", result.getPayload());
}
@Test(expected = MessagingException.class)
@Test(expected = IllegalArgumentException.class)
public void typeConversionFailureConfiguredWithMethodReference() throws Exception {
TestBean testBean = new TestBean();
Method testMethod = testBean.getClass().getMethod("exclaim", String.class);
@@ -86,7 +85,7 @@ public class MethodInvokingTransformerTests {
transformer.transform(message);
}
@Test(expected = MessagingException.class)
@Test(expected = IllegalArgumentException.class)
public void typeConversionFailureConfiguredWithMethodName() throws Exception {
TestBean testBean = new TestBean();
MethodInvokingTransformer transformer = new MethodInvokingTransformer(testBean, "exclaim");