Search in sources :

Example 66 with Session

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

the class SimpleMessageListenerContainerTests method testNoRollbackOccursIfSessionIsNotTransactedAndThatExceptionsDo_NOT_Propagate.

@Test
public void testNoRollbackOccursIfSessionIsNotTransactedAndThatExceptionsDo_NOT_Propagate() throws Exception {
    final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer();
    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);
    // an exception is thrown, so the rollback logic is being applied here...
    given(session.getTransacted()).willReturn(false);
    Connection connection = mock(Connection.class);
    // session gets created in order to register MessageListener...
    given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session);
    // and the connection is start()ed after the listener is registered...
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.createConnection()).willReturn(connection);
    this.container.setConnectionFactory(connectionFactory);
    this.container.setDestinationName(DESTINATION_NAME);
    this.container.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            throw new UnsupportedOperationException();
        }
    });
    this.container.afterPropertiesSet();
    this.container.start();
    // manually trigger an Exception with the above bad MessageListener...
    final Message message = mock(Message.class);
    // a Throwable from a MessageListener MUST simply be swallowed...
    messageConsumer.sendMessage(message);
    verify(connection).setExceptionListener(this.container);
    verify(connection).start();
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) Session(javax.jms.Session) Test(org.junit.Test)

Example 67 with Session

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

the class JmsResponseTests method resolveDestinationForQueue.

@Test
public void resolveDestinationForQueue() throws JMSException {
    Session session = mock(Session.class);
    DestinationResolver destinationResolver = mock(DestinationResolver.class);
    Destination destination = mock(Destination.class);
    given(destinationResolver.resolveDestinationName(session, "myQueue", false)).willReturn(destination);
    JmsResponse<String> jmsResponse = JmsResponse.forQueue("foo", "myQueue");
    Destination actual = jmsResponse.resolveDestination(destinationResolver, session);
    assertSame(destination, actual);
}
Also used : Destination(javax.jms.Destination) DestinationResolver(org.springframework.jms.support.destination.DestinationResolver) Session(javax.jms.Session) Test(org.junit.Test)

Example 68 with Session

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

the class JmsTemplateTests method testProducerCallbackWithIdAndTimestampDisabled.

@Test
public void testProducerCallbackWithIdAndTimestampDisabled() throws Exception {
    JmsTemplate template = createTemplate();
    template.setConnectionFactory(connectionFactory);
    template.setMessageIdEnabled(false);
    template.setMessageTimestampEnabled(false);
    MessageProducer messageProducer = mock(MessageProducer.class);
    given(session.createProducer(null)).willReturn(messageProducer);
    given(messageProducer.getPriority()).willReturn(4);
    template.execute(new ProducerCallback<Void>() {

        @Override
        public Void doInJms(Session session, MessageProducer producer) throws JMSException {
            session.getTransacted();
            producer.getPriority();
            return null;
        }
    });
    verify(messageProducer).setDisableMessageID(true);
    verify(messageProducer).setDisableMessageTimestamp(true);
    verify(messageProducer).close();
    verify(session).close();
    verify(connection).close();
}
Also used : JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) Test(org.junit.Test)

Example 69 with Session

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

the class JmsTemplateTests method doTestSendDestination.

/**
	 * Common method for testing a send method that uses the MessageCreator
	 * callback but with different QOS options.
	 * @param ignoreQOS test using default QOS options.
	 */
private void doTestSendDestination(boolean explicitDestination, boolean useDefaultDestination, boolean ignoreQOS, boolean disableIdAndTimestamp) throws Exception {
    JmsTemplate template = createTemplate();
    template.setConnectionFactory(connectionFactory);
    String destinationName = "testDestination";
    if (useDefaultDestination) {
        if (explicitDestination) {
            template.setDefaultDestination(queue);
        } else {
            template.setDefaultDestinationName(destinationName);
        }
    }
    if (disableIdAndTimestamp) {
        template.setMessageIdEnabled(false);
        template.setMessageTimestampEnabled(false);
    }
    MessageProducer messageProducer = mock(MessageProducer.class);
    TextMessage textMessage = mock(TextMessage.class);
    given(session.createProducer(queue)).willReturn(messageProducer);
    given(session.createTextMessage("just testing")).willReturn(textMessage);
    if (!ignoreQOS) {
        template.setExplicitQosEnabled(true);
        template.setDeliveryMode(deliveryMode);
        template.setPriority(priority);
        template.setTimeToLive(timeToLive);
    }
    if (useDefaultDestination) {
        template.send(new MessageCreator() {

            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage("just testing");
            }
        });
    } else {
        if (explicitDestination) {
            template.send(queue, new MessageCreator() {

                @Override
                public Message createMessage(Session session) throws JMSException {
                    return session.createTextMessage("just testing");
                }
            });
        } else {
            template.send(destinationName, new MessageCreator() {

                @Override
                public Message createMessage(Session session) throws JMSException {
                    return session.createTextMessage("just testing");
                }
            });
        }
    }
    if (useTransactedTemplate()) {
        verify(session).commit();
    }
    if (disableIdAndTimestamp) {
        verify(messageProducer).setDisableMessageID(true);
        verify(messageProducer).setDisableMessageTimestamp(true);
    }
    if (ignoreQOS) {
        verify(messageProducer).send(textMessage);
    } else {
        verify(messageProducer).send(textMessage, deliveryMode, priority, timeToLive);
    }
    verify(messageProducer).close();
    verify(session).close();
    verify(connection).close();
}
Also used : Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 70 with Session

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

the class JmsTemplateTests method testProducerCallback.

@Test
public void testProducerCallback() throws Exception {
    JmsTemplate template = createTemplate();
    template.setConnectionFactory(connectionFactory);
    MessageProducer messageProducer = mock(MessageProducer.class);
    given(session.createProducer(null)).willReturn(messageProducer);
    given(messageProducer.getPriority()).willReturn(4);
    template.execute(new ProducerCallback<Void>() {

        @Override
        public Void doInJms(Session session, MessageProducer producer) throws JMSException {
            session.getTransacted();
            producer.getPriority();
            return null;
        }
    });
    verify(messageProducer).close();
    verify(session).close();
    verify(connection).close();
}
Also used : JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

Session (javax.jms.Session)281 MessageProducer (javax.jms.MessageProducer)131 Connection (javax.jms.Connection)120 Test (org.junit.Test)110 Message (javax.jms.Message)86 TextMessage (javax.jms.TextMessage)84 JMSException (javax.jms.JMSException)80 MessageConsumer (javax.jms.MessageConsumer)64 Destination (javax.jms.Destination)46 Topic (javax.jms.Topic)40 ConnectionFactory (javax.jms.ConnectionFactory)37 ObjectMessage (javax.jms.ObjectMessage)28 Queue (javax.jms.Queue)26 StubTextMessage (org.springframework.jms.StubTextMessage)20 QueueSession (javax.jms.QueueSession)16 MessagingMessageListenerAdapter (org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter)16 MessageCreator (org.springframework.jms.core.MessageCreator)15 BytesMessage (javax.jms.BytesMessage)14 MapMessage (javax.jms.MapMessage)12 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)12