use of javax.jms.MessageConsumer in project wildfly by wildfly.
the class SendToJMSQueueTest method sendMessageWithClientAcknowledge.
@Test
public void sendMessageWithClientAcknowledge() throws Exception {
Connection senderConnection = null;
Connection consumerConnection = null;
Session senderSession = null;
Session consumerSession = null;
MessageConsumer consumer = null;
try {
// CREATE CONSUMER
logger.trace("******* Creating connection for consumer");
consumerConnection = factory.createConnection();
logger.trace("Creating session for consumer");
consumerSession = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
logger.trace("Creating consumer");
consumer = consumerSession.createConsumer(queue2);
logger.trace("Start session");
consumerConnection.start();
// SEND A MESSAGE
logger.trace("***** Start - sending message to topic");
senderConnection = factory.createConnection();
logger.trace("Creating session..");
senderSession = senderConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
MessageProducer producer = senderSession.createProducer(queue2);
TextMessage message = senderSession.createTextMessage("Hahaha!");
logger.trace("Sending..");
producer.send(message);
logger.trace("Message sent");
senderConnection.start();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
logger.trace("Closing connections and sessions");
if (senderSession != null) {
senderSession.close();
}
if (senderConnection != null) {
senderConnection.close();
}
}
Message receivedMessage = null;
Message receivedMessage2 = null;
try {
logger.trace("Receiving");
receivedMessage = consumer.receive(5000);
logger.trace("Received a message");
receivedMessage.acknowledge();
consumerSession.recover();
receivedMessage2 = consumer.receive(5000);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (consumerSession != null) {
consumerSession.close();
}
if (consumerConnection != null) {
consumerConnection.close();
}
}
if (receivedMessage == null) {
Assert.fail("received null instead of a TextMessage");
}
Assert.assertTrue("received a " + receivedMessage.getClass().getName() + " instead of a TextMessage", receivedMessage instanceof TextMessage);
Assert.assertEquals("Hahaha!", ((TextMessage) receivedMessage).getText());
if (receivedMessage2 != null) {
Assert.fail("redelivered=" + String.valueOf(receivedMessage2.getJMSRedelivered()) + ", text=" + ((TextMessage) receivedMessage).getText());
}
Assert.assertNull("Message should not have been re-delivered", receivedMessage2);
}
use of javax.jms.MessageConsumer in project wildfly by wildfly.
the class SendToJMSQueueTest method sendMessageWithMissingClientAcknowledge.
@Test
public void sendMessageWithMissingClientAcknowledge() throws Exception {
Connection senderConnection = null;
Connection consumerConnection = null;
Session senderSession = null;
Session consumerSession = null;
MessageConsumer consumer = null;
try {
// CREATE SUBSCRIBER
logger.trace("******* Creating connection for consumer");
consumerConnection = factory.createConnection();
logger.trace("Creating session for consumer");
consumerSession = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
logger.trace("Creating consumer");
consumer = consumerSession.createConsumer(queue3);
logger.trace("Start session");
consumerConnection.start();
// SEND A MESSAGE
logger.trace("***** Start - sending message to topic");
senderConnection = factory.createConnection();
logger.trace("Creating session..");
senderSession = senderConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
MessageProducer producer = senderSession.createProducer(queue3);
TextMessage message = senderSession.createTextMessage("Hello world!");
logger.trace("Sending..");
producer.send(message);
logger.trace("Message sent");
senderConnection.start();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
logger.trace("Closing connections and sessions");
if (senderSession != null) {
senderSession.close();
}
if (senderConnection != null) {
senderConnection.close();
}
}
Message receivedMessage = null;
try {
logger.trace("Receiving");
receivedMessage = consumer.receive(5000);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
consumerSession.recover();
receivedMessage = consumer.receive(5000);
receivedMessage.acknowledge();
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
} finally {
if (consumerSession != null) {
consumerSession.close();
}
if (consumerConnection != null) {
consumerConnection.close();
}
}
if (receivedMessage == null) {
Assert.fail("Message should have been re-delivered, but subsequent attempt to receive it returned null");
}
Assert.assertTrue("received a " + receivedMessage.getClass().getName() + " instead of a TextMessage", receivedMessage instanceof TextMessage);
Assert.assertEquals(((TextMessage) receivedMessage).getText(), "Hello world!");
Assert.assertTrue("Redelivered header should have been set to true", receivedMessage.getJMSRedelivered());
}
use of javax.jms.MessageConsumer in project wildfly by wildfly.
the class SendToJMSQueueTest method sendAndReceiveMessage.
@Test
public void sendAndReceiveMessage() throws Exception {
Connection connection = null;
Session session = null;
Message receivedMessage = null;
try {
// SEND A MESSAGE
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
Message message = session.createTextMessage(MESSAGE_TEXT);
producer.send(message);
connection.start();
session.close();
connection.close();
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
}
// RECEIVE THE MESSAGE BACK
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(queue);
connection.start();
receivedMessage = consumer.receive(5000);
} catch (Exception e) {
e.printStackTrace();
} finally {
// CLEANUP
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
}
// ASSERTIONS
Assert.assertTrue(receivedMessage instanceof TextMessage);
Assert.assertTrue(((TextMessage) receivedMessage).getText().equals(MESSAGE_TEXT));
}
use of javax.jms.MessageConsumer in project wildfly by wildfly.
the class ResourceInjectionSubstitutionTestCase method testResourceInjectionSubstitutionMdb.
/**
* Test resource injection with MDB
*/
@Test
public void testResourceInjectionSubstitutionMdb() throws Exception {
// ConnectionFactory and Reply message are injected in SimpleMDB
ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
Connection con = factory.createConnection();
try {
Destination dest = (Destination) ctx.lookup("java:jboss/queue/testQueue");
Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(dest);
Queue replyQueue = session.createTemporaryQueue();
MessageConsumer consumer = session.createConsumer(replyQueue);
con.start();
TextMessage msg = session.createTextMessage();
msg.setJMSReplyTo(replyQueue);
msg.setText("This is message one");
producer.send(msg);
MapMessage replyMsg = (MapMessage) consumer.receive(5000);
Assert.assertNotNull(replyMsg);
Assert.assertEquals("It's Friday!!!", replyMsg.getString("replyMsg"));
} finally {
con.close();
}
}
use of javax.jms.MessageConsumer in project ats-framework by Axway.
the class ManagedSession method close.
@Override
public synchronized void close() throws JMSException {
for (final QueueBrowser b : browsers) try {
b.close();
} catch (final Exception e) {
}
browsers.clear();
for (final MessageConsumer b : consumers) try {
b.close();
} catch (final Exception e) {
}
for (final MessageProducer b : producers) try {
b.close();
} catch (final Exception e) {
}
for (final ManagedSession b : sessions) b.shutdown();
session.close();
}
Aggregations