use of javax.jms.Connection 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.Connection 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.Connection 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.Connection in project wildfly by wildfly.
the class EnvEntryTestCase method testEnvEntriesMDB.
@Test
public void testEnvEntriesMDB() throws Exception {
ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
Connection con = factory.createConnection();
try {
Destination dest = (Destination) ctx.lookup("java:jboss/queue/testEnvEntry");
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(16, replyMsg.getInt("maxExceptions"));
Assert.assertEquals(12, replyMsg.getInt("numExceptions"));
Assert.assertEquals(7, replyMsg.getInt("minExceptions"));
} finally {
con.close();
}
}
use of javax.jms.Connection in project wildfly by wildfly.
the class TestEnvEntryMDBean method onMessage.
public void onMessage(Message msg) {
log.trace("OnMessage working...");
try {
Destination destination = msg.getJMSReplyTo();
Connection conn = connectionFactory.createConnection();
try {
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer replyProducer = session.createProducer(destination);
MapMessage replyMsg = session.createMapMessage();
replyMsg.setJMSCorrelationID(msg.getJMSMessageID());
replyMsg.setInt("maxExceptions", maxExceptions);
replyMsg.setInt("numExceptions", numExceptions);
replyMsg.setInt("minExceptions", minExceptions);
// System.err.println("reply to: " + destination);
// System.err.println("maxExceptions: " + maxExceptions);
// System.err.println("numExceptions: " + numExceptions);
// System.err.println("minExceptions: " + minExceptions);
replyProducer.send(replyMsg);
} finally {
conn.close();
}
} catch (JMSException e) {
throw new RuntimeException(e);
}
}
Aggregations