INT-904, INT-907 added exception mapper support to AbstractMessagingGateway, Changed ChannelPublishingMessageListener to extend form AbstractMesagingGateway, added namespace support, tests, etc...
This commit is contained in:
@@ -39,7 +39,7 @@ import org.w3c.dom.Element;
|
||||
public class GatewayParser extends AbstractSimpleBeanDefinitionParser {
|
||||
|
||||
private static String[] referenceAttributes = new String[] {
|
||||
"default-request-channel", "default-reply-channel", "message-mapper"
|
||||
"default-request-channel", "default-reply-channel", "message-mapper", "exception-mapper"
|
||||
};
|
||||
|
||||
private static String[] innerAttributes = new String[] {
|
||||
|
||||
@@ -27,6 +27,8 @@ import org.springframework.integration.endpoint.PollingConsumer;
|
||||
import org.springframework.integration.endpoint.EventDrivenConsumer;
|
||||
import org.springframework.integration.handler.BridgeHandler;
|
||||
import org.springframework.integration.message.ErrorMessage;
|
||||
import org.springframework.integration.message.InboundMessageMapper;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.message.MessageHandler;
|
||||
import org.springframework.integration.message.MessageDeliveryException;
|
||||
import org.springframework.scheduling.support.PeriodicTrigger;
|
||||
@@ -41,6 +43,8 @@ import org.springframework.util.Assert;
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public abstract class AbstractMessagingGateway extends AbstractEndpoint {
|
||||
|
||||
private volatile InboundMessageMapper<Throwable> exceptionMapper;
|
||||
|
||||
private volatile MessageChannel requestChannel;
|
||||
|
||||
@@ -120,7 +124,7 @@ public abstract class AbstractMessagingGateway extends AbstractEndpoint {
|
||||
}
|
||||
}
|
||||
|
||||
public void send(Object object) {
|
||||
protected void send(Object object) {
|
||||
this.initializeIfNecessary();
|
||||
Assert.state(this.requestChannel != null,
|
||||
"send is not supported, because no request channel has been configured");
|
||||
@@ -131,7 +135,7 @@ public abstract class AbstractMessagingGateway extends AbstractEndpoint {
|
||||
}
|
||||
}
|
||||
|
||||
public Object receive() {
|
||||
protected Object receive() {
|
||||
this.initializeIfNecessary();
|
||||
Assert.state(this.replyChannel != null && (this.replyChannel instanceof PollableChannel),
|
||||
"receive is not supported, because no pollable reply channel has been configured");
|
||||
@@ -147,15 +151,15 @@ public abstract class AbstractMessagingGateway extends AbstractEndpoint {
|
||||
}
|
||||
}
|
||||
|
||||
public Object sendAndReceive(Object object) {
|
||||
protected Object sendAndReceive(Object object) {
|
||||
return this.sendAndReceive(object, true);
|
||||
}
|
||||
|
||||
public Message<?> sendAndReceiveMessage(Object object) {
|
||||
protected Message<?> sendAndReceiveMessage(Object object) {
|
||||
return (Message<?>) this.sendAndReceive(object, false);
|
||||
}
|
||||
|
||||
private Object sendAndReceive(Object object, boolean shouldMapMessage) {
|
||||
Object sendAndReceive(Object object, boolean shouldMapMessage) {
|
||||
Message<?> request = this.toMessage(object);
|
||||
Message<?> reply = this.sendAndReceiveMessage(request);
|
||||
if (!shouldMapMessage) {
|
||||
@@ -174,7 +178,19 @@ public abstract class AbstractMessagingGateway extends AbstractEndpoint {
|
||||
if (this.replyChannel != null && this.replyMessageCorrelator == null) {
|
||||
this.registerReplyMessageCorrelator();
|
||||
}
|
||||
Message<?> reply = this.channelTemplate.sendAndReceive(message, this.requestChannel);
|
||||
Message<?> reply = null;
|
||||
try {
|
||||
reply = this.channelTemplate.sendAndReceive(message, this.requestChannel);
|
||||
} catch (Exception e) {
|
||||
logger.warn("Execution of endpoint by the MessageListener resulted in : " + e);
|
||||
reply = this.toMessage(e);
|
||||
if (reply == null){ // if reply wasn't mapped re-throw
|
||||
if (e instanceof RuntimeException){
|
||||
throw (RuntimeException)e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (reply != null && this.shouldThrowErrors && reply instanceof ErrorMessage) {
|
||||
Throwable error = ((ErrorMessage) reply).getPayload();
|
||||
if (error instanceof RuntimeException) {
|
||||
@@ -226,10 +242,29 @@ public abstract class AbstractMessagingGateway extends AbstractEndpoint {
|
||||
}
|
||||
}
|
||||
|
||||
public InboundMessageMapper<Throwable> getExceptionMapper() {
|
||||
return exceptionMapper;
|
||||
}
|
||||
|
||||
public void setExceptionMapper(InboundMessageMapper<Throwable> exceptionMapper) {
|
||||
this.exceptionMapper = exceptionMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclasses must implement this to map from an Object to a Message.
|
||||
*/
|
||||
protected abstract Message<?> toMessage(Object object);
|
||||
protected Message<?> toMessage(Object object){
|
||||
if (object instanceof Throwable){
|
||||
if (this.exceptionMapper != null){
|
||||
try {
|
||||
return exceptionMapper.toMessage((Throwable) object);
|
||||
} catch (Exception e2) {
|
||||
logger.warn("Problem mapping " + object + " to message with: " + exceptionMapper);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclasses must implement this to map from a Message to an Object.
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.core.MessageChannel;
|
||||
import org.springframework.integration.endpoint.AbstractEndpoint;
|
||||
import org.springframework.integration.handler.ArgumentArrayMessageMapper;
|
||||
import org.springframework.integration.message.InboundMessageMapper;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -48,6 +49,8 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
public class GatewayProxyFactoryBean extends AbstractEndpoint implements FactoryBean<Object>, MethodInterceptor, BeanClassLoaderAware {
|
||||
|
||||
private volatile InboundMessageMapper<Throwable> exceptionMapper;
|
||||
|
||||
private volatile Class<?> serviceInterface;
|
||||
|
||||
private volatile MessageChannel defaultRequestChannel;
|
||||
@@ -287,6 +290,7 @@ public class GatewayProxyFactoryBean extends AbstractEndpoint implements Factory
|
||||
}
|
||||
ArgumentArrayMessageMapper messageMapper = new ArgumentArrayMessageMapper(method, staticHeaders);
|
||||
SimpleMessagingGateway gateway = new SimpleMessagingGateway(messageMapper, new SimpleMessageMapper());
|
||||
gateway.setExceptionMapper(exceptionMapper);
|
||||
if (this.getTaskScheduler() != null) {
|
||||
gateway.setTaskScheduler(this.getTaskScheduler());
|
||||
}
|
||||
@@ -332,5 +336,13 @@ public class GatewayProxyFactoryBean extends AbstractEndpoint implements Factory
|
||||
public void setMethodToChannelMap(Map<String, GatewayMethodDefinition> methodToChannelMap) {
|
||||
this.methodToChannelMap = methodToChannelMap;
|
||||
}
|
||||
|
||||
public InboundMessageMapper<Throwable> getExceptionMapper() {
|
||||
return exceptionMapper;
|
||||
}
|
||||
|
||||
public void setExceptionMapper(InboundMessageMapper<Throwable> exceptionMapper) {
|
||||
this.exceptionMapper = exceptionMapper;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -52,6 +52,14 @@ public class SimpleMessagingGateway extends AbstractMessagingGateway {
|
||||
this.inboundMapper = inboundMapper;
|
||||
this.outboundMapper = outboundMapper;
|
||||
}
|
||||
|
||||
public Message<?> sendAndReceiveMessage(Object object) {
|
||||
return (Message<?>) super.sendAndReceive(object, false);
|
||||
}
|
||||
|
||||
public Object sendAndReceive(Object object) {
|
||||
return super.sendAndReceive(object, true);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@@ -69,19 +77,24 @@ public class SimpleMessagingGateway extends AbstractMessagingGateway {
|
||||
|
||||
@Override
|
||||
protected Message<?> toMessage(Object object) {
|
||||
try {
|
||||
Message<?> message = this.inboundMapper.toMessage(object);
|
||||
if (message != null) {
|
||||
message.getHeaders().getHistory().addEvent(this);
|
||||
Message<?> message = null;
|
||||
if (object instanceof Throwable){
|
||||
message = super.toMessage(object);
|
||||
} else {
|
||||
try {
|
||||
message = this.inboundMapper.toMessage(object);
|
||||
if (message != null) {
|
||||
message.getHeaders().getHistory().addEvent(this);
|
||||
}
|
||||
}
|
||||
return message;
|
||||
}
|
||||
catch (Exception e) {
|
||||
if (e instanceof RuntimeException) {
|
||||
throw (RuntimeException) e;
|
||||
catch (Exception e) {
|
||||
if (e instanceof RuntimeException) {
|
||||
throw (RuntimeException) e;
|
||||
}
|
||||
throw new MessagingException("failed to create Message", e);
|
||||
}
|
||||
throw new MessagingException("failed to create Message", e);
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -533,6 +533,16 @@
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="exception-mapper" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
<![CDATA[
|
||||
Allows you to provide implementation of InboundMessageMapper which allows you to map
|
||||
an Exception thrown by the endpoint to a successfull return message.
|
||||
]]>
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="default-request-channel" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
|
||||
@@ -13,14 +13,15 @@
|
||||
<si:gateway id="gatewayWithError"
|
||||
default-request-channel="routingChannel"
|
||||
service-interface="org.springframework.integration.gateway.GatewayInvokingMessageHandlerTests$SimpleGateway"/>
|
||||
|
||||
|
||||
<si:gateway id="gatewayWithErrorAndMapper"
|
||||
default-request-channel="routingChannel"
|
||||
service-interface="org.springframework.integration.gateway.GatewayInvokingMessageHandlerTests$SimpleGateway"
|
||||
exception-mapper="exceptionMapper"/>
|
||||
|
||||
|
||||
<si:router input-channel="routingChannel" expression="payload"/>
|
||||
|
||||
|
||||
<si:service-activator input-channel="echoWithErrorMessageChannel" method="echoWithErrorMessage">
|
||||
<bean class="org.springframework.integration.gateway.GatewayInvokingMessageHandlerTests$SimpleService" />
|
||||
</si:service-activator>
|
||||
|
||||
<si:service-activator input-channel="echoWithRuntimeExceptionChannel" method="echoWithRuntimeException">
|
||||
<bean class="org.springframework.integration.gateway.GatewayInvokingMessageHandlerTests$SimpleService" />
|
||||
</si:service-activator>
|
||||
@@ -29,18 +30,6 @@
|
||||
<bean class="org.springframework.integration.gateway.GatewayInvokingMessageHandlerTests$SimpleService" />
|
||||
</si:service-activator>
|
||||
|
||||
<si:service-activator input-channel="echoWithCheckedExceptionChannel" method="echoWithCheckedException">
|
||||
<bean class="org.springframework.integration.gateway.GatewayInvokingMessageHandlerTests$SimpleService" />
|
||||
</si:service-activator>
|
||||
|
||||
<si:service-activator input-channel="echoWithRuntimeExceptionChannel" method="echoWithRuntimeExceptionThrown">
|
||||
<bean class="org.springframework.integration.gateway.GatewayInvokingMessageHandlerTests$SimpleService" />
|
||||
</si:service-activator>
|
||||
|
||||
<si:service-activator input-channel="echoWithMessagingExceptionChannel" method="echoWithMessagingExceptionThrown">
|
||||
<bean class="org.springframework.integration.gateway.GatewayInvokingMessageHandlerTests$SimpleService" />
|
||||
</si:service-activator>
|
||||
|
||||
<si:publish-subscribe-channel id="inputA" />
|
||||
<si:publish-subscribe-channel id="inputB" />
|
||||
<si:publish-subscribe-channel id="inputC" />
|
||||
@@ -75,5 +64,7 @@
|
||||
<bean class="org.springframework.integration.gateway.GatewayInvokingMessageHandlerTests$SimpleService" />
|
||||
</si:service-activator>
|
||||
</si:chain>
|
||||
|
||||
<bean id="exceptionMapper" class="org.springframework.integration.gateway.GatewayInvokingMessageHandlerTests$SampleExceptionMapper"/>
|
||||
|
||||
</beans>
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.integration.channel.SubscribableChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.message.InboundMessageMapper;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.message.MessageHandler;
|
||||
import org.springframework.integration.message.MessageHandlingException;
|
||||
@@ -51,6 +52,11 @@ public class GatewayInvokingMessageHandlerTests {
|
||||
@Qualifier("gatewayWithError")
|
||||
SimpleGateway gatewayWithError;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("gatewayWithErrorAndMapper")
|
||||
SimpleGateway gatewayWithErrorAndMapper;
|
||||
|
||||
|
||||
@Autowired
|
||||
@Qualifier("inputB")
|
||||
SubscribableChannel output;
|
||||
@@ -82,39 +88,44 @@ public class GatewayInvokingMessageHandlerTests {
|
||||
|
||||
@Test
|
||||
public void validateGatewayWithErrorMessageReturned() {
|
||||
|
||||
try {
|
||||
gatewayWithError.sendRecieve("echoWithErrorMessageChannel");
|
||||
Assert.fail();
|
||||
String result = gatewayWithErrorAndMapper.sendRecieve("echoWithRuntimeExceptionChannel");
|
||||
Assert.assertNotNull(result);
|
||||
Assert.assertEquals("Error happened in message: echoWithRuntimeExceptionChannel", result);
|
||||
} catch (Exception e) {
|
||||
Assert.assertEquals("echoWithErrorMessageChannel", e.getMessage());
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
try {
|
||||
gatewayWithError.sendRecieve("echoWithRuntimeExceptionChannel");
|
||||
Assert.fail();
|
||||
} catch (Exception e) {
|
||||
Assert.assertEquals("echoWithRuntimeExceptionChannel", e.getMessage());
|
||||
} catch (MessageHandlingException e) {
|
||||
Assert.assertEquals("echoWithRuntimeExceptionChannel", e.getFailedMessage().getPayload());
|
||||
}
|
||||
|
||||
try {
|
||||
gatewayWithError.sendRecieve("echoWithMessagingExceptionChannel");
|
||||
Assert.fail();
|
||||
} catch (MessageHandlingException e) {
|
||||
Assert.assertEquals("echoWithMessagingExceptionChannel", e.getFailedMessage().getPayload());
|
||||
}
|
||||
|
||||
try {
|
||||
gatewayWithError.sendRecieve("echoWithCheckedExceptionChannel");
|
||||
Assert.fail();
|
||||
String result = gatewayWithErrorAndMapper.sendRecieve("echoWithMessagingExceptionChannel");
|
||||
Assert.assertNotNull(result);
|
||||
Assert.assertEquals("Error happened in message: echoWithMessagingExceptionChannel", result);
|
||||
} catch (Exception e) {
|
||||
Assert.assertEquals("echoWithCheckedExceptionChannel", e.getCause().getMessage());
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
//String result = gatewayWithError.sendRecieve("echoWithErrorMessageChannel");
|
||||
//System.out.println("Result: " + result);
|
||||
//Assert.assertEquals("echo:echo:echo:hello", result);
|
||||
}
|
||||
|
||||
|
||||
public static class SampleExceptionMapper implements InboundMessageMapper<Throwable>{
|
||||
public Message<?> toMessage(Throwable object) throws Exception {
|
||||
MessageHandlingException ex = (MessageHandlingException) object;
|
||||
return MessageBuilder.withPayload("Error happened in message: " + ex.getFailedMessage().getPayload()).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static interface SimpleGateway {
|
||||
public String sendRecieve(String str);
|
||||
@@ -124,22 +135,11 @@ public class GatewayInvokingMessageHandlerTests {
|
||||
public String echo(String value) {
|
||||
return "echo:" + value;
|
||||
}
|
||||
public Message echoWithErrorMessage(String value) {
|
||||
return MessageBuilder.withPayload(new RuntimeException(value)).build();
|
||||
}
|
||||
public RuntimeException echoWithRuntimeException(String value) {
|
||||
return new RuntimeException(value);
|
||||
}
|
||||
public MessageHandlingException echoWithMessagingException(String value) {
|
||||
return new MessageHandlingException(new StringMessage(value));
|
||||
}
|
||||
public SampleCheckedException echoWithCheckedException(String value) {
|
||||
return new SampleCheckedException(value);
|
||||
}
|
||||
public String echoWithRuntimeExceptionThrown(String value) {
|
||||
throw new RuntimeException(value);
|
||||
}
|
||||
public String echoWithMessagingExceptionThrown(String value) {
|
||||
|
||||
public MessageHandlingException echoWithMessagingException(String value) {
|
||||
throw new MessageHandlingException(new StringMessage(value));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user