Search in sources :

Example 16 with TextMessage

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

the class MethodJmsListenerEndpointTests method processAndReplyWithSendTo.

private void processAndReplyWithSendTo(MessagingMessageListenerAdapter listener, String replyDestinationName, boolean pubSubDomain) throws JMSException {
    String body = "echo text";
    String correlationId = "link-1234";
    Destination replyDestination = new Destination() {
    };
    DestinationResolver destinationResolver = mock(DestinationResolver.class);
    TextMessage reply = mock(TextMessage.class);
    QueueSender queueSender = mock(QueueSender.class);
    Session session = mock(Session.class);
    given(destinationResolver.resolveDestinationName(session, replyDestinationName, pubSubDomain)).willReturn(replyDestination);
    given(session.createTextMessage(body)).willReturn(reply);
    given(session.createProducer(replyDestination)).willReturn(queueSender);
    listener.setDestinationResolver(destinationResolver);
    StubTextMessage inputMessage = createSimpleJmsTextMessage(body);
    inputMessage.setJMSCorrelationID(correlationId);
    listener.onMessage(inputMessage, session);
    verify(destinationResolver).resolveDestinationName(session, replyDestinationName, pubSubDomain);
    verify(reply).setJMSCorrelationID(correlationId);
    verify(queueSender).send(reply);
    verify(queueSender).close();
}
Also used : Destination(javax.jms.Destination) DestinationResolver(org.springframework.jms.support.destination.DestinationResolver) QueueSender(javax.jms.QueueSender) StubTextMessage(org.springframework.jms.StubTextMessage) StubTextMessage(org.springframework.jms.StubTextMessage) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 17 with TextMessage

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

the class JmsMessagingTemplateTests method convertAndSendDefaultDestination.

@Test
public void convertAndSendDefaultDestination() throws JMSException {
    Destination destination = new Destination() {
    };
    messagingTemplate.setDefaultDestination(destination);
    messagingTemplate.convertAndSend("my Payload");
    verify(jmsTemplate).send(eq(destination), messageCreator.capture());
    TextMessage textMessage = createTextMessage(messageCreator.getValue());
    assertEquals("my Payload", textMessage.getText());
}
Also used : Destination(javax.jms.Destination) StubTextMessage(org.springframework.jms.StubTextMessage) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 18 with TextMessage

use of javax.jms.TextMessage 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 19 with TextMessage

use of javax.jms.TextMessage 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 20 with TextMessage

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

the class MessageListenerAdapterTests method testWithMessageContentsDelegateForTextMessage.

@Test
public void testWithMessageContentsDelegateForTextMessage() throws Exception {
    TextMessage textMessage = mock(TextMessage.class);
    // TextMessage contents must be unwrapped...
    given(textMessage.getText()).willReturn(TEXT);
    MessageContentsDelegate delegate = mock(MessageContentsDelegate.class);
    MessageListenerAdapter adapter = new MessageListenerAdapter(delegate);
    adapter.onMessage(textMessage);
    verify(delegate).handleMessage(TEXT);
}
Also used : TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Aggregations

TextMessage (javax.jms.TextMessage)191 Test (org.junit.Test)88 Message (javax.jms.Message)63 Session (javax.jms.Session)62 MessageProducer (javax.jms.MessageProducer)59 JMSException (javax.jms.JMSException)47 MessageConsumer (javax.jms.MessageConsumer)37 Destination (javax.jms.Destination)36 Connection (javax.jms.Connection)31 ObjectMessage (javax.jms.ObjectMessage)22 Queue (javax.jms.Queue)21 QueueSession (javax.jms.QueueSession)18 StubTextMessage (org.springframework.jms.StubTextMessage)18 BytesMessage (javax.jms.BytesMessage)17 ConnectionFactory (javax.jms.ConnectionFactory)11 QueueConnection (javax.jms.QueueConnection)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 JMSContext (javax.jms.JMSContext)9 MapMessage (javax.jms.MapMessage)9 NamingException (javax.naming.NamingException)8