JmsMessagingTemplate exception management
This commit introduces MessagingExceptionTranslator, a messaging exception translation infrastructure similar to what PersistenceExceptionTranslator provides. JmsMessagingTemplate does not throw raw JmsException anymore but translates those to an instance of Spring's MessagingException hierarchy. Issue: SPR-12038
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.jms.core;
|
||||
|
||||
import org.springframework.jms.InvalidDestinationException;
|
||||
import org.springframework.jms.JmsException;
|
||||
import org.springframework.jms.support.converter.MessageConversionException;
|
||||
import org.springframework.jms.support.destination.DestinationResolutionException;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.messaging.MessagingExceptionTranslator;
|
||||
|
||||
/**
|
||||
* {@link MessagingExceptionTranslator} capable of translating {@link JmsException}
|
||||
* instances to Spring's {@link MessagingException} hierarchy.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 4.1
|
||||
*/
|
||||
public class JmsMessagingExceptionTranslator implements MessagingExceptionTranslator {
|
||||
|
||||
@Override
|
||||
public MessagingException translateExceptionIfPossible(RuntimeException ex) {
|
||||
if (ex instanceof JmsException) {
|
||||
return convertJmsException((JmsException) ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private MessagingException convertJmsException(JmsException ex) {
|
||||
if (ex instanceof DestinationResolutionException ||
|
||||
ex instanceof InvalidDestinationException) {
|
||||
return new org.springframework.messaging.core.DestinationResolutionException(ex.getMessage(), ex);
|
||||
}
|
||||
if (ex instanceof MessageConversionException) {
|
||||
return new org.springframework.messaging.converter.MessageConversionException(ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
|
||||
// Fallback
|
||||
return new MessagingException(ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
@@ -22,12 +22,14 @@ import javax.jms.JMSException;
|
||||
import javax.jms.Session;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.jms.support.converter.MessageConversionException;
|
||||
import org.springframework.jms.JmsException;
|
||||
import org.springframework.jms.support.converter.MessageConverter;
|
||||
import org.springframework.jms.support.converter.MessagingMessageConverter;
|
||||
import org.springframework.jms.support.converter.SimpleMessageConverter;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.messaging.MessagingExceptionTranslator;
|
||||
import org.springframework.messaging.converter.MessageConversionException;
|
||||
import org.springframework.messaging.core.AbstractMessagingTemplate;
|
||||
import org.springframework.messaging.core.MessagePostProcessor;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -45,6 +47,8 @@ public class JmsMessagingTemplate extends AbstractMessagingTemplate<Destination>
|
||||
|
||||
private MessageConverter jmsMessageConverter = new MessagingMessageConverter();
|
||||
|
||||
private MessagingExceptionTranslator exceptionTranslator = new JmsMessagingExceptionTranslator();
|
||||
|
||||
private String defaultDestinationName;
|
||||
|
||||
|
||||
@@ -85,6 +89,14 @@ public class JmsMessagingTemplate extends AbstractMessagingTemplate<Destination>
|
||||
this.jmsMessageConverter = jmsMessageConverter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link MessagingExceptionTranslator} to use. Default to
|
||||
* {@link JmsMessagingExceptionTranslator}.
|
||||
*/
|
||||
public void setExceptionTranslator(MessagingExceptionTranslator exceptionTranslator) {
|
||||
this.exceptionTranslator = exceptionTranslator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the default destination name to use in send methods that don't have
|
||||
* a destination argument. If a default destination is not configured, send methods
|
||||
@@ -271,35 +283,65 @@ public class JmsMessagingTemplate extends AbstractMessagingTemplate<Destination>
|
||||
|
||||
@Override
|
||||
protected void doSend(Destination destination, Message<?> message) {
|
||||
this.jmsTemplate.send(destination, createMessageCreator(message));
|
||||
try {
|
||||
this.jmsTemplate.send(destination, createMessageCreator(message));
|
||||
}
|
||||
catch (JmsException ex) {
|
||||
throw translateIfNecessary(ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected void doSend(String destinationName, Message<?> message) {
|
||||
this.jmsTemplate.send(destinationName, createMessageCreator(message));
|
||||
try {
|
||||
this.jmsTemplate.send(destinationName, createMessageCreator(message));
|
||||
}
|
||||
catch (JmsException ex) {
|
||||
throw translateIfNecessary(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Message<?> doReceive(Destination destination) {
|
||||
javax.jms.Message jmsMessage = this.jmsTemplate.receive(destination);
|
||||
return doConvert(jmsMessage);
|
||||
try {
|
||||
javax.jms.Message jmsMessage = this.jmsTemplate.receive(destination);
|
||||
return doConvert(jmsMessage);
|
||||
}
|
||||
catch (JmsException ex) {
|
||||
throw translateIfNecessary(ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected Message<?> doReceive(String destinationName) {
|
||||
javax.jms.Message jmsMessage = this.jmsTemplate.receive(destinationName);
|
||||
return doConvert(jmsMessage);
|
||||
try {
|
||||
javax.jms.Message jmsMessage = this.jmsTemplate.receive(destinationName);
|
||||
return doConvert(jmsMessage);
|
||||
}
|
||||
catch (JmsException ex) {
|
||||
throw translateIfNecessary(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Message<?> doSendAndReceive(Destination destination, Message<?> requestMessage) {
|
||||
javax.jms.Message jmsMessage = this.jmsTemplate
|
||||
.sendAndReceive(destination, createMessageCreator(requestMessage));
|
||||
return doConvert(jmsMessage);
|
||||
try {
|
||||
javax.jms.Message jmsMessage = this.jmsTemplate
|
||||
.sendAndReceive(destination, createMessageCreator(requestMessage));
|
||||
return doConvert(jmsMessage);
|
||||
}
|
||||
catch (JmsException ex) {
|
||||
throw translateIfNecessary(ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected Message<?> doSendAndReceive(String destinationName, Message<?> requestMessage) {
|
||||
javax.jms.Message jmsMessage = this.jmsTemplate
|
||||
.sendAndReceive(destinationName, createMessageCreator(requestMessage));
|
||||
return doConvert(jmsMessage);
|
||||
try {
|
||||
javax.jms.Message jmsMessage = this.jmsTemplate
|
||||
.sendAndReceive(destinationName, createMessageCreator(requestMessage));
|
||||
return doConvert(jmsMessage);
|
||||
}
|
||||
catch (JmsException ex) {
|
||||
throw translateIfNecessary(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private MessagingMessageCreator createMessageCreator(Message<?> message) {
|
||||
@@ -325,6 +367,15 @@ public class JmsMessagingTemplate extends AbstractMessagingTemplate<Destination>
|
||||
catch (JMSException ex) {
|
||||
throw new MessageConversionException("Could not convert '" + message + "'", ex);
|
||||
}
|
||||
catch (JmsException ex) {
|
||||
throw new MessageConversionException("Could not convert '" + message + "'", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
|
||||
protected RuntimeException translateIfNecessary(RuntimeException rawException) {
|
||||
MessagingException messagingException = this.exceptionTranslator.translateExceptionIfPossible(rawException);
|
||||
return (messagingException != null ? messagingException : rawException);
|
||||
}
|
||||
|
||||
|
||||
@@ -341,7 +392,15 @@ public class JmsMessagingTemplate extends AbstractMessagingTemplate<Destination>
|
||||
|
||||
@Override
|
||||
public javax.jms.Message createMessage(Session session) throws JMSException {
|
||||
return this.messageConverter.toMessage(this.message, session);
|
||||
try {
|
||||
return this.messageConverter.toMessage(this.message, session);
|
||||
}
|
||||
catch (JMSException ex) {
|
||||
throw new MessageConversionException("Could not convert '" + message + "'", ex);
|
||||
}
|
||||
catch (JmsException ex) {
|
||||
throw new MessageConversionException("Could not convert '" + message + "'", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user