Search in sources :

Example 41 with Connection

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

the class SingleConnectionFactoryTests method testWithConnectionAggregatedStartStop.

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

Example 42 with Connection

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

the class SingleConnectionFactoryTests method testWithTopicConnection.

@Test
public void testWithTopicConnection() throws JMSException {
    Connection con = mock(TopicConnection.class);
    SingleConnectionFactory scf = new SingleConnectionFactory(con);
    TopicConnection con1 = scf.createTopicConnection();
    con1.start();
    con1.stop();
    con1.close();
    TopicConnection con2 = scf.createTopicConnection();
    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) TopicConnection(jakarta.jms.TopicConnection) Connection(jakarta.jms.Connection) TopicConnection(jakarta.jms.TopicConnection) Test(org.junit.jupiter.api.Test)

Example 43 with Connection

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

the class SingleConnectionFactoryTests method testCachingConnectionFactory.

@Test
public void testCachingConnectionFactory() throws JMSException {
    ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    Session txSession = mock(Session.class);
    Session nonTxSession = mock(Session.class);
    given(cf.createConnection()).willReturn(con);
    given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(txSession);
    given(txSession.getTransacted()).willReturn(true);
    given(con.createSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession);
    CachingConnectionFactory scf = new CachingConnectionFactory(cf);
    scf.setReconnectOnException(false);
    Connection con1 = scf.createConnection();
    Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE);
    session1.getTransacted();
    // should lead to rollback
    session1.close();
    session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    session1.close();
    con1.start();
    Connection con2 = scf.createConnection();
    Session session2 = con2.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    session2.close();
    session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE);
    session2.commit();
    session2.close();
    con2.start();
    con1.close();
    con2.close();
    // should trigger actual close
    scf.destroy();
    verify(txSession).commit();
    verify(txSession).close();
    verify(nonTxSession).close();
    verify(con).start();
    verify(con).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) QueueSession(jakarta.jms.QueueSession) Session(jakarta.jms.Session) TopicSession(jakarta.jms.TopicSession) Test(org.junit.jupiter.api.Test)

Example 44 with Connection

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

the class SingleConnectionFactoryTests method testWithQueueConnectionFactoryAndJms102Usage.

@Test
public void testWithQueueConnectionFactoryAndJms102Usage() throws JMSException {
    QueueConnectionFactory cf = mock(QueueConnectionFactory.class);
    QueueConnection con = mock(QueueConnection.class);
    given(cf.createQueueConnection()).willReturn(con);
    SingleConnectionFactory scf = new SingleConnectionFactory(cf);
    Connection con1 = scf.createQueueConnection();
    Connection con2 = scf.createQueueConnection();
    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)

Example 45 with Connection

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

the class JmsResourceHolder method closeAll.

/**
 * Close all of this resource holder's Sessions and clear its state.
 * @see Session#close()
 */
public void closeAll() {
    for (Session session : this.sessions) {
        try {
            session.close();
        } catch (Throwable ex) {
            logger.debug("Could not close synchronized JMS Session after transaction", ex);
        }
    }
    for (Connection con : this.connections) {
        ConnectionFactoryUtils.releaseConnection(con, this.connectionFactory, true);
    }
    this.connections.clear();
    this.sessions.clear();
    this.sessionsPerConnection.clear();
}
Also used : Connection(jakarta.jms.Connection) Session(jakarta.jms.Session)

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