Search in sources :

Example 26 with Connection

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

the class JmsTemplateTests method testSessionCallbackWithinSynchronizedTransaction.

@Test
public void testSessionCallbackWithinSynchronizedTransaction() throws Exception {
    SingleConnectionFactory scf = new SingleConnectionFactory(connectionFactory);
    JmsTemplate template = createTemplate();
    template.setConnectionFactory(scf);
    TransactionSynchronizationManager.initSynchronization();
    try {
        template.execute(new SessionCallback<Void>() {

            @Override
            public Void doInJms(Session session) throws JMSException {
                session.getTransacted();
                return null;
            }
        });
        template.execute(new SessionCallback<Void>() {

            @Override
            public Void doInJms(Session session) throws JMSException {
                session.getTransacted();
                return null;
            }
        });
        assertSame(session, ConnectionFactoryUtils.getTransactionalSession(scf, null, false));
        assertSame(session, ConnectionFactoryUtils.getTransactionalSession(scf, scf.createConnection(), false));
        TransactionAwareConnectionFactoryProxy tacf = new TransactionAwareConnectionFactoryProxy(scf);
        Connection tac = tacf.createConnection();
        Session tas = tac.createSession(false, Session.AUTO_ACKNOWLEDGE);
        tas.getTransacted();
        tas.close();
        tac.close();
        List<TransactionSynchronization> synchs = TransactionSynchronizationManager.getSynchronizations();
        assertEquals(1, synchs.size());
        TransactionSynchronization synch = synchs.get(0);
        synch.beforeCommit(false);
        synch.beforeCompletion();
        synch.afterCommit();
        synch.afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
    } finally {
        TransactionSynchronizationManager.clearSynchronization();
        scf.destroy();
    }
    assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
    verify(connection).start();
    if (useTransactedTemplate()) {
        verify(session).commit();
    }
    verify(session).close();
    verify(connection).stop();
    verify(connection).close();
}
Also used : TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) SingleConnectionFactory(org.springframework.jms.connection.SingleConnectionFactory) Connection(javax.jms.Connection) TransactionAwareConnectionFactoryProxy(org.springframework.jms.connection.TransactionAwareConnectionFactoryProxy) JMSException(javax.jms.JMSException) Session(javax.jms.Session) Test(org.junit.Test)

Example 27 with Connection

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

the class SingleConnectionFactoryTests method testWithTopicConnectionFactoryAndJms11Usage.

@Test
public void testWithTopicConnectionFactoryAndJms11Usage() throws JMSException {
    TopicConnectionFactory cf = mock(TopicConnectionFactory.class);
    TopicConnection con = mock(TopicConnection.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 : TopicConnectionFactory(javax.jms.TopicConnectionFactory) Connection(javax.jms.Connection) TopicConnection(javax.jms.TopicConnection) QueueConnection(javax.jms.QueueConnection) TopicConnection(javax.jms.TopicConnection) Test(org.junit.Test)

Example 28 with Connection

use of javax.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(javax.jms.TopicConnectionFactory) TopicSession(javax.jms.TopicSession) Connection(javax.jms.Connection) TopicConnection(javax.jms.TopicConnection) QueueConnection(javax.jms.QueueConnection) TopicConnection(javax.jms.TopicConnection) QueueSession(javax.jms.QueueSession) Session(javax.jms.Session) TopicSession(javax.jms.TopicSession) Test(org.junit.Test)

Example 29 with Connection

use of javax.jms.Connection 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();
    assertSame(listener, con1.getExceptionListener());
    con1.start();
    con.getExceptionListener().onException(new JMSException(""));
    Connection con2 = scf.createConnection();
    con2.start();
    // should trigger actual close
    scf.destroy();
    assertEquals(2, con.getStartCount());
    assertEquals(2, con.getCloseCount());
    assertEquals(1, listener.getCount());
}
Also used : QueueConnectionFactory(javax.jms.QueueConnectionFactory) TopicConnectionFactory(javax.jms.TopicConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) Connection(javax.jms.Connection) TopicConnection(javax.jms.TopicConnection) QueueConnection(javax.jms.QueueConnection) JMSException(javax.jms.JMSException) Test(org.junit.Test)

Example 30 with Connection

use of javax.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);
    assertSame(listener1, con1.getExceptionListener());
    Connection con2 = scf.createConnection();
    con2.setExceptionListener(listener2);
    assertSame(listener2, con2.getExceptionListener());
    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();
    assertEquals(0, con.getStartCount());
    assertEquals(1, con.getCloseCount());
    assertEquals(3, listener0.getCount());
    assertEquals(2, listener1.getCount());
    assertEquals(1, listener2.getCount());
}
Also used : QueueConnectionFactory(javax.jms.QueueConnectionFactory) TopicConnectionFactory(javax.jms.TopicConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) Connection(javax.jms.Connection) TopicConnection(javax.jms.TopicConnection) QueueConnection(javax.jms.QueueConnection) JMSException(javax.jms.JMSException) Test(org.junit.Test)

Aggregations

Connection (javax.jms.Connection)124 Session (javax.jms.Session)88 Test (org.junit.Test)62 MessageProducer (javax.jms.MessageProducer)46 ConnectionFactory (javax.jms.ConnectionFactory)44 Message (javax.jms.Message)38 MessageConsumer (javax.jms.MessageConsumer)38 JMSException (javax.jms.JMSException)32 TextMessage (javax.jms.TextMessage)32 QueueConnection (javax.jms.QueueConnection)22 TopicConnection (javax.jms.TopicConnection)22 Destination (javax.jms.Destination)15 Queue (javax.jms.Queue)12 QueueConnectionFactory (javax.jms.QueueConnectionFactory)11 TopicConnectionFactory (javax.jms.TopicConnectionFactory)11 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)7 JmsTemplate (org.springframework.jms.core.JmsTemplate)7 TransactionStatus (org.springframework.transaction.TransactionStatus)7 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)7 TransactionAttribute (javax.ejb.TransactionAttribute)4