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<>();
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();
}
}
use of javax.jms.ObjectMessage in project openolat by klemens.
the class JmsIndexer method onMessage.
@Override
public void onMessage(Message message) {
if (message instanceof ObjectMessage) {
try {
ObjectMessage objMsg = (ObjectMessage) message;
JmsIndexWork workUnit = (JmsIndexWork) objMsg.getObject();
if (JmsIndexWork.INDEX.equals(workUnit.getAction())) {
doIndex(workUnit);
} else if (JmsIndexWork.DELETE.equals(workUnit.getAction())) {
doDelete(workUnit);
}
message.acknowledge();
} catch (JMSException e) {
log.error("", e);
} finally {
DBFactory.getInstance().commitAndCloseSession();
}
}
}
use of javax.jms.ObjectMessage in project openolat by klemens.
the class SearchClientProxy method doSearch.
/**
* Uses Request/reply mechanism for synchronous operation.
* @see org.olat.search.service.searcher.OLATSearcher#doSearch(java.lang.String, org.olat.core.id.Identity, org.olat.core.id.Roles, boolean)
*/
@Override
public SearchResults doSearch(String queryString, List<String> condQueries, Identity identity, Roles roles, int firstResult, int maxResults, boolean doHighlighting) throws ServiceNotAvailableException, ParseException, QueryException {
boolean isDebug = log.isDebug();
if (isDebug) {
log.debug("STARTqueryString=" + queryString);
}
SearchRequest searchRequest = new SearchRequest(queryString, condQueries, identity.getKey(), roles, firstResult, maxResults, doHighlighting);
Session session = null;
try {
session = acquireSession();
if (isDebug) {
log.debug("doSearch session=" + session);
}
Message requestMessage = session.createObjectMessage(searchRequest);
Message returnedMessage = doSearchRequest(session, requestMessage);
queryCount_++;
if (returnedMessage != null) {
String responseStatus = returnedMessage.getStringProperty(JMS_RESPONSE_STATUS_PROPERTY_NAME);
if (responseStatus.equalsIgnoreCase(JMS_RESPONSE_STATUS_OK)) {
SearchResults searchResult = (SearchResults) ((ObjectMessage) returnedMessage).getObject();
if (isDebug) {
log.debug("ENDqueryString=" + queryString);
}
return searchResult;
} else if (responseStatus.equalsIgnoreCase(JMS_RESPONSE_STATUS_PARSE_EXCEPTION)) {
throw new ParseException("can not parse query=" + queryString);
} else if (responseStatus.equalsIgnoreCase(JMS_RESPONSE_STATUS_QUERY_EXCEPTION)) {
throw new QueryException("invalid query=" + queryString);
} else if (responseStatus.equalsIgnoreCase(JMS_RESPONSE_STATUS_SERVICE_NOT_AVAILABLE_EXCEPTION)) {
throw new ServiceNotAvailableException("Remote search service not available" + queryString);
} else {
log.warn("doSearch: receive unkown responseStatus=" + responseStatus);
return null;
}
} else {
// null returnedMessage
throw new ServiceNotAvailableException("communication error with JMS - cannot receive messages!!!");
}
} catch (JMSException e) {
log.error("Search failure I", e);
throw new ServiceNotAvailableException("communication error with JMS - cannot send messages!!!");
} finally {
releaseSession(session);
}
}
use of javax.jms.ObjectMessage in project openolat by klemens.
the class JmsSearchProvider method onMessage.
public void onMessage(Message message) {
if (log_.isDebug()) {
log_.debug("onMessage, message=" + message);
}
try {
long sentTimestamp = message.getJMSTimestamp();
long currentTimestamp = System.currentTimeMillis();
// check if received message is not too old because in case of overload we could have old search-messages
if ((currentTimestamp - sentTimestamp) < receiveTimeout) {
final String correlationID = message.getJMSCorrelationID();
final Destination replyTo = message.getJMSReplyTo();
if (message instanceof ObjectMessage) {
ObjectMessage objectMessage = (ObjectMessage) message;
final SearchRequest searchRequest = (SearchRequest) objectMessage.getObject();
taskExecutorManager.execute(new Runnable() {
public void run() {
onSearchMessage(searchRequest, correlationID, replyTo);
}
});
} else if (message instanceof TextMessage) {
TextMessage testMessage = (TextMessage) message;
final String spellText = testMessage.getText();
taskExecutorManager.execute(new Runnable() {
public void run() {
onSpellMessage(spellText, correlationID, replyTo);
}
});
}
} else {
// JMS message is too old, discard it (do nothing)
log_.warn("JMS message was too old, discard message, timeout=" + receiveTimeout + "ms , received time=" + (currentTimestamp - sentTimestamp) + "ms");
}
} catch (JMSException e) {
log_.error("error when receiving jms messages", e);
// signal search not available
return;
} catch (Error err) {
log_.warn("Error in onMessage, ", err);
// OLAT-3973: don't throw exceptions here
} catch (RuntimeException runEx) {
log_.warn("RuntimeException in onMessage, ", runEx);
// OLAT-3973: don't throw exceptions here
}
}
Aggregations