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 vcell by virtualcell.
the class VCMessageJms method show.
public String show() {
StringBuffer buffer = new StringBuffer();
try {
java.util.Enumeration enum1 = jmsMessage.getPropertyNames();
while (enum1.hasMoreElements()) {
String propName = (String) enum1.nextElement();
try {
String value = jmsMessage.getStringProperty(propName);
buffer.append(" " + propName + "='" + value + "'");
} catch (MessagePropertyNotFoundException ex) {
// definitely should not happen
delegate.onException(ex);
}
}
int maxContentLength = 120;
if (jmsMessage instanceof TextMessage) {
buffer.append(" textContent='");
String textContent = ((TextMessage) jmsMessage).getText();
if (textContent != null && textContent.length() > maxContentLength) {
buffer.append(textContent.substring(0, maxContentLength - 3) + "...");
} else {
buffer.append(textContent);
}
buffer.append("'");
} else if (jmsMessage instanceof ObjectMessage) {
buffer.append(" objectContent='");
String text = "" + ((ObjectMessage) jmsMessage).getObject();
if (text.length() > maxContentLength) {
buffer.append(text.substring(0, maxContentLength - 3) + "...");
} else {
buffer.append(text);
}
buffer.append("'");
}
} catch (JMSException e) {
e.printStackTrace(System.out);
throw new RuntimeException(e.getMessage(), e);
}
return buffer.toString();
}
use of javax.jms.ObjectMessage in project ArachneCentralAPI by OHDSI.
the class BaseDataNodeMessagingController method saveCommonEntity.
private void saveCommonEntity(Principal principal, String id, Serializable object) throws PermissionDeniedException {
DataNode dataNode = getDatanode(principal);
String queueBase = MessagingUtils.Entities.getBaseQueue(dataNode);
String responseQueue = getResponseQueueName(queueBase);
jmsTemplate.send(responseQueue, session -> {
ObjectMessage message = session.createObjectMessage(object);
message.setJMSCorrelationID(id);
return message;
});
}
use of javax.jms.ObjectMessage in project ArachneCentralAPI by OHDSI.
the class BaseDataNodeMessagingController method saveListResponse.
/**
* Posts responses for for CommonEntity list requests
* (for pushing by Node's back)
*/
@RequestMapping(value = "/api/v1/data-nodes/entity-lists/responses", method = POST)
public void saveListResponse(@RequestBody @Valid CommonListEntityResponseDTO commonListEntityResponseDTO, Principal principal) throws PermissionDeniedException {
DataNode dataNode = getDatanode(principal);
String responseQueue = getResponseQueueName(MessagingUtils.EntitiesList.getBaseQueue(dataNode));
List<CommonEntityDTO> response = (List<CommonEntityDTO>) commonListEntityResponseDTO.getEntities();
for (String correlationId : commonListEntityResponseDTO.getRequestIds()) {
jmsTemplate.send(responseQueue, session -> {
ObjectMessage message = session.createObjectMessage((Serializable) response);
message.setJMSCorrelationID(correlationId);
return message;
});
}
}
Aggregations