Search in sources :

Example 86 with Connection

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

the class SimpleMessageListenerContainerTests method testContextRefreshedEventDoesNotStartTheConnectionIfAutoStartIsSetToFalse.

@Test
public void testContextRefreshedEventDoesNotStartTheConnectionIfAutoStartIsSetToFalse() 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);
    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.setAutoStartup(false);
    this.container.afterPropertiesSet();
    GenericApplicationContext context = new GenericApplicationContext();
    context.getBeanFactory().registerSingleton("messageListenerContainer", this.container);
    context.refresh();
    verify(connection).setExceptionListener(this.container);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ConnectionFactory(javax.jms.ConnectionFactory) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Connection(javax.jms.Connection) Session(javax.jms.Session) Test(org.junit.Test)

Example 87 with Connection

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

the class SimpleMessageListenerContainerTests method testTaskExecutorCorrectlyInvokedWhenSpecified.

@Test
public void testTaskExecutorCorrectlyInvokedWhenSpecified() throws Exception {
    final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer();
    final Session session = mock(Session.class);
    given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION);
    // no MessageSelector...
    given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer);
    given(session.getTransacted()).willReturn(false);
    given(session.getAcknowledgeMode()).willReturn(Session.AUTO_ACKNOWLEDGE);
    Connection connection = mock(Connection.class);
    given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session);
    final ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.createConnection()).willReturn(connection);
    final TestMessageListener listener = new TestMessageListener();
    this.container.setConnectionFactory(connectionFactory);
    this.container.setDestinationName(DESTINATION_NAME);
    this.container.setMessageListener(listener);
    this.container.setTaskExecutor(new TaskExecutor() {

        @Override
        public void execute(Runnable task) {
            listener.executorInvoked = true;
            assertFalse(listener.listenerInvoked);
            task.run();
            assertTrue(listener.listenerInvoked);
        }
    });
    this.container.afterPropertiesSet();
    this.container.start();
    final Message message = mock(Message.class);
    messageConsumer.sendMessage(message);
    assertTrue(listener.executorInvoked);
    assertTrue(listener.listenerInvoked);
    verify(connection).setExceptionListener(this.container);
    verify(connection).start();
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) TaskExecutor(org.springframework.core.task.TaskExecutor) Message(javax.jms.Message) Connection(javax.jms.Connection) Session(javax.jms.Session) Test(org.junit.Test)

Example 88 with Connection

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

the class SingleConnectionFactoryTests method testWithConnectionAggregatedStartStop.

@Test
public void testWithConnectionAggregatedStartStop() throws JMSException {
    Connection con = mock(Connection.class);
    SingleConnectionFactory scf = new SingleConnectionFactory(con);
    Connection con1 = scf.createConnection();
    con1.start();
    verify(con).start();
    con1.stop();
    verify(con).stop();
    Connection con2 = scf.createConnection();
    con2.start();
    verify(con, times(2)).start();
    con2.stop();
    verify(con, times(2)).stop();
    con2.start();
    verify(con, times(3)).start();
    con1.start();
    con2.stop();
    con1.stop();
    verify(con, times(3)).stop();
    con1.start();
    verify(con, times(4)).start();
    con1.close();
    verify(con, times(4)).stop();
    con2.close();
    scf.destroy();
    verify(con).close();
    verifyNoMoreInteractions(con);
}
Also used : Connection(javax.jms.Connection) TopicConnection(javax.jms.TopicConnection) QueueConnection(javax.jms.QueueConnection) Test(org.junit.Test)

Example 89 with Connection

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

the class SingleConnectionFactoryTests method testWithConnectionFactoryAndExceptionListener.

@Test
public void testWithConnectionFactoryAndExceptionListener() throws JMSException {
    ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    ExceptionListener listener = new ChainedExceptionListener();
    given(cf.createConnection()).willReturn(con);
    given(con.getExceptionListener()).willReturn(listener);
    SingleConnectionFactory scf = new SingleConnectionFactory(cf);
    scf.setExceptionListener(listener);
    Connection con1 = scf.createConnection();
    assertEquals(listener, con1.getExceptionListener());
    con1.start();
    con1.stop();
    con1.close();
    Connection con2 = scf.createConnection();
    con2.start();
    con2.stop();
    con2.close();
    // should trigger actual close
    scf.destroy();
    verify(con).setExceptionListener(listener);
    verify(con, times(2)).start();
    verify(con, times(2)).stop();
    verify(con).close();
}
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) ExceptionListener(javax.jms.ExceptionListener) Test(org.junit.Test)

Example 90 with Connection

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

the class SingleConnectionFactoryTests method testWithQueueConnectionFactoryAndJms11Usage.

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