Search in sources :

Example 16 with Connection

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

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

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

the class SingleConnectionFactoryTests method testWithConnection.

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

Example 19 with Connection

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

the class SingleConnectionFactoryTests method testCachingConnectionFactoryWithTopicConnectionFactoryAndJms102Usage.

@Test
public void testCachingConnectionFactoryWithTopicConnectionFactoryAndJms102Usage() throws JMSException {
    TopicConnectionFactory cf = mock(TopicConnectionFactory.class);
    TopicConnection con = mock(TopicConnection.class);
    TopicSession txSession = mock(TopicSession.class);
    TopicSession nonTxSession = mock(TopicSession.class);
    given(cf.createTopicConnection()).willReturn(con);
    given(con.createTopicSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(txSession);
    given(txSession.getTransacted()).willReturn(true);
    given(con.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession);
    CachingConnectionFactory scf = new CachingConnectionFactory(cf);
    scf.setReconnectOnException(false);
    Connection con1 = scf.createTopicConnection();
    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();
    TopicConnection con2 = scf.createTopicConnection();
    Session session2 = con2.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE);
    session2.close();
    session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE);
    session2.getTransacted();
    session2.close();
    con2.start();
    con1.close();
    con2.close();
    // should trigger actual close
    scf.destroy();
    verify(txSession).close();
    verify(nonTxSession).close();
    verify(con).start();
    verify(con).stop();
    verify(con).close();
}
Also used : TopicConnectionFactory(jakarta.jms.TopicConnectionFactory) TopicSession(jakarta.jms.TopicSession) QueueConnection(jakarta.jms.QueueConnection) TopicConnection(jakarta.jms.TopicConnection) Connection(jakarta.jms.Connection) TopicConnection(jakarta.jms.TopicConnection) QueueSession(jakarta.jms.QueueSession) Session(jakarta.jms.Session) TopicSession(jakarta.jms.TopicSession) Test(org.junit.jupiter.api.Test)

Example 20 with Connection

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

the class SimpleMessageListenerContainerTests method testContextRefreshedEventStartsTheConnectionByDefault.

@Test
public void testContextRefreshedEventStartsTheConnectionByDefault() throws Exception {
    MessageConsumer messageConsumer = mock(MessageConsumer.class);
    Session session = mock(Session.class);
    // Queue gets created in order to create MessageConsumer for that Destination...
    given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION);
    // and then the MessageConsumer gets created...
    // no MessageSelector...
    given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer);
    Connection connection = mock(Connection.class);
    // session gets created in order to register MessageListener...
    given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session);
    // and the connection is start()ed after the listener is registered...
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.createConnection()).willReturn(connection);
    this.container.setConnectionFactory(connectionFactory);
    this.container.setDestinationName(DESTINATION_NAME);
    this.container.setMessageListener(new TestMessageListener());
    this.container.afterPropertiesSet();
    GenericApplicationContext context = new GenericApplicationContext();
    context.getBeanFactory().registerSingleton("messageListenerContainer", this.container);
    context.refresh();
    context.close();
    verify(connection).setExceptionListener(this.container);
    verify(connection).start();
}
Also used : MessageConsumer(jakarta.jms.MessageConsumer) ConnectionFactory(jakarta.jms.ConnectionFactory) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Connection(jakarta.jms.Connection) Session(jakarta.jms.Session) 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