Search in sources :

Example 16 with JMSException

use of jakarta.jms.JMSException in project spring-framework by spring-projects.

the class SingleConnectionFactoryTests method testWithConnectionFactoryAndExceptionListenerAndReconnectOnException.

@Test
public void testWithConnectionFactoryAndExceptionListenerAndReconnectOnException() throws JMSException {
    ConnectionFactory cf = mock(ConnectionFactory.class);
    TestConnection con = new TestConnection();
    given(cf.createConnection()).willReturn(con);
    TestExceptionListener listener = new TestExceptionListener();
    SingleConnectionFactory scf = new SingleConnectionFactory(cf);
    scf.setExceptionListener(listener);
    scf.setReconnectOnException(true);
    Connection con1 = scf.createConnection();
    assertThat(con1.getExceptionListener()).isSameAs(listener);
    con1.start();
    con.getExceptionListener().onException(new JMSException(""));
    Connection con2 = scf.createConnection();
    con2.start();
    // should trigger actual close
    scf.destroy();
    assertThat(con.getStartCount()).isEqualTo(2);
    assertThat(con.getCloseCount()).isEqualTo(2);
    assertThat(listener.getCount()).isEqualTo(1);
}
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) JMSException(jakarta.jms.JMSException) Test(org.junit.jupiter.api.Test)

Example 17 with JMSException

use of jakarta.jms.JMSException in project spring-framework by spring-projects.

the class SingleConnectionFactoryTests method testWithConnectionFactoryAndLocalExceptionListenerWithCleanup.

@Test
public void testWithConnectionFactoryAndLocalExceptionListenerWithCleanup() throws JMSException {
    ConnectionFactory cf = mock(ConnectionFactory.class);
    TestConnection con = new TestConnection();
    given(cf.createConnection()).willReturn(con);
    TestExceptionListener listener0 = new TestExceptionListener();
    TestExceptionListener listener1 = new TestExceptionListener();
    TestExceptionListener listener2 = new TestExceptionListener();
    SingleConnectionFactory scf = new SingleConnectionFactory(cf) {

        @Override
        public void onException(JMSException ex) {
        // no-op
        }
    };
    scf.setReconnectOnException(true);
    scf.setExceptionListener(listener0);
    Connection con1 = scf.createConnection();
    con1.setExceptionListener(listener1);
    assertThat(con1.getExceptionListener()).isSameAs(listener1);
    Connection con2 = scf.createConnection();
    con2.setExceptionListener(listener2);
    assertThat(con2.getExceptionListener()).isSameAs(listener2);
    con.getExceptionListener().onException(new JMSException(""));
    con2.close();
    con.getExceptionListener().onException(new JMSException(""));
    con1.close();
    con.getExceptionListener().onException(new JMSException(""));
    // should trigger actual close
    scf.destroy();
    assertThat(con.getStartCount()).isEqualTo(0);
    assertThat(con.getCloseCount()).isEqualTo(1);
    assertThat(listener0.getCount()).isEqualTo(3);
    assertThat(listener1.getCount()).isEqualTo(2);
    assertThat(listener2.getCount()).isEqualTo(1);
}
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) JMSException(jakarta.jms.JMSException) Test(org.junit.jupiter.api.Test)

Example 18 with JMSException

use of jakarta.jms.JMSException in project spring-framework by spring-projects.

the class SingleConnectionFactoryTests method testWithConnectionFactoryAndLocalExceptionListenerWithReconnect.

@Test
public void testWithConnectionFactoryAndLocalExceptionListenerWithReconnect() throws JMSException {
    ConnectionFactory cf = mock(ConnectionFactory.class);
    TestConnection con = new TestConnection();
    given(cf.createConnection()).willReturn(con);
    TestExceptionListener listener0 = new TestExceptionListener();
    TestExceptionListener listener1 = new TestExceptionListener();
    TestExceptionListener listener2 = new TestExceptionListener();
    SingleConnectionFactory scf = new SingleConnectionFactory(cf);
    scf.setReconnectOnException(true);
    scf.setExceptionListener(listener0);
    Connection con1 = scf.createConnection();
    con1.setExceptionListener(listener1);
    assertThat(con1.getExceptionListener()).isSameAs(listener1);
    con1.start();
    Connection con2 = scf.createConnection();
    con2.setExceptionListener(listener2);
    assertThat(con2.getExceptionListener()).isSameAs(listener2);
    con.getExceptionListener().onException(new JMSException(""));
    con2.close();
    con1.getMetaData();
    con.getExceptionListener().onException(new JMSException(""));
    con1.close();
    // should trigger actual close
    scf.destroy();
    assertThat(con.getStartCount()).isEqualTo(2);
    assertThat(con.getCloseCount()).isEqualTo(2);
    assertThat(listener0.getCount()).isEqualTo(2);
    assertThat(listener1.getCount()).isEqualTo(2);
    assertThat(listener2.getCount()).isEqualTo(1);
}
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) JMSException(jakarta.jms.JMSException) Test(org.junit.jupiter.api.Test)

Example 19 with JMSException

use of jakarta.jms.JMSException in project spring-framework by spring-projects.

the class DefaultMessageListenerContainerTests method createFailingContainerFactory.

private ConnectionFactory createFailingContainerFactory() {
    try {
        ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
        given(connectionFactory.createConnection()).will(invocation -> {
            throw new JMSException("Test exception");
        });
        return connectionFactory;
    } catch (JMSException ex) {
        // never happen
        throw new IllegalStateException(ex);
    }
}
Also used : ConnectionFactory(jakarta.jms.ConnectionFactory) JMSException(jakarta.jms.JMSException)

Example 20 with JMSException

use of jakarta.jms.JMSException in project spring-framework by spring-projects.

the class DefaultMessageListenerContainerTests method createRecoverableContainerFactory.

private ConnectionFactory createRecoverableContainerFactory(final int failingAttempts) {
    try {
        ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
        given(connectionFactory.createConnection()).will(new Answer<Object>() {

            int currentAttempts = 0;

            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                currentAttempts++;
                if (currentAttempts <= failingAttempts) {
                    throw new JMSException("Test exception (attempt " + currentAttempts + ")");
                } else {
                    return mock(Connection.class);
                }
            }
        });
        return connectionFactory;
    } catch (JMSException ex) {
        // never happen
        throw new IllegalStateException(ex);
    }
}
Also used : ConnectionFactory(jakarta.jms.ConnectionFactory) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Connection(jakarta.jms.Connection) JMSException(jakarta.jms.JMSException)

Aggregations

JMSException (jakarta.jms.JMSException)38 Connection (jakarta.jms.Connection)21 Test (org.junit.jupiter.api.Test)20 ConnectionFactory (jakarta.jms.ConnectionFactory)18 Session (jakarta.jms.Session)16 Destination (jakarta.jms.Destination)10 Message (jakarta.jms.Message)10 MessageProducer (jakarta.jms.MessageProducer)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)8 BDDMockito.given (org.mockito.BDDMockito.given)8 Mockito.mock (org.mockito.Mockito.mock)8 Mockito.times (org.mockito.Mockito.times)8 Mockito.verify (org.mockito.Mockito.verify)8 TransactionStatus (org.springframework.transaction.TransactionStatus)8 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)8 AfterEach (org.junit.jupiter.api.AfterEach)7 StubQueue (org.springframework.jms.StubQueue)7 JmsTemplate (org.springframework.jms.core.JmsTemplate)7 SessionCallback (org.springframework.jms.core.SessionCallback)7