Search in sources :

Example 76 with Connection

use of javax.jms.Connection in project camel by apache.

the class SjmsProducer method createSession.

protected Session createSession() throws Exception {
    ConnectionResource connectionResource = getOrCreateConnectionResource();
    Connection conn = connectionResource.borrowConnection();
    try {
        return conn.createSession(isEndpointTransacted(), getAcknowledgeMode());
    } catch (Exception e) {
        log.error("Unable to create the Session", e);
        throw e;
    } finally {
        connectionResource.returnConnection(conn);
    }
}
Also used : ConnectionResource(org.apache.camel.component.sjms.jms.ConnectionResource) Connection(javax.jms.Connection)

Example 77 with Connection

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

the class JmsHealthIndicatorTests method jmsBrokerUsesFailover.

@Test
public void jmsBrokerUsesFailover() throws JMSException {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    ConnectionMetaData connectionMetaData = mock(ConnectionMetaData.class);
    given(connectionMetaData.getJMSProviderName()).willReturn("JMS test provider");
    Connection connection = mock(Connection.class);
    given(connection.getMetaData()).willReturn(connectionMetaData);
    willThrow(new JMSException("Could not start", "123")).given(connection).start();
    given(connectionFactory.createConnection()).willReturn(connection);
    JmsHealthIndicator indicator = new JmsHealthIndicator(connectionFactory);
    Health health = indicator.health();
    assertThat(health.getStatus()).isEqualTo(Status.DOWN);
    assertThat(health.getDetails().get("provider")).isNull();
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) ConnectionMetaData(javax.jms.ConnectionMetaData) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) Test(org.junit.Test)

Example 78 with Connection

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

the class JmsHealthIndicator method doHealthCheck.

@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
    Connection connection = this.connectionFactory.createConnection();
    try {
        connection.start();
        builder.up().withDetail("provider", connection.getMetaData().getJMSProviderName());
    } finally {
        connection.close();
    }
}
Also used : Connection(javax.jms.Connection)

Example 79 with Connection

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

the class JmsTransactionManagerTests method testSuspendedTransaction.

@Test
public void testSuspendedTransaction() 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);
    given(con.createSession(false, Session.AUTO_ACKNOWLEDGE)).willReturn(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_NOT_SUPPORTED);
    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(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 80 with Connection

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

the class JmsTransactionManagerTests method testParticipatingTransactionWithRollbackOnly.

@Test
public void testParticipatingTransactionWithRollbackOnly() 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;
                }
            });
            status.setRollbackOnly();
        }
    });
    try {
        tm.commit(ts);
        fail("Should have thrown UnexpectedRollbackException");
    } catch (UnexpectedRollbackException ex) {
    // expected
    }
    verify(session).rollback();
    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) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) 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