use of javax.jms.ObjectMessage 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.ObjectMessage in project wildfly by wildfly.
the class ObjectMessageTestCase method testObjectMessageWithObjectArray.
/**
* Test that the MDB can process a {@link ObjectMessage} which consists of an array of objects,
* without any classloading issues
*
* @throws Exception
*/
@Test
public void testObjectMessageWithObjectArray() throws Exception {
final String goodMorning = "Good morning";
final String goodEvening = "Good evening";
// create the message
final SimpleMessageInEarLibJar[] multipleGreetings = new SimpleMessageInEarLibJar[2];
final SimpleMessageInEarLibJar messageOne = new SimpleMessageInEarLibJar(goodMorning);
final SimpleMessageInEarLibJar messageTwo = new SimpleMessageInEarLibJar(goodEvening);
multipleGreetings[0] = messageOne;
multipleGreetings[1] = messageTwo;
// send as ObjectMessage
this.jmsUtil.sendObjectMessage(multipleGreetings, this.objectMessageOfArrayTypeQueue, this.objectMessageOfArrayTypeReplyQueue);
// wait for an reply
final Message reply = this.jmsUtil.receiveMessage(objectMessageOfArrayTypeReplyQueue, 5000);
// test the reply
Assert.assertNotNull("Reply message was null on reply queue: " + this.objectMessageOfArrayTypeReplyQueue, reply);
final SimpleMessageInEarLibJar[] replyMessage = (SimpleMessageInEarLibJar[]) ((ObjectMessage) reply).getObject();
Assert.assertTrue("Unexpected reply message on reply queue: " + this.objectMessageOfArrayTypeReplyQueue, Arrays.equals(replyMessage, multipleGreetings));
}
use of javax.jms.ObjectMessage in project wildfly by wildfly.
the class ObjectMessageTestCase method testObjectMessage.
/**
* Test that the MDB can process a {@link ObjectMessage} without any classloading issues
*
* @throws Exception
*/
@Test
public void testObjectMessage() throws Exception {
final String goodAfternoon = "Good afternoon!";
// create the message
final SimpleMessageInEarLibJar message = new SimpleMessageInEarLibJar(goodAfternoon);
// send as ObjectMessage
this.jmsUtil.sendObjectMessage(message, this.objectMessageQueue, this.objectMessageReplyQueue);
// wait for an reply
final Message reply = this.jmsUtil.receiveMessage(objectMessageReplyQueue, 5000);
// test the reply
Assert.assertNotNull("Reply message was null on reply queue: " + this.objectMessageReplyQueue, reply);
final SimpleMessageInEarLibJar replyMessage = (SimpleMessageInEarLibJar) ((ObjectMessage) reply).getObject();
Assert.assertEquals("Unexpected reply message on reply queue: " + this.objectMessageReplyQueue, message, replyMessage);
}
use of javax.jms.ObjectMessage in project tomee by apache.
the class JMSProducerImpl method send.
@Override
public JMSProducer send(final Destination destination, final Serializable body) {
final ObjectMessage message = wrap(context.createObjectMessage(body));
send(destination, message);
return this;
}
use of javax.jms.ObjectMessage in project tomee by apache.
the class JmsTest method createSender.
@SuppressWarnings("unchecked")
private synchronized void createSender(final Connection connection, final Destination requestQueue) throws JMSException {
Session session = null;
MessageProducer producer = null;
MessageConsumer consumer = null;
try {
// create request
final Map<String, Object> request = new TreeMap<>();
request.put("args", new Object[] { "cheese" });
// create a new temp response queue
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Destination responseQueue = session.createTemporaryQueue();
// Create a request messages
final ObjectMessage requestMessage = session.createObjectMessage();
requestMessage.setJMSReplyTo(responseQueue);
requestMessage.setObject((Serializable) request);
// Send the request message
producer = session.createProducer(requestQueue);
producer.send(requestMessage);
// wait for the response message
consumer = session.createConsumer(responseQueue);
final Message message = consumer.receive(30000);
// verify message
assertNotNull("Did not get a response message", message);
assertTrue("Response message is not an ObjectMessage", message instanceof ObjectMessage);
final ObjectMessage responseMessage = (ObjectMessage) message;
final Serializable object = responseMessage.getObject();
assertNotNull("Response ObjectMessage contains a null object");
assertTrue("Response ObjectMessage does not contain an instance of Map", object instanceof Map);
final Map<String, String> response = (Map<String, String>) object;
// process results
final String returnValue = response.get("return");
assertEquals("test-cheese", returnValue);
} finally {
MdbUtil.close(consumer);
MdbUtil.close(producer);
MdbUtil.close(session);
}
}
Aggregations