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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations