Search in sources :

Example 31 with Connection

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

Example 32 with Connection

use of javax.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);
    assertSame(listener1, con1.getExceptionListener());
    con1.start();
    Connection con2 = scf.createConnection();
    con2.setExceptionListener(listener2);
    assertSame(listener2, con2.getExceptionListener());
    con.getExceptionListener().onException(new JMSException(""));
    con2.close();
    con1.getMetaData();
    con.getExceptionListener().onException(new JMSException(""));
    con1.close();
    // should trigger actual close
    scf.destroy();
    assertEquals(2, con.getStartCount());
    assertEquals(2, con.getCloseCount());
    assertEquals(2, 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)

Example 33 with Connection

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

the class JmsHealthIndicatorTests method jmsBrokerIsUp.

@Test
public void jmsBrokerIsUp() throws JMSException {
    ConnectionMetaData connectionMetaData = mock(ConnectionMetaData.class);
    given(connectionMetaData.getJMSProviderName()).willReturn("JMS test provider");
    Connection connection = mock(Connection.class);
    given(connection.getMetaData()).willReturn(connectionMetaData);
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.createConnection()).willReturn(connection);
    JmsHealthIndicator indicator = new JmsHealthIndicator(connectionFactory);
    Health health = indicator.health();
    assertThat(health.getStatus()).isEqualTo(Status.UP);
    assertThat(health.getDetails().get("provider")).isEqualTo("JMS test provider");
    verify(connection, times(1)).close();
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) ConnectionMetaData(javax.jms.ConnectionMetaData) Connection(javax.jms.Connection) Test(org.junit.Test)

Example 34 with Connection

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

the class JmsHealthIndicatorTests method jmsBrokerCouldNotRetrieveProviderMetadata.

@Test
public void jmsBrokerCouldNotRetrieveProviderMetadata() throws JMSException {
    ConnectionMetaData connectionMetaData = mock(ConnectionMetaData.class);
    given(connectionMetaData.getJMSProviderName()).willThrow(new JMSException("test", "123"));
    Connection connection = mock(Connection.class);
    given(connection.getMetaData()).willReturn(connectionMetaData);
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    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();
    verify(connection, times(1)).close();
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) ConnectionMetaData(javax.jms.ConnectionMetaData) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) Test(org.junit.Test)

Example 35 with Connection

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

the class SjmsConsumer method createConsumer.

/**
     * Creates a {@link MessageConsumerResources} with a dedicated
     * {@link Session} required for transacted and InOut consumers.
     */
private MessageConsumerResources createConsumer() throws Exception {
    MessageConsumerResources answer;
    ConnectionResource connectionResource = getOrCreateConnectionResource();
    Connection conn = connectionResource.borrowConnection();
    try {
        Session session = conn.createSession(isTransacted(), isTransacted() ? Session.SESSION_TRANSACTED : Session.AUTO_ACKNOWLEDGE);
        MessageConsumer messageConsumer = getEndpoint().getJmsObjectFactory().createMessageConsumer(session, getEndpoint());
        MessageListener handler = createMessageHandler(session);
        messageConsumer.setMessageListener(handler);
        answer = new MessageConsumerResources(session, messageConsumer);
    } catch (Exception e) {
        log.error("Unable to create the MessageConsumer", e);
        throw e;
    } finally {
        connectionResource.returnConnection(conn);
    }
    return answer;
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ConnectionResource(org.apache.camel.component.sjms.jms.ConnectionResource) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) Session(javax.jms.Session)

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