INT-904, INT-907 polishing, refactoring of AbstractMessagingGateway and ChannelPublishingJmsMessageListener

This commit is contained in:
Oleg Zhurakousky
2010-06-25 02:55:42 +00:00
parent 1e1c0e84b7
commit fef67c7326
3 changed files with 36 additions and 51 deletions

View File

@@ -31,6 +31,7 @@ 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.integration.message.MessageMappingException;
import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.util.Assert;
@@ -183,7 +184,13 @@ public abstract class AbstractMessagingGateway extends AbstractEndpoint {
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 (this.exceptionMapper != null){
try {
reply = exceptionMapper.toMessage(e);
} catch (Exception e2) {
logger.warn("Problem mapping " + e + " to message with: " + exceptionMapper);
}
}
if (reply == null){ // if reply wasn't mapped re-throw
if (e instanceof RuntimeException){
throw (RuntimeException)e;
@@ -250,25 +257,17 @@ public abstract class AbstractMessagingGateway extends AbstractEndpoint {
this.exceptionMapper = exceptionMapper;
}
/**
* Subclasses must implement this to map from an Object to a Message.
*/
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;
protected Object fromMessage(Message<?> message) {
throw new MessageMappingException("Can not map " + message + " to a object. No Mappers defined");
}
/**
* Subclasses must implement this to map from a Message to an Object.
*/
protected abstract Object fromMessage(Message<?> message);
protected Message<?> toMessage(Object object) {
if (object instanceof Message<?>){
return (Message<?>) object;
} else {
throw new MessageMappingException("Can not map " + object + " to a message. No Mappers defined");
}
}
}

View File

@@ -78,21 +78,17 @@ public class SimpleMessagingGateway extends AbstractMessagingGateway {
@Override
protected Message<?> toMessage(Object object) {
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);
}
try {
message = this.inboundMapper.toMessage(object);
if (message != null) {
message.getHeaders().getHistory().addEvent(this);
}
catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new MessagingException("failed to create Message", e);
}
catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new MessagingException("failed to create Message", e);
}
return message;
}