Skip to content

Commit 616a48a

Browse files
committed
SPR-6142 AbstractMessageListenerContainer now accepts an implementation of the ErrorHandler strategy for handling any uncaught exceptions thrown while processing a Message. The default behavior is still to log at error-level only (no ErrorHandler).
1 parent 33265ee commit 616a48a

2 files changed

Lines changed: 96 additions & 5 deletions

File tree

‎org.springframework.jms/src/main/java/org/springframework/jms/listener/AbstractMessageListenerContainer.java‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828

2929
import org.springframework.jms.support.JmsUtils;
3030
import org.springframework.util.Assert;
31+
import org.springframework.util.ErrorHandler;
3132

3233
/**
3334
* Abstract base class for message listener containers. Can either host
@@ -44,7 +45,11 @@
4445
* is to <b>never</b> propagate an exception thrown by a message listener up to
4546
* the JMS provider. Instead, it will log any such exception at the error level.
4647
* This means that from the perspective of the attendant JMS provider no such
47-
* listener will ever fail.
48+
* listener will ever fail. However, if error handling is necessary, then
49+
* any implementation of the {@link ErrorHandler} strategy may be provided to
50+
* the {@link #setErrorHandler(ErrorHandler)} method. Note that JMSExceptions
51+
* <b>will</b> be passed to the ErrorHandler in addition to (but after) being
52+
* passed to an {@link ExceptionListener}, if one has been provided.
4853
*
4954
* <p>The listener container offers the following message acknowledgment options:
5055
* <ul>
@@ -132,6 +137,8 @@ public abstract class AbstractMessageListenerContainer extends AbstractJmsListen
132137

133138
private ExceptionListener exceptionListener;
134139

140+
private ErrorHandler errorHandler;
141+
135142
private boolean exposeListenerSession = true;
136143

137144
private boolean acceptMessagesWhileStopping = false;
@@ -342,6 +349,15 @@ public ExceptionListener getExceptionListener() {
342349
return this.exceptionListener;
343350
}
344351

352+
/**
353+
* Set an ErrorHandler to be invoked in case of any uncaught exceptions thrown
354+
* while processing a Message. By default there will be <b>no</b> ErrorHandler
355+
* so that error-level logging is the only result.
356+
*/
357+
public void setErrorHandler(ErrorHandler errorHandler) {
358+
this.errorHandler = errorHandler;
359+
}
360+
345361
/**
346362
* Set whether to expose the listener JMS Session to a registered
347363
* {@link SessionAwareMessageListener} as well as to
@@ -642,8 +658,8 @@ protected void handleListenerException(Throwable ex) {
642658
}
643659
if (isActive()) {
644660
// Regular case: failed while active.
645-
// Log at error level.
646-
logger.warn("Execution of JMS message listener failed", ex);
661+
// Invoke ErrorHandler if available.
662+
invokeErrorHandler(ex);
647663
}
648664
else {
649665
// Rare case: listener thread failed after container shutdown.
@@ -664,6 +680,20 @@ protected void invokeExceptionListener(JMSException ex) {
664680
}
665681
}
666682

683+
/**
684+
* Invoke the registered ErrorHandler, if any. Log at error level otherwise.
685+
* @param ex the uncaught error that arose during JMS processing.
686+
* @see #setErrorHandler
687+
*/
688+
protected void invokeErrorHandler(Throwable ex) {
689+
if (this.errorHandler != null) {
690+
this.errorHandler.handleError(ex);
691+
}
692+
else if (logger.isWarnEnabled()) {
693+
logger.warn("Execution of JMS message listener failed, and no ErrorHandler has been set.", ex);
694+
}
695+
}
696+
667697

668698
/**
669699
* Internal exception class that indicates a rejected message on shutdown.

‎org.springframework.jms/src/test/java/org/springframework/jms/listener/SimpleMessageListenerContainerTests.java‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,17 +29,20 @@
2929
import javax.jms.MessageListener;
3030
import javax.jms.Session;
3131

32+
import org.easymock.EasyMock;
3233
import org.easymock.MockControl;
3334
import org.easymock.internal.AlwaysMatcher;
3435
import org.junit.Before;
3536
import org.junit.Test;
3637
import org.springframework.core.task.TaskExecutor;
3738
import org.springframework.jms.StubQueue;
39+
import org.springframework.util.ErrorHandler;
3840

3941
/**
4042
* @author Rick Evans
4143
* @author Juergen Hoeller
4244
* @author Chris Beams
45+
* @author Mark Fisher
4346
*/
4447
public class SimpleMessageListenerContainerTests extends AbstractMessageListenerContainerTests {
4548

@@ -380,6 +383,64 @@ public void onMessage(Message message, Session session) throws JMSException {
380383
mockConnectionFactory.verify();
381384
}
382385

386+
@Test
387+
public void testRegisteredErrorHandlerIsInvokedOnException() throws Exception {
388+
final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer();
389+
390+
Session session = EasyMock.createMock(Session.class);
391+
392+
// Queue gets created in order to create MessageConsumer for that Destination...
393+
session.createQueue(DESTINATION_NAME);
394+
EasyMock.expectLastCall().andReturn(QUEUE_DESTINATION);
395+
// and then the MessageConsumer gets created...
396+
session.createConsumer(QUEUE_DESTINATION, null); // no MessageSelector...
397+
EasyMock.expectLastCall().andReturn(messageConsumer);
398+
// an exception is thrown, so the rollback logic is being applied here...
399+
session.getTransacted();
400+
EasyMock.expectLastCall().andReturn(false);
401+
EasyMock.replay(session);
402+
403+
Connection connection = EasyMock.createMock(Connection.class);
404+
connection.setExceptionListener(this.container);
405+
// session gets created in order to register MessageListener...
406+
connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode());
407+
EasyMock.expectLastCall().andReturn(session);
408+
// and the connection is start()ed after the listener is registered...
409+
connection.start();
410+
EasyMock.replay(connection);
411+
412+
ConnectionFactory connectionFactory = EasyMock.createMock(ConnectionFactory.class);
413+
connectionFactory.createConnection();
414+
EasyMock.expectLastCall().andReturn(connection);
415+
EasyMock.replay(connectionFactory);
416+
417+
final IllegalStateException theException = new IllegalStateException("intentional test failure");
418+
419+
this.container.setConnectionFactory(connectionFactory);
420+
this.container.setDestinationName(DESTINATION_NAME);
421+
this.container.setMessageListener(new SessionAwareMessageListener() {
422+
public void onMessage(Message message, Session session) throws JMSException {
423+
throw theException;
424+
}
425+
});
426+
427+
ErrorHandler errorHandler = EasyMock.createMock(ErrorHandler.class);
428+
errorHandler.handleError(theException);
429+
EasyMock.expectLastCall();
430+
EasyMock.replay(errorHandler);
431+
this.container.setErrorHandler(errorHandler);
432+
this.container.afterPropertiesSet();
433+
434+
// manually trigger an Exception with the above bad MessageListener...
435+
Message message = EasyMock.createMock(Message.class);
436+
EasyMock.replay(message);
437+
438+
// a Throwable from a MessageListener MUST simply be swallowed...
439+
messageConsumer.sendMessage(message);
440+
441+
EasyMock.verify(errorHandler, message, session, connection, connectionFactory);
442+
}
443+
383444
@Test
384445
public void testNoRollbackOccursIfSessionIsNotTransactedAndThatExceptionsDo_NOT_Propagate() throws Exception {
385446
final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer();

0 commit comments

Comments
 (0)