Search in sources :

Example 1 with ExceptionListener

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);
}
Also used : ConnectionFactory(jakarta.jms.ConnectionFactory) Message(jakarta.jms.Message) Connection(jakarta.jms.Connection) JMSException(jakarta.jms.JMSException) ExceptionListener(jakarta.jms.ExceptionListener) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 2 with ExceptionListener

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);
    });
}
Also used : DefaultMessageListenerContainer(org.springframework.jms.listener.DefaultMessageListenerContainer) ExceptionListener(jakarta.jms.ExceptionListener) Test(org.junit.jupiter.api.Test)

Example 3 with 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();
}
Also used : TopicConnectionFactory(jakarta.jms.TopicConnectionFactory) ConnectionFactory(jakarta.jms.ConnectionFactory) QueueConnectionFactory(jakarta.jms.QueueConnectionFactory) QueueConnection(jakarta.jms.QueueConnection) TopicConnection(jakarta.jms.TopicConnection) Connection(jakarta.jms.Connection) ExceptionListener(jakarta.jms.ExceptionListener) Test(org.junit.jupiter.api.Test)

Aggregations

ExceptionListener (jakarta.jms.ExceptionListener)3 Test (org.junit.jupiter.api.Test)3 Connection (jakarta.jms.Connection)2 ConnectionFactory (jakarta.jms.ConnectionFactory)2 JMSException (jakarta.jms.JMSException)1 Message (jakarta.jms.Message)1 QueueConnection (jakarta.jms.QueueConnection)1 QueueConnectionFactory (jakarta.jms.QueueConnectionFactory)1 Session (jakarta.jms.Session)1 TopicConnection (jakarta.jms.TopicConnection)1 TopicConnectionFactory (jakarta.jms.TopicConnectionFactory)1 DefaultMessageListenerContainer (org.springframework.jms.listener.DefaultMessageListenerContainer)1