INT-1194, MOdified the ErrorMessage to allow for headers to be set as well as Message builder which builds Message as ErrorMessage if payload is of type Throwable

This commit is contained in:
Oleg Zhurakousky
2010-06-24 12:46:50 +00:00
parent d5d0857f22
commit e07a8ace15
4 changed files with 120 additions and 7 deletions

View File

@@ -16,10 +16,13 @@
package org.springframework.integration.message;
import java.util.Map;
/**
* A message implementation that accepts a {@link Throwable} payload.
*
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
public class ErrorMessage extends GenericMessage<Throwable> {
@@ -28,5 +31,9 @@ public class ErrorMessage extends GenericMessage<Throwable> {
public ErrorMessage(Throwable payload) {
super(payload);
}
public ErrorMessage(Throwable payload, Map<String, Object> headers) {
super(payload, headers);
}
}

View File

@@ -32,6 +32,7 @@ import org.springframework.util.StringUtils;
/**
* @author Arjen Poutsma
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
public final class MessageBuilder<T> {
@@ -196,11 +197,17 @@ public final class MessageBuilder<T> {
return this.setHeader(MessageHeaders.PRIORITY, priority);
}
@SuppressWarnings("unchecked")
public Message<T> build() {
if (!this.modified && this.originalMessage != null) {
return this.originalMessage;
}
return new GenericMessage<T>(this.payload, this.headers);
if (payload instanceof Throwable){
Throwable t = (Throwable) payload;
return (Message<T>) new ErrorMessage(t, this.headers);
} else {
return new GenericMessage<T>(this.payload, this.headers);
}
}
private void verifyType(String headerName, Object headerValue) {

View File

@@ -9,6 +9,37 @@
default-request-channel="inputA"
default-reply-channel="inputB"
service-interface="org.springframework.integration.gateway.GatewayInvokingMessageHandlerTests$SimpleGateway"/>
<si:gateway id="gatewayWithError"
default-request-channel="routingChannel"
service-interface="org.springframework.integration.gateway.GatewayInvokingMessageHandlerTests$SimpleGateway"/>
<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>
<si:service-activator input-channel="echoWithMessagingExceptionChannel" method="echoWithMessagingException">
<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" />
@@ -18,7 +49,7 @@
<si:header-enricher>
<si:header name="foo" value="foo" />
</si:header-enricher>
<si:service-activator>
<si:service-activator method="echo">
<bean class="org.springframework.integration.gateway.GatewayInvokingMessageHandlerTests$SimpleService" />
</si:service-activator>
<si:gateway request-channel="inputC"/>
@@ -31,7 +62,7 @@
<si:header name="name" value="oleg" />
</si:header-enricher>
<si:gateway request-channel="inputD"/>
<si:service-activator>
<si:service-activator method="echo">
<bean class="org.springframework.integration.gateway.GatewayInvokingMessageHandlerTests$SimpleService" />
</si:service-activator>
</si:chain>
@@ -40,7 +71,7 @@
<si:header-enricher>
<si:header name="name" value="oleg" />
</si:header-enricher>
<si:service-activator>
<si:service-activator method="echo">
<bean class="org.springframework.integration.gateway.GatewayInvokingMessageHandlerTests$SimpleService" />
</si:service-activator>
</si:chain>

View File

@@ -20,16 +20,20 @@ import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
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.ErrorMessage;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.integration.message.MessageHandler;
import org.springframework.integration.message.MessageHandlingException;
import org.springframework.integration.message.StringMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;
/**
* @author Oleg Zhurakousky
* @since 2.0
@@ -46,6 +50,10 @@ public class GatewayInvokingMessageHandlerTests {
@Qualifier("simpleGateway")
SimpleGateway gateway;
@Autowired
@Qualifier("gatewayWithError")
SimpleGateway gatewayWithError;
@Autowired
@Qualifier("inputB")
SubscribableChannel output;
@@ -74,14 +82,74 @@ public class GatewayInvokingMessageHandlerTests {
String result = gateway.sendRecieve("hello");
Assert.assertEquals("echo:echo:echo:hello", result);
}
@Test
public void validateGatewayWithErrorMessageReturned() {
try {
gatewayWithError.sendRecieve("echoWithErrorMessageChannel");
Assert.fail();
} catch (Exception e) {
Assert.assertEquals("echoWithErrorMessageChannel", e.getMessage());
}
try {
gatewayWithError.sendRecieve("echoWithRuntimeExceptionChannel");
Assert.fail();
} catch (Exception e) {
Assert.assertEquals("echoWithRuntimeExceptionChannel", e.getMessage());
}
try {
gatewayWithError.sendRecieve("echoWithMessagingExceptionChannel");
Assert.fail();
} catch (MessageHandlingException e) {
Assert.assertEquals("echoWithMessagingExceptionChannel", e.getFailedMessage().getPayload());
}
try {
gatewayWithError.sendRecieve("echoWithCheckedExceptionChannel");
Assert.fail();
} catch (Exception e) {
Assert.assertEquals("echoWithCheckedExceptionChannel", e.getCause().getMessage());
}
//String result = gatewayWithError.sendRecieve("echoWithErrorMessageChannel");
//System.out.println("Result: " + result);
//Assert.assertEquals("echo:echo:echo:hello", result);
}
public static interface SimpleGateway {
public String sendRecieve(String str);
}
public static class SimpleService {
public String echo(String str) {
return "echo:" + str;
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) {
throw new MessageHandlingException(new StringMessage(value));
}
}
public static class SampleCheckedException extends Exception {
public SampleCheckedException(String message){
super(message);
}
}