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;
}

View File

@@ -201,6 +201,10 @@ public class ChannelPublishingJmsMessageListener extends AbstractMessagingGatewa
public void setExtractReplyPayload(boolean extractReplyPayload) {
this.extractReplyPayload = extractReplyPayload;
}
/*
* (non-Javadoc)
* @see org.springframework.integration.gateway.AbstractMessagingGateway#onInit()
*/
public final void onInit() {
if (!(this.messageConverter instanceof HeaderMappingMessageConverter)) {
HeaderMappingMessageConverter hmmc = new HeaderMappingMessageConverter(this.messageConverter, this.headerMapper);
@@ -209,7 +213,9 @@ public class ChannelPublishingJmsMessageListener extends AbstractMessagingGatewa
this.messageConverter = hmmc;
}
}
/**
*
*/
public void onMessage(javax.jms.Message jmsMessage, Session session) throws JMSException {
Object object = this.messageConverter.fromMessage(jmsMessage);
Message<?> requestMessage = (object instanceof Message<?>) ?
@@ -221,7 +227,7 @@ public class ChannelPublishingJmsMessageListener extends AbstractMessagingGatewa
Message<?> replyMessage = this.sendAndReceiveMessage(requestMessage);
if (replyMessage != null){
Destination destination = this.getReplyDestination(jmsMessage, session, false);
Destination destination = this.getReplyDestination(jmsMessage, session);
if (destination != null){
javax.jms.Message jmsReply = this.messageConverter.toMessage(replyMessage, session);
if (jmsReply.getJMSCorrelationID() == null) {
@@ -263,7 +269,7 @@ public class ChannelPublishingJmsMessageListener extends AbstractMessagingGatewa
* @see #setDefaultReplyDestination
* @see javax.jms.Message#getJMSReplyTo()
*/
private Destination getReplyDestination(javax.jms.Message request, Session session, boolean error) throws JMSException {
private Destination getReplyDestination(javax.jms.Message request, Session session) throws JMSException {
Destination replyTo = request.getJMSReplyTo();
if (replyTo == null) {
@@ -314,20 +320,4 @@ public class ChannelPublishingJmsMessageListener extends AbstractMessagingGatewa
this.isTopic = isTopic;
}
}
@Override
protected Object fromMessage(Message<?> message) {
throw new UnsupportedOperationException("'fromMessage' is not supported within this instance");
}
@Override
protected Message<?> toMessage(Object object) {
Message<?> message = null;
if (object instanceof Throwable){
message = super.toMessage(object);
} else if (object instanceof Message<?>) {
message = (Message<?>) object;
}
return message;
}
}