use of jakarta.jms.ExceptionListener in project spring-framework by spring-projects.
the class SimpleMessageListenerContainerTests method testRegisteredExceptionListenerIsInvokedOnException.
@Test
public void testRegisteredExceptionListenerIsInvokedOnException() throws Exception {
final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer();
Session session = mock(Session.class);
// Queue gets created in order to create MessageConsumer for that Destination...
given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION);
// and then the MessageConsumer gets created...
// no MessageSelector...
given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer);
// an exception is thrown, so the rollback logic is being applied here...
given(session.getTransacted()).willReturn(false);
Connection connection = mock(Connection.class);
// session gets created in order to register MessageListener...
given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session);
// and the connection is start()ed after the listener is registered...
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
given(connectionFactory.createConnection()).willReturn(connection);
final JMSException theException = new JMSException(EXCEPTION_MESSAGE);
this.container.setConnectionFactory(connectionFactory);
this.container.setDestinationName(DESTINATION_NAME);
this.container.setMessageListener((SessionAwareMessageListener<Message>) (Message message, @Nullable Session session1) -> {
throw theException;
});
ExceptionListener exceptionListener = mock(ExceptionListener.class);
this.container.setExceptionListener(exceptionListener);
this.container.afterPropertiesSet();
this.container.start();
// manually trigger an Exception with the above bad MessageListener...
final Message message = mock(Message.class);
// a Throwable from a MessageListener MUST simply be swallowed...
messageConsumer.sendMessage(message);
verify(connection).setExceptionListener(this.container);
verify(connection).start();
verify(exceptionListener).onException(theException);
}
use of jakarta.jms.ExceptionListener in project spring-boot by spring-projects.
the class JmsAutoConfigurationTests method testDefaultContainerFactoryWithExceptionListener.
@Test
void testDefaultContainerFactoryWithExceptionListener() {
ExceptionListener exceptionListener = mock(ExceptionListener.class);
this.contextRunner.withUserConfiguration(EnableJmsConfiguration.class).withBean(ExceptionListener.class, () -> exceptionListener).run((context) -> {
DefaultMessageListenerContainer container = getContainer(context, "jmsListenerContainerFactory");
assertThat(container.getExceptionListener()).isSameAs(exceptionListener);
});
}
use of jakarta.jms.ExceptionListener in project spring-framework by spring-projects.
the class SingleConnectionFactoryTests method testWithConnectionFactoryAndExceptionListener.
@Test
public void testWithConnectionFactoryAndExceptionListener() throws JMSException {
ConnectionFactory cf = mock(ConnectionFactory.class);
Connection con = mock(Connection.class);
ExceptionListener listener = new ChainedExceptionListener();
given(cf.createConnection()).willReturn(con);
given(con.getExceptionListener()).willReturn(listener);
SingleConnectionFactory scf = new SingleConnectionFactory(cf);
scf.setExceptionListener(listener);
Connection con1 = scf.createConnection();
assertThat(con1.getExceptionListener()).isEqualTo(listener);
con1.start();
con1.stop();
con1.close();
Connection con2 = scf.createConnection();
con2.start();
con2.stop();
con2.close();
// should trigger actual close
scf.destroy();
verify(con).setExceptionListener(listener);
verify(con, times(2)).start();
verify(con, times(2)).stop();
verify(con).close();
}
Aggregations