Search in sources :

Example 16 with MessageConsumer

use of javax.jms.MessageConsumer 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 17 with MessageConsumer

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

the class InOutQueueProducerTest method testInOutQueueProducer.

@Test
public void testInOutQueueProducer() throws Exception {
    MessageConsumer mc = createQueueConsumer(TEST_DESTINATION_NAME + ".request");
    assertNotNull(mc);
    final String requestText = "Hello World!";
    final String responseText = "How are you";
    mc.setMessageListener(new MyMessageListener(requestText, responseText));
    Object responseObject = template.requestBody("direct:start", requestText);
    assertNotNull(responseObject);
    assertTrue(responseObject instanceof String);
    assertEquals(responseText, responseObject);
    mc.close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Test(org.junit.Test)

Example 18 with MessageConsumer

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

the class InOutTempQueueProducerTest method testInOutQueueProducerWithCorrelationId.

@Test
public void testInOutQueueProducerWithCorrelationId() throws Exception {
    String queueName = "in.out.queue.producer.test.request";
    MessageConsumer mc = createQueueConsumer(queueName);
    assertNotNull(mc);
    final String requestText = "Hello World!";
    final String responseText = "How are you";
    mc.setMessageListener(new MyMessageListener(requestText, responseText));
    final String correlationId = UUID.randomUUID().toString().replace("-", "");
    Exchange exchange = template.request("sjms:queue:" + queueName + "?exchangePattern=InOut", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody(requestText);
            exchange.getIn().setHeader("JMSCorrelationID", correlationId);
        }
    });
    assertNotNull(exchange);
    assertTrue(exchange.getIn().getBody() instanceof String);
    assertEquals(responseText, exchange.getOut().getBody());
    assertEquals(correlationId, exchange.getOut().getHeader("JMSCorrelationID", String.class));
    mc.close();
}
Also used : Exchange(org.apache.camel.Exchange) MessageConsumer(javax.jms.MessageConsumer) Processor(org.apache.camel.Processor) JMSException(javax.jms.JMSException) Test(org.junit.Test)

Example 19 with MessageConsumer

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

the class InOutTempQueueProducerTest method testInOutQueueProducer.

@Test
public void testInOutQueueProducer() throws Exception {
    String queueName = "in.out.queue.producer.test.request";
    MessageConsumer mc = createQueueConsumer(queueName);
    assertNotNull(mc);
    final String requestText = "Hello World!";
    final String responseText = "How are you";
    mc.setMessageListener(new MyMessageListener(requestText, responseText));
    Object responseObject = template.requestBody("sjms:queue:" + queueName + "?exchangePattern=InOut", requestText);
    assertNotNull(responseObject);
    assertTrue(responseObject instanceof String);
    assertEquals(responseText, responseObject);
    mc.close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Test(org.junit.Test)

Example 20 with MessageConsumer

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

the class QueueProducerTest method testQueueProducer.

@Test
public void testQueueProducer() throws Exception {
    MessageConsumer mc = createQueueConsumer(TEST_DESTINATION_NAME);
    assertNotNull(mc);
    final String expectedBody = "Hello World!";
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    mock.expectedBodiesReceived(expectedBody);
    template.sendBody("direct:start", expectedBody);
    Message message = mc.receive(5000);
    assertNotNull(message);
    assertTrue(message instanceof TextMessage);
    TextMessage tm = (TextMessage) message;
    String text = tm.getText();
    assertNotNull(text);
    template.sendBody("direct:finish", text);
    mock.assertIsSatisfied();
    mc.close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Aggregations

MessageConsumer (javax.jms.MessageConsumer)82 Session (javax.jms.Session)53 Message (javax.jms.Message)40 MessageProducer (javax.jms.MessageProducer)40 Test (org.junit.Test)39 Connection (javax.jms.Connection)38 TextMessage (javax.jms.TextMessage)38 ConnectionFactory (javax.jms.ConnectionFactory)14 JMSException (javax.jms.JMSException)14 Destination (javax.jms.Destination)12 Queue (javax.jms.Queue)8 TemporaryQueue (javax.jms.TemporaryQueue)8 ObjectMessage (javax.jms.ObjectMessage)6 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)5 Serializable (java.io.Serializable)4 Map (java.util.Map)4 TreeMap (java.util.TreeMap)4 MessageListener (javax.jms.MessageListener)4 HornetQMixIn (org.switchyard.component.test.mixins.hornetq.HornetQMixIn)4 ArrayList (java.util.ArrayList)3