Search in sources :

Example 46 with ConnectionFactory

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

the class SingleConnectionFactoryTests method testWithConnectionFactoryAndReconnectOnException.

@Test
public void testWithConnectionFactoryAndReconnectOnException() throws JMSException {
    ConnectionFactory cf = mock(ConnectionFactory.class);
    TestConnection con = new TestConnection();
    given(cf.createConnection()).willReturn(con);
    SingleConnectionFactory scf = new SingleConnectionFactory(cf);
    scf.setReconnectOnException(true);
    Connection con1 = scf.createConnection();
    assertThat(con1.getExceptionListener()).isNull();
    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);
}
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 47 with ConnectionFactory

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

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

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

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

the class JmsTransactionManager method obtainConnectionFactory.

/**
 * Obtain the ConnectionFactory for actual use.
 * @return the ConnectionFactory (never {@code null})
 * @throws IllegalStateException in case of no ConnectionFactory set
 * @since 5.0
 */
protected final ConnectionFactory obtainConnectionFactory() {
    ConnectionFactory connectionFactory = getConnectionFactory();
    Assert.state(connectionFactory != null, "No ConnectionFactory set");
    return connectionFactory;
}
Also used : ConnectionFactory(jakarta.jms.ConnectionFactory)

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