Search in sources :

Example 81 with ObjectMessage

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();
    }
}
Also used : Serializable(java.io.Serializable) Method(java.lang.reflect.Method) TreeMap(java.util.TreeMap) InvocationTargetException(java.lang.reflect.InvocationTargetException) JMSException(javax.jms.JMSException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ObjectMessage(javax.jms.ObjectMessage) MessageProducer(javax.jms.MessageProducer) TreeMap(java.util.TreeMap) Map(java.util.Map) Session(javax.jms.Session)

Example 82 with ObjectMessage

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();
    }
}
Also used : Serializable(java.io.Serializable) Method(java.lang.reflect.Method) TreeMap(java.util.TreeMap) InvocationTargetException(java.lang.reflect.InvocationTargetException) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ObjectMessage(javax.jms.ObjectMessage) TreeMap(java.util.TreeMap) Map(java.util.Map) Session(javax.jms.Session)

Example 83 with ObjectMessage

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();
        }
    }
}
Also used : ObjectMessage(javax.jms.ObjectMessage) JMSException(javax.jms.JMSException)

Example 84 with ObjectMessage

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);
    }
}
Also used : ServiceNotAvailableException(org.olat.search.ServiceNotAvailableException) QueryException(org.olat.search.QueryException) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) JMSException(javax.jms.JMSException) ParseException(org.apache.lucene.queryparser.classic.ParseException) SearchResults(org.olat.search.SearchResults) Session(javax.jms.Session)

Example 85 with ObjectMessage

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
    }
}
Also used : Destination(javax.jms.Destination) ObjectMessage(javax.jms.ObjectMessage) JMSException(javax.jms.JMSException) TextMessage(javax.jms.TextMessage)

Aggregations

ObjectMessage (javax.jms.ObjectMessage)227 Test (org.junit.Test)85 JMSException (javax.jms.JMSException)82 Session (javax.jms.Session)79 Message (javax.jms.Message)70 MessageProducer (javax.jms.MessageProducer)64 MessageConsumer (javax.jms.MessageConsumer)52 Connection (javax.jms.Connection)46 Queue (javax.jms.Queue)40 TextMessage (javax.jms.TextMessage)38 BytesMessage (javax.jms.BytesMessage)30 Destination (javax.jms.Destination)29 MapMessage (javax.jms.MapMessage)28 AbstractServiceComponentAgentFunctionalIntegrationTest (com.hack23.cia.service.component.agent.impl.AbstractServiceComponentAgentFunctionalIntegrationTest)25 StreamMessage (javax.jms.StreamMessage)21 HashMap (java.util.HashMap)18 Serializable (java.io.Serializable)17 Map (java.util.Map)16 ArrayList (java.util.ArrayList)13 QueueSession (javax.jms.QueueSession)11