Search in sources :

Example 81 with Message

use of javax.jms.Message 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 82 with Message

use of javax.jms.Message 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 83 with Message

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

the class JmsTemplateTests method doTestSendAndReceive.

private void doTestSendAndReceive(boolean explicitDestination, boolean useDefaultDestination, long timeout) throws Exception {
    JmsTemplate template = createTemplate();
    template.setConnectionFactory(connectionFactory);
    String destinationName = "testDestination";
    if (useDefaultDestination) {
        if (explicitDestination) {
            template.setDefaultDestination(queue);
        } else {
            template.setDefaultDestinationName(destinationName);
        }
    }
    template.setReceiveTimeout(timeout);
    Session localSession = getLocalSession();
    TemporaryQueue replyDestination = mock(TemporaryQueue.class);
    MessageProducer messageProducer = mock(MessageProducer.class);
    given(localSession.createProducer(queue)).willReturn(messageProducer);
    given(localSession.createTemporaryQueue()).willReturn(replyDestination);
    MessageConsumer messageConsumer = mock(MessageConsumer.class);
    given(localSession.createConsumer(replyDestination)).willReturn(messageConsumer);
    TextMessage request = mock(TextMessage.class);
    MessageCreator messageCreator = mock(MessageCreator.class);
    given(messageCreator.createMessage(localSession)).willReturn(request);
    TextMessage reply = mock(TextMessage.class);
    if (timeout == JmsTemplate.RECEIVE_TIMEOUT_NO_WAIT) {
        given(messageConsumer.receiveNoWait()).willReturn(reply);
    } else if (timeout == JmsTemplate.RECEIVE_TIMEOUT_INDEFINITE_WAIT) {
        given(messageConsumer.receive()).willReturn(reply);
    } else {
        given(messageConsumer.receive(timeout)).willReturn(reply);
    }
    Message message = null;
    if (useDefaultDestination) {
        message = template.sendAndReceive(messageCreator);
    } else if (explicitDestination) {
        message = template.sendAndReceive(queue, messageCreator);
    } else {
        message = template.sendAndReceive(destinationName, messageCreator);
    }
    // replyTO set on the request
    verify(request).setJMSReplyTo(replyDestination);
    assertSame("Reply message not received", reply, message);
    verify(connection).start();
    verify(connection).close();
    verify(localSession).close();
    verify(messageConsumer).close();
    verify(messageProducer).close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 84 with Message

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

the class MessageListenerAdapterTests method testWithResponsiveMessageDelegateWhenReturnTypeIsAJMSMessageAndNoMessageConverterIsSupplied.

@Test
public void testWithResponsiveMessageDelegateWhenReturnTypeIsAJMSMessageAndNoMessageConverterIsSupplied() throws Exception {
    Queue destination = mock(Queue.class);
    final TextMessage sentTextMessage = mock(TextMessage.class);
    // correlation ID is queried when response is being created...
    given(sentTextMessage.getJMSCorrelationID()).willReturn(CORRELATION_ID);
    // Reply-To is queried when response is being created...
    given(sentTextMessage.getJMSReplyTo()).willReturn(destination);
    TextMessage responseMessage = mock(TextMessage.class);
    QueueSender queueSender = mock(QueueSender.class);
    Session session = mock(Session.class);
    given(session.createProducer(destination)).willReturn(queueSender);
    ResponsiveJmsTextMessageReturningMessageDelegate delegate = mock(ResponsiveJmsTextMessageReturningMessageDelegate.class);
    given(delegate.handleMessage(sentTextMessage)).willReturn(responseMessage);
    final MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {

        @Override
        protected Object extractMessage(Message message) {
            return message;
        }
    };
    adapter.setMessageConverter(null);
    adapter.onMessage(sentTextMessage, session);
    verify(responseMessage).setJMSCorrelationID(CORRELATION_ID);
    verify(queueSender).send(responseMessage);
    verify(queueSender).close();
}
Also used : TextMessage(javax.jms.TextMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) QueueSender(javax.jms.QueueSender) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) QueueSession(javax.jms.QueueSession) Session(javax.jms.Session) Test(org.junit.Test)

Example 85 with Message

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

the class MessageListenerAdapterTests method testWithResponsiveMessageDelegateNoDefaultDestination_SendsReturnTextMessageWhenSessionSupplied.

@Test
public void testWithResponsiveMessageDelegateNoDefaultDestination_SendsReturnTextMessageWhenSessionSupplied() throws Exception {
    Queue destination = mock(Queue.class);
    TextMessage sentTextMessage = mock(TextMessage.class);
    // correlation ID is queried when response is being created...
    given(sentTextMessage.getJMSCorrelationID()).willReturn(null);
    given(sentTextMessage.getJMSMessageID()).willReturn(CORRELATION_ID);
    // Reply-To is queried when response is being created...
    given(sentTextMessage.getJMSReplyTo()).willReturn(destination);
    TextMessage responseTextMessage = mock(TextMessage.class);
    MessageProducer messageProducer = mock(MessageProducer.class);
    Session session = mock(Session.class);
    given(session.createTextMessage(RESPONSE_TEXT)).willReturn(responseTextMessage);
    given(session.createProducer(destination)).willReturn(messageProducer);
    ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class);
    given(delegate.handleMessage(sentTextMessage)).willReturn(RESPONSE_TEXT);
    MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {

        @Override
        protected Object extractMessage(Message message) {
            return message;
        }
    };
    adapter.onMessage(sentTextMessage, session);
    verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID);
    verify(messageProducer).send(responseTextMessage);
    verify(messageProducer).close();
    verify(delegate).handleMessage(sentTextMessage);
}
Also used : TextMessage(javax.jms.TextMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) QueueSession(javax.jms.QueueSession) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

Message (javax.jms.Message)230 TextMessage (javax.jms.TextMessage)103 Test (org.junit.Test)91 Session (javax.jms.Session)86 JMSException (javax.jms.JMSException)77 MessageProducer (javax.jms.MessageProducer)64 ObjectMessage (javax.jms.ObjectMessage)52 MessageConsumer (javax.jms.MessageConsumer)50 Connection (javax.jms.Connection)45 BytesMessage (javax.jms.BytesMessage)37 Destination (javax.jms.Destination)30 MapMessage (javax.jms.MapMessage)26 Queue (javax.jms.Queue)20 ConnectionFactory (javax.jms.ConnectionFactory)18 QueueSession (javax.jms.QueueSession)17 MessageCreator (org.springframework.jms.core.MessageCreator)15 MessageListener (javax.jms.MessageListener)12 Map (java.util.Map)10 StreamMessage (javax.jms.StreamMessage)10 TemporaryQueue (javax.jms.TemporaryQueue)9