use of javax.jms.MessageConsumer in project quickstarts by jboss-switchyard.
the class JCAOutboundBindingTest method testOrderService.
@Test
public void testOrderService() throws Exception {
String[] orders = { "BREAD", "PIZZA", "JAM", "POTATO", "MILK", "JAM" };
Connection conn = _connectionFactory.createConnection(HORNETQ_USER, HORNETQ_PASSWORD);
conn.start();
try {
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(_orderQueue);
for (String order : orders) {
final TextMessage message = session.createTextMessage();
message.setText(order);
producer.send(message);
}
session.close();
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(_orderQueue);
Assert.assertNull("Request message is still in the queue:" + _orderQueue, consumer.receive(3000));
consumer.close();
consumer = session.createConsumer(_shippingQueue);
List<String> expectedShippingOrders = new ArrayList<String>(Arrays.asList("BREAD", "JAM", "MILK", "JAM"));
Message msg = null;
while ((msg = consumer.receive(1000)) != null) {
Assert.assertTrue(expectedShippingOrders.remove(TextMessage.class.cast(msg).getText()));
}
Assert.assertEquals(0, expectedShippingOrders.size());
consumer.close();
consumer = session.createConsumer(_fillingStockQueue);
List<String> expectedFillingStockOrders = new ArrayList<String>(Arrays.asList("PIZZA", "POTATO"));
while ((msg = consumer.receive(1000)) != null) {
Assert.assertTrue(expectedFillingStockOrders.remove(TextMessage.class.cast(msg).getText()));
}
Assert.assertEquals(0, expectedFillingStockOrders.size());
consumer.close();
} finally {
conn.close();
}
}
use of javax.jms.MessageConsumer in project camel by apache.
the class InOnlyTopicProducerTest method testInOnlyTopicProducerProducer.
@Test
public void testInOnlyTopicProducerProducer() throws Exception {
MessageConsumer mc = createTopicConsumer(TEST_DESTINATION_NAME, null);
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();
}
use of javax.jms.MessageConsumer in project camel by apache.
the class InOutQueueProducerTest method testInOutQueueProducerWithCorrelationId.
@Test
public void testInOutQueueProducerWithCorrelationId() 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));
final String correlationId = UUID.randomUUID().toString().replace("-", "");
Exchange exchange = template.request("direct:start", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getOut().setBody(requestText);
exchange.getOut().setHeader("JMSCorrelationID", correlationId);
}
});
assertNotNull(exchange);
assertTrue(exchange.getIn().getBody() instanceof String);
assertEquals(responseText, exchange.getIn().getBody());
assertEquals(correlationId, exchange.getIn().getHeader("JMSCorrelationID", String.class));
mc.close();
}
use of javax.jms.MessageConsumer in project ignite by apache.
the class JmsStreamer method initializeJmsObjectsForTopic.
private void initializeJmsObjectsForTopic() throws JMSException {
Session session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
Topic topic = (Topic) destination;
if (destination == null)
topic = session.createTopic(destinationName);
MessageConsumer consumer = durableSubscription ? session.createDurableSubscriber(topic, durableSubscriptionName) : session.createConsumer(topic);
IgniteJmsMessageListener messageListener = new IgniteJmsMessageListener(session, true);
consumer.setMessageListener(messageListener);
consumers.add(consumer);
sessions.add(session);
listeners.add(messageListener);
}
use of javax.jms.MessageConsumer in project karaf by apache.
the class PooledSession method close.
@Override
public void close() throws JMSException {
if (!ignoreClose) {
boolean invalidate = false;
try {
// lets reset the session
getInternalSession().setMessageListener(null);
// Close any consumers and browsers that may have been created.
for (MessageConsumer consumer : consumers) {
consumer.close();
}
for (QueueBrowser browser : browsers) {
browser.close();
}
if (transactional && !isXa) {
try {
getInternalSession().rollback();
} catch (JMSException e) {
invalidate = true;
LOG.warn("Caught exception trying rollback() when putting session back into the pool, will invalidate. " + e, e);
}
}
} catch (JMSException ex) {
invalidate = true;
LOG.warn("Caught exception trying close() when putting session back into the pool, will invalidate. " + ex, ex);
} finally {
consumers.clear();
browsers.clear();
for (PooledSessionEventListener listener : this.sessionEventListeners) {
listener.onSessionClosed(this);
}
sessionEventListeners.clear();
}
if (invalidate) {
// instead invalidate it so the pool can create a new one on demand.
if (session != null) {
try {
session.close();
} catch (JMSException e1) {
LOG.trace("Ignoring exception on close as discarding session: " + e1, e1);
}
session = null;
}
try {
sessionPool.invalidateObject(key, this);
} catch (Exception e) {
LOG.trace("Ignoring exception on invalidateObject as discarding session: " + e, e);
}
} else {
try {
sessionPool.returnObject(key, this);
} catch (Exception e) {
javax.jms.IllegalStateException illegalStateException = new javax.jms.IllegalStateException(e.toString());
illegalStateException.initCause(e);
throw illegalStateException;
}
}
}
}
Aggregations