Search in sources :

Example 16 with Connection

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

the class SingleConnectionFactory method createQueueConnection.

@Override
public QueueConnection createQueueConnection() throws JMSException {
    Connection con;
    synchronized (this.connectionMonitor) {
        this.pubSubMode = Boolean.FALSE;
        con = createConnection();
    }
    if (!(con instanceof QueueConnection)) {
        throw new javax.jms.IllegalStateException("This SingleConnectionFactory does not hold a QueueConnection but rather: " + con);
    }
    return ((QueueConnection) con);
}
Also used : QueueConnection(javax.jms.QueueConnection) Connection(javax.jms.Connection) TopicConnection(javax.jms.TopicConnection) QueueConnection(javax.jms.QueueConnection)

Example 17 with Connection

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

the class JmsTemplate method executeLocal.

/**
	 * A variant of {@link #execute(SessionCallback, boolean)} that explicitly
	 * creates a non-transactional {@link Session}. The given {@link SessionCallback}
	 * does not participate in an existing transaction.
	 */
private <T> T executeLocal(SessionCallback<T> action, boolean startConnection) throws JmsException {
    Assert.notNull(action, "Callback object must not be null");
    Connection con = null;
    Session session = null;
    try {
        con = getConnectionFactory().createConnection();
        session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
        if (startConnection) {
            con.start();
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Executing callback on JMS Session: " + session);
        }
        return action.doInJms(session);
    } catch (JMSException ex) {
        throw convertJmsAccessException(ex);
    } finally {
        JmsUtils.closeSession(session);
        ConnectionFactoryUtils.releaseConnection(con, getConnectionFactory(), startConnection);
    }
}
Also used : Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) Session(javax.jms.Session)

Example 18 with Connection

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

the class JmsTransactionManagerTests method testTransactionCommit.

@Test
public void testTransactionCommit() throws JMSException {
    ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    final Session session = mock(Session.class);
    given(cf.createConnection()).willReturn(con);
    given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
    JmsTransactionManager tm = new JmsTransactionManager(cf);
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    JmsTemplate jt = new JmsTemplate(cf);
    jt.execute(new SessionCallback<Void>() {

        @Override
        public Void doInJms(Session sess) {
            assertTrue(sess == session);
            return null;
        }
    });
    tm.commit(ts);
    verify(session).commit();
    verify(session).close();
    verify(con).close();
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Connection(javax.jms.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) JmsTemplate(org.springframework.jms.core.JmsTemplate) Session(javax.jms.Session) Test(org.junit.Test)

Example 19 with Connection

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

the class JmsTransactionManagerTests method testTransactionSuspension.

@Test
public void testTransactionSuspension() throws JMSException {
    final ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    final Session session = mock(Session.class);
    final Session session2 = mock(Session.class);
    given(cf.createConnection()).willReturn(con);
    given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session, session2);
    JmsTransactionManager tm = new JmsTransactionManager(cf);
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    final JmsTemplate jt = new JmsTemplate(cf);
    jt.execute(new SessionCallback<Void>() {

        @Override
        public Void doInJms(Session sess) {
            assertTrue(sess == session);
            return null;
        }
    });
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            jt.execute(new SessionCallback<Void>() {

                @Override
                public Void doInJms(Session sess) {
                    assertTrue(sess != session);
                    return null;
                }
            });
        }
    });
    jt.execute(new SessionCallback<Void>() {

        @Override
        public Void doInJms(Session sess) {
            assertTrue(sess == session);
            return null;
        }
    });
    tm.commit(ts);
    verify(session).commit();
    verify(session2).commit();
    verify(session).close();
    verify(session2).close();
    verify(con, times(2)).close();
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Connection(javax.jms.Connection) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) SessionCallback(org.springframework.jms.core.SessionCallback) ConnectionFactory(javax.jms.ConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Session(javax.jms.Session) Test(org.junit.Test)

Example 20 with Connection

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

the class JmsTransactionManagerTests method testParticipatingTransactionWithCommit.

@Test
public void testParticipatingTransactionWithCommit() throws JMSException {
    ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    final Session session = mock(Session.class);
    given(cf.createConnection()).willReturn(con);
    given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
    JmsTransactionManager tm = new JmsTransactionManager(cf);
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    final JmsTemplate jt = new JmsTemplate(cf);
    jt.execute(new SessionCallback<Void>() {

        @Override
        public Void doInJms(Session sess) {
            assertTrue(sess == session);
            return null;
        }
    });
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            jt.execute(new SessionCallback<Void>() {

                @Override
                public Void doInJms(Session sess) {
                    assertTrue(sess == session);
                    return null;
                }
            });
        }
    });
    tm.commit(ts);
    verify(session).commit();
    verify(session).close();
    verify(con).close();
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Connection(javax.jms.Connection) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) SessionCallback(org.springframework.jms.core.SessionCallback) ConnectionFactory(javax.jms.ConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Session(javax.jms.Session) 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