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:
Stephane Nicoll
2014-07-29 12:29:24 +02:00
parent 8f484d382e
commit 89d63eb79b
5 changed files with 319 additions and 15 deletions

View File

@@ -0,0 +1,34 @@
/*
* 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.junit.Test;
import static org.junit.Assert.*;
/**
* @author Stephane Nicoll
*/
public class JmsMessagingExceptionTranslatorTests {
private final JmsMessagingExceptionTranslator translator = new JmsMessagingExceptionTranslator();
@Test
public void translateNonJmsException() {
assertNull(translator.translateExceptionIfPossible(new NullPointerException()));
}
}

View File

@@ -21,6 +21,8 @@ import java.util.HashMap;
import java.util.Map;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageFormatException;
import javax.jms.MessageNotWriteableException;
import javax.jms.Session;
import javax.jms.TextMessage;
@@ -36,11 +38,16 @@ import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.jms.InvalidDestinationException;
import org.springframework.jms.MessageNotReadableException;
import org.springframework.jms.StubTextMessage;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.converter.SimpleMessageConverter;
import org.springframework.jms.support.destination.DestinationResolutionException;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.converter.GenericMessageConverter;
import org.springframework.messaging.converter.MessageConversionException;
import org.springframework.messaging.support.MessageBuilder;
import static org.junit.Assert.*;
@@ -443,6 +450,110 @@ public class JmsMessagingTemplateTests {
messagingTemplate.convertSendAndReceive("my Payload", String.class);
}
@Test
public void convertMessageConversionExceptionOnSend() throws JMSException {
Message<String> message = createTextMessage();
MessageConverter messageConverter = mock(MessageConverter.class);
doThrow(org.springframework.jms.support.converter.MessageConversionException.class)
.when(messageConverter).toMessage(eq(message), anyObject());
messagingTemplate.setJmsMessageConverter(messageConverter);
invokeMessageCreator("myQueue");
thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
messagingTemplate.send("myQueue", message);
}
@Test
public void convertMessageConversionExceptionOnReceive() throws JMSException {
javax.jms.Message message = createJmsTextMessage();
MessageConverter messageConverter = mock(MessageConverter.class);
doThrow(org.springframework.jms.support.converter.MessageConversionException.class)
.when(messageConverter).fromMessage(message);
messagingTemplate.setJmsMessageConverter(messageConverter);
given(jmsTemplate.receive("myQueue")).willReturn(message);
thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
messagingTemplate.receive("myQueue");
}
@Test
public void convertMessageNotReadableException() throws JMSException {
doThrow(MessageNotReadableException.class).when(jmsTemplate).receive("myQueue");
thrown.expect(MessagingException.class);
messagingTemplate.receive("myQueue");
}
@Test
public void convertDestinationResolutionExceptionOnSend() {
Destination destination = new Destination() {};
doThrow(DestinationResolutionException.class).when(jmsTemplate).send(eq(destination), anyObject());
thrown.expect(org.springframework.messaging.core.DestinationResolutionException.class);
messagingTemplate.send(destination, createTextMessage());
}
@Test
public void convertDestinationResolutionExceptionOnReceive() {
Destination destination = new Destination() {};
doThrow(DestinationResolutionException.class).when(jmsTemplate).receive(destination);
thrown.expect(org.springframework.messaging.core.DestinationResolutionException.class);
messagingTemplate.receive(destination);
}
@Test
public void convertMessageFormatException() throws JMSException {
Message<String> message = createTextMessage();
MessageConverter messageConverter = mock(MessageConverter.class);
doThrow(MessageFormatException.class).when(messageConverter).toMessage(eq(message), anyObject());
messagingTemplate.setJmsMessageConverter(messageConverter);
invokeMessageCreator("myQueue");
thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
messagingTemplate.send("myQueue", message);
}
@Test
public void convertMessageNotWritableException() throws JMSException {
Message<String> message = createTextMessage();
MessageConverter messageConverter = mock(MessageConverter.class);
doThrow(MessageNotWriteableException.class).when(messageConverter).toMessage(eq(message), anyObject());
messagingTemplate.setJmsMessageConverter(messageConverter);
invokeMessageCreator("myQueue");
thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
messagingTemplate.send("myQueue", message);
}
@Test
public void convertInvalidDestinationExceptionOnSendAndReceiveWithName() {
doThrow(InvalidDestinationException.class).when(jmsTemplate).sendAndReceive(eq("unknownQueue"), anyObject());
thrown.expect(org.springframework.messaging.core.DestinationResolutionException.class);
messagingTemplate.sendAndReceive("unknownQueue", createTextMessage());
}
@Test
public void convertInvalidDestinationExceptionOnSendAndReceive() {
Destination destination = new Destination() {};
doThrow(InvalidDestinationException.class).when(jmsTemplate).sendAndReceive(eq(destination), anyObject());
thrown.expect(org.springframework.messaging.core.DestinationResolutionException.class);
messagingTemplate.sendAndReceive(destination, createTextMessage());
}
private void invokeMessageCreator(String destinationName) {
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
MessageCreator messageCreator = (MessageCreator) invocation.getArguments()[1];
messageCreator.createMessage(null);
return null;
}
}).when(jmsTemplate).send(eq("myQueue"), anyObject());
}
private Message<String> createTextMessage(String payload) {
return MessageBuilder