Search in sources :

Example 21 with ConnectionFactory

use of jakarta.jms.ConnectionFactory 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 22 with ConnectionFactory

use of jakarta.jms.ConnectionFactory 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 23 with ConnectionFactory

use of jakarta.jms.ConnectionFactory 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 24 with ConnectionFactory

use of jakarta.jms.ConnectionFactory 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 25 with ConnectionFactory

use of jakarta.jms.ConnectionFactory 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

ConnectionFactory (jakarta.jms.ConnectionFactory)60 Test (org.junit.jupiter.api.Test)42 Connection (jakarta.jms.Connection)32 JMSException (jakarta.jms.JMSException)20 Session (jakarta.jms.Session)19 Message (jakarta.jms.Message)15 QueueConnectionFactory (jakarta.jms.QueueConnectionFactory)15 TopicConnectionFactory (jakarta.jms.TopicConnectionFactory)15 JmsTemplate (org.springframework.jms.core.JmsTemplate)11 QueueConnection (jakarta.jms.QueueConnection)10 TopicConnection (jakarta.jms.TopicConnection)10 StubQueue (org.springframework.jms.StubQueue)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)9 BDDMockito.given (org.mockito.BDDMockito.given)9 Mockito.mock (org.mockito.Mockito.mock)9 Mockito.verify (org.mockito.Mockito.verify)9 TransactionStatus (org.springframework.transaction.TransactionStatus)9 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)9 Destination (jakarta.jms.Destination)8 MessageProducer (jakarta.jms.MessageProducer)8