Search in sources :

Example 36 with Connection

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

the class SingleConnectionFactoryTests method testWithQueueConnection.

@Test
public void testWithQueueConnection() throws JMSException {
    Connection con = mock(QueueConnection.class);
    SingleConnectionFactory scf = new SingleConnectionFactory(con);
    QueueConnection con1 = scf.createQueueConnection();
    con1.start();
    con1.stop();
    con1.close();
    QueueConnection con2 = scf.createQueueConnection();
    con2.start();
    con2.stop();
    con2.close();
    // should trigger actual close
    scf.destroy();
    verify(con, times(2)).start();
    verify(con, times(2)).stop();
    verify(con).close();
    verifyNoMoreInteractions(con);
}
Also used : QueueConnection(jakarta.jms.QueueConnection) QueueConnection(jakarta.jms.QueueConnection) TopicConnection(jakarta.jms.TopicConnection) Connection(jakarta.jms.Connection) Test(org.junit.jupiter.api.Test)

Example 37 with Connection

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

Example 38 with Connection

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

the class SingleConnectionFactoryTests method testWithConnectionFactoryAndClientId.

@Test
public void testWithConnectionFactoryAndClientId() throws JMSException {
    ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    given(cf.createConnection()).willReturn(con);
    SingleConnectionFactory scf = new SingleConnectionFactory(cf);
    scf.setClientId("myId");
    Connection con1 = scf.createConnection();
    Connection con2 = scf.createConnection();
    con1.start();
    con2.start();
    con1.close();
    con2.close();
    // should trigger actual close
    scf.destroy();
    verify(con).setClientID("myId");
    verify(con).start();
    verify(con).stop();
    verify(con).close();
    verifyNoMoreInteractions(con);
}
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) Test(org.junit.jupiter.api.Test)

Example 39 with Connection

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

the class SingleConnectionFactoryTests method testCachingConnectionFactoryWithQueueConnectionFactoryAndJms102Usage.

@Test
public void testCachingConnectionFactoryWithQueueConnectionFactoryAndJms102Usage() throws JMSException {
    QueueConnectionFactory cf = mock(QueueConnectionFactory.class);
    QueueConnection con = mock(QueueConnection.class);
    QueueSession txSession = mock(QueueSession.class);
    QueueSession nonTxSession = mock(QueueSession.class);
    given(cf.createQueueConnection()).willReturn(con);
    given(con.createQueueSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(txSession);
    given(txSession.getTransacted()).willReturn(true);
    given(con.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession);
    CachingConnectionFactory scf = new CachingConnectionFactory(cf);
    scf.setReconnectOnException(false);
    Connection con1 = scf.createQueueConnection();
    Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE);
    session1.rollback();
    session1.close();
    session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    session1.close();
    con1.start();
    QueueConnection con2 = scf.createQueueConnection();
    Session session2 = con2.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
    session2.close();
    session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE);
    session2.getTransacted();
    // should lead to rollback
    session2.close();
    con2.start();
    con1.close();
    con2.close();
    // should trigger actual close
    scf.destroy();
    verify(txSession).rollback();
    verify(txSession).close();
    verify(nonTxSession).close();
    verify(con).start();
    verify(con).stop();
    verify(con).close();
}
Also used : QueueConnection(jakarta.jms.QueueConnection) QueueConnectionFactory(jakarta.jms.QueueConnectionFactory) QueueConnection(jakarta.jms.QueueConnection) TopicConnection(jakarta.jms.TopicConnection) Connection(jakarta.jms.Connection) QueueSession(jakarta.jms.QueueSession) QueueSession(jakarta.jms.QueueSession) Session(jakarta.jms.Session) TopicSession(jakarta.jms.TopicSession) Test(org.junit.jupiter.api.Test)

Example 40 with Connection

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

the class SingleConnectionFactoryTests method testWithQueueConnectionFactoryAndJms11Usage.

@Test
public void testWithQueueConnectionFactoryAndJms11Usage() throws JMSException {
    QueueConnectionFactory cf = mock(QueueConnectionFactory.class);
    QueueConnection con = mock(QueueConnection.class);
    given(cf.createConnection()).willReturn(con);
    SingleConnectionFactory scf = new SingleConnectionFactory(cf);
    Connection con1 = scf.createConnection();
    Connection con2 = scf.createConnection();
    con1.start();
    con2.start();
    con1.close();
    con2.close();
    // should trigger actual close
    scf.destroy();
    verify(con).start();
    verify(con).stop();
    verify(con).close();
    verifyNoMoreInteractions(con);
}
Also used : QueueConnection(jakarta.jms.QueueConnection) QueueConnectionFactory(jakarta.jms.QueueConnectionFactory) QueueConnection(jakarta.jms.QueueConnection) TopicConnection(jakarta.jms.TopicConnection) Connection(jakarta.jms.Connection) Test(org.junit.jupiter.api.Test)

Aggregations

Connection (jakarta.jms.Connection)52 Test (org.junit.jupiter.api.Test)40 ConnectionFactory (jakarta.jms.ConnectionFactory)31 Session (jakarta.jms.Session)29 JMSException (jakarta.jms.JMSException)22 QueueConnection (jakarta.jms.QueueConnection)21 TopicConnection (jakarta.jms.TopicConnection)21 Message (jakarta.jms.Message)16 QueueConnectionFactory (jakarta.jms.QueueConnectionFactory)11 TopicConnectionFactory (jakarta.jms.TopicConnectionFactory)11 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 BDDMockito.given (org.mockito.BDDMockito.given)10 Mockito.mock (org.mockito.Mockito.mock)10 Mockito.verify (org.mockito.Mockito.verify)10 StubQueue (org.springframework.jms.StubQueue)10 Destination (jakarta.jms.Destination)9 MessageProducer (jakarta.jms.MessageProducer)9 MessageConsumer (jakarta.jms.MessageConsumer)8 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)8 JmsTemplate (org.springframework.jms.core.JmsTemplate)8