use of javax.jms.Session in project wildfly by wildfly.
the class ReplyUtil method reply.
public static void reply(ConnectionFactory factory, Message message) {
Connection connection = null;
try {
connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Destination destination = message.getJMSReplyTo();
// ignore messages that need no reply
if (destination == null)
return;
final MessageProducer replyProducer = session.createProducer(destination);
final Message replyMsg = session.createMessage();
replyMsg.setJMSCorrelationID(message.getJMSMessageID());
replyProducer.send(replyMsg);
replyProducer.close();
} catch (JMSException e) {
throw new RuntimeException(e);
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
}
}
}
}
use of javax.jms.Session in project wildfly by wildfly.
the class HelloBean method sendMessage.
public String sendMessage() throws Exception {
String destinationName = "java:jboss/exported/queue/TestQueue";
Context ic = null;
ConnectionFactory cf = null;
Connection connection = null;
try {
ic = getInitialContext();
cf = (ConnectionFactory) ic.lookup("java:/ConnectionFactory");
Queue queue = (Queue) ic.lookup(destinationName);
connection = cf.createConnection("guest", "guest");
// we need to start connection for consumer
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer sender = session.createProducer(queue);
TextMessage message = session.createTextMessage("hello goodbye");
TemporaryQueue replyQueue = session.createTemporaryQueue();
message.setJMSReplyTo(replyQueue);
sender.send(message);
MessageConsumer consumer = session.createConsumer(replyQueue);
TextMessage replyMsg = (TextMessage) consumer.receive(5000);
log.trace("Message received:" + replyMsg);
return replyMsg.getText();
} finally {
if (ic != null) {
try {
ic.close();
} catch (Exception ignore) {
}
}
closeConnection(connection);
}
}
use of javax.jms.Session in project wildfly by wildfly.
the class RunAsTestCaseEJBMDB method testSendMessage.
@Test
public void testSendMessage() throws Exception {
ConnectionFactory cf = null;
Connection connection = null;
Session session = null;
try {
cf = (ConnectionFactory) initialContext.lookup("jms/RemoteConnectionFactory");
Queue queue = (Queue) initialContext.lookup(QUEUE_NAME);
connection = cf.createConnection("guest", "guest");
//for consumer we need to start connection
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer sender = session.createProducer(queue);
TemporaryQueue replyQueue = session.createTemporaryQueue();
TextMessage message = session.createTextMessage("hello goodbye");
message.setJMSReplyTo(replyQueue);
sender.send(message);
log.trace("testSendMessage(): Message sent!");
MessageConsumer consumer = session.createConsumer(replyQueue);
Message replyMsg = consumer.receive(5000);
Assert.assertNotNull(replyMsg);
Assert.assertTrue(replyMsg instanceof TextMessage);
String actual = ((TextMessage) replyMsg).getText();
Assert.assertEquals("Howdy Fred! GoodBye user1", actual);
} finally {
if (session != null) {
session.close();
}
closeConnection(connection);
}
}
use of javax.jms.Session in project wildfly by wildfly.
the class RunAsMDBUnitTestCase method testSendMessage.
@Test
public void testSendMessage() throws Exception {
ConnectionFactory connFactory = lookup("ConnectionFactory", ConnectionFactory.class);
Connection conn = connFactory.createConnection();
conn.start();
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
TemporaryQueue replyQueue = session.createTemporaryQueue();
TextMessage msg = session.createTextMessage("Hello world");
msg.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
msg.setJMSReplyTo(replyQueue);
Queue queue = lookup("java:jboss/" + queueName, Queue.class);
MessageProducer producer = session.createProducer(queue);
producer.send(msg);
MessageConsumer consumer = session.createConsumer(replyQueue);
Message replyMsg = consumer.receive(5000);
Assert.assertNotNull(replyMsg);
if (replyMsg instanceof ObjectMessage) {
Exception e = (Exception) ((ObjectMessage) replyMsg).getObject();
throw e;
}
Assert.assertTrue(replyMsg instanceof TextMessage);
String actual = ((TextMessage) replyMsg).getText();
Assert.assertEquals("SUCCESS", actual);
consumer.close();
producer.close();
session.close();
conn.close();
}
use of javax.jms.Session in project wildfly by wildfly.
the class GetCallerPrincipalTestCase method testMDBLifecycle.
/**
* Run this one in the container so it can lookup the queue
* @throws Exception
*/
@OperateOnDeployment("test")
@Test
public void testMDBLifecycle() throws Exception {
deployer.deploy("mdb");
SecurityClient client = this.login();
ITestResultsSingleton results = this.getResultsSingleton();
MessageProducer producer = null;
MessageConsumer consumer = null;
QueueConnection conn = null;
Session session = null;
try {
QueueConnectionFactory qcf = (QueueConnectionFactory) new InitialContext().lookup("java:/ConnectionFactory");
Queue queue = (Queue) new InitialContext().lookup("java:jboss/" + QUEUE_NAME);
conn = qcf.createQueueConnection("guest", "guest");
conn.start();
session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
TemporaryQueue replyQueue = session.createTemporaryQueue();
TextMessage msg = session.createTextMessage("Hello world");
msg.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
msg.setJMSReplyTo(replyQueue);
producer = session.createProducer(queue);
producer.send(msg);
consumer = session.createConsumer(replyQueue);
Message replyMsg = consumer.receive(5000);
Object obj = ((ObjectMessage) replyMsg).getObject();
log.trace("MDB message get: " + obj);
Assert.assertEquals(OK + "start", results.getMdb("postconstruct"));
deployer.undeploy("mdb");
Assert.assertEquals(OK + "stop", results.getMdb("predestroy"));
} finally {
if (consumer != null) {
consumer.close();
}
if (producer != null) {
producer.close();
}
if (session != null) {
session.close();
}
if (conn != null) {
conn.close();
}
client.logout();
}
}
Aggregations