use of javax.jms.ObjectMessage 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();
}
}
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<String, Object>();
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);
}
}
use of javax.jms.ObjectMessage in project tomee by apache.
the class MdbInvoker method onMessage.
public void onMessage(final Message message) {
if (!(message instanceof ObjectMessage))
return;
try {
final Session session = getSession();
if (session == null)
throw new IllegalStateException("Invoker has been destroyed");
if (message == null)
throw new NullPointerException("request message is null");
if (!(message instanceof ObjectMessage))
throw new IllegalArgumentException("Expected a ObjectMessage request but got a " + message.getClass().getName());
final ObjectMessage objectMessage = (ObjectMessage) message;
final Serializable object = objectMessage.getObject();
if (object == null)
throw new NullPointerException("object in ObjectMessage is null");
if (!(object instanceof Map)) {
if (message instanceof ObjectMessage)
throw new IllegalArgumentException("Expected a Map contained in the ObjectMessage request but got a " + object.getClass().getName());
}
final Map request = (Map) object;
final String signature = (String) request.get("method");
final Method method = signatures.get(signature);
final Object[] args = (Object[]) request.get("args");
boolean exception = false;
Object result = null;
try {
result = method.invoke(target, args);
} catch (final IllegalAccessException e) {
result = e;
exception = true;
} catch (final InvocationTargetException e) {
result = e.getCause();
if (result == null)
result = e;
exception = true;
}
MessageProducer producer = null;
try {
// create response
final Map<String, Object> response = new TreeMap<String, Object>();
if (exception) {
response.put("exception", "true");
}
response.put("return", result);
// create response message
final ObjectMessage resMessage = session.createObjectMessage();
resMessage.setJMSCorrelationID(objectMessage.getJMSCorrelationID());
resMessage.setObject((Serializable) response);
// send response message
producer = session.createProducer(objectMessage.getJMSReplyTo());
producer.send(resMessage);
} catch (final Exception e) {
e.printStackTrace();
} finally {
MdbUtil.close(producer);
destroy();
}
} catch (final Throwable e) {
e.printStackTrace();
}
}
use of javax.jms.ObjectMessage in project tomee by apache.
the class MdbInvoker method onMessage.
@Override
public void onMessage(final Message message) {
try {
if (!(message instanceof ObjectMessage)) {
return;
}
final Session session = getSession();
if (session == null)
throw new IllegalStateException("Invoker has been destroyed");
if (message == null)
throw new NullPointerException("request message is null");
if (!(message instanceof ObjectMessage))
throw new IllegalArgumentException("Expected a ObjectMessage request but got a " + message.getClass().getName());
final Serializable object = ((ObjectMessage) message).getObject();
if (object == null)
throw new NullPointerException("object in ObjectMessage is null");
if (!(object instanceof Map)) {
if (message instanceof ObjectMessage)
throw new IllegalArgumentException("Expected a Map contained in the ObjectMessage request but got a " + object.getClass().getName());
}
final Map request = (Map) object;
final String signature = (String) request.get("method");
if (signature == null)
throw new NullPointerException("method property is null");
final Method method = signatures.get(signature);
if (method == null)
throw new IllegalArgumentException("no such method " + signature + "; known methods are " + signatures.keySet());
final Object[] args = (Object[]) request.get("args");
boolean exception = false;
Object result;
try {
result = method.invoke(target, args);
} catch (final IllegalAccessException e) {
result = e;
exception = true;
} catch (final InvocationTargetException e) {
result = e.getCause();
if (result == null)
result = e;
exception = true;
} catch (final Exception e) {
result = e.getCause();
if (result == null)
result = e;
exception = true;
}
try {
// create response
final Map<String, Object> response = new TreeMap<String, Object>();
if (exception) {
response.put("exception", "true");
}
response.put("return", result);
// create response message
final ObjectMessage resMessage = session.createObjectMessage();
resMessage.setJMSCorrelationID(message.getJMSCorrelationID());
resMessage.setObject((Serializable) response);
// send response message
replyProducer.send(message.getJMSReplyTo(), resMessage);
} catch (final Exception e) {
e.printStackTrace();
}
} catch (final Throwable e) {
e.printStackTrace();
} finally {
this.destroy();
}
}
Aggregations