Search in sources :

Example 6 with QueueInfo

use of org.jaffa.modules.messaging.services.configdomain.QueueInfo in project jaffa-framework by jaffa-projects.

the class JmsQueueAdmin method queueQuery.

public QueueQueryResponse queueQuery(QueueCriteria criteria) {
    if (log.isDebugEnabled())
        log.debug("Input to queueQuery: " + criteria);
    QueueQueryResponse output = new QueueQueryResponse();
    try {
        // Match the queueSystemId as well apply checks for unsupported criteria
        if (FinderTx.match(QUEUE_SYSTEM_ID, criteria.getQueueSystemId()) && FinderTx.match(null, criteria.getSuccessCount()) && FinderTx.match(null, criteria.getHoldCount()) && FinderTx.match(null, criteria.getInterruptedCount())) {
            String[] queueNames = JmsBrowser.getAccessibleQueueNames();
            if (queueNames != null && queueNames.length > 0) {
                PropertyFilter pf = PropertyFilter.getInstance(QueueGraph.class, criteria.getResultGraphRules());
                Collection<QueueGraph> graphs = new LinkedList<QueueGraph>();
                for (String queueName : queueNames) {
                    if (FinderTx.match(queueName, criteria.getType())) {
                        QueueGraph graph = new QueueGraph();
                        // Compute message count only if required by criteria or if it is included in the property-filter
                        if (criteria.getOpenCount() != null || pf.isFieldIncluded("openCount")) {
                            Message[] messages = JmsBrowser.getPendingMessages(queueName);
                            Long count = messages != null ? messages.length : 0L;
                            if (!FinderTx.match(count, criteria.getOpenCount()))
                                continue;
                            if (pf.isFieldIncluded("openCount"))
                                graph.setOpenCount(count);
                        }
                        // Apply the status criteria
                        if (criteria.getStatus() != null || pf.isFieldIncluded("status")) {
                            QueueGraph.Status status;
                            QueueInfo queueInfo = ConfigurationService.getInstance().getQueueInfo(queueName);
                            if (queueInfo != null && queueInfo.getConsumerPolicy() != ConsumerPolicy.NONE)
                                status = QueueGraph.Status.ACTIVE;
                            else
                                status = QueueGraph.Status.ACTIVE;
                            if (!FinderTx.match(status.toString(), criteria.getStatus()))
                                continue;
                            if (pf.isFieldIncluded("status"))
                                graph.setStatus(status);
                        }
                        // Stamp the remaining properties, if included in the property-filter
                        graph.setQueueMetaData(createQueueMetaData(queueName, pf));
                        if (pf.isFieldIncluded("type"))
                            graph.setType(queueName);
                        if (pf.isFieldIncluded("hasAdminAccess"))
                            graph.setHasAdminAccess(JmsBrowser.hasAdminMessageAccess(queueName));
                        graphs.add(graph);
                    }
                }
                if (graphs.size() > 0)
                    output.setGraphs(graphs.toArray(new QueueGraph[graphs.size()]));
            }
        }
    } catch (Exception e) {
        // add errors to the response
        ApplicationExceptions appExps = ExceptionHelper.extractApplicationExceptions(e);
        if (appExps != null) {
            if (log.isDebugEnabled())
                log.debug("Error in queueQuery execution", appExps);
            output.setErrors(ServiceError.generate(appExps));
        } else {
            log.error("Internal Error in queueQuery execution", e);
            output.setErrors(ServiceError.generate(e));
        }
    }
    if (log.isDebugEnabled())
        log.debug("Output from queueQuery: " + output);
    return output;
}
Also used : QueueInfo(org.jaffa.modules.messaging.services.configdomain.QueueInfo) ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) QueueQueryResponse(org.jaffa.qm.apis.data.QueueQueryResponse) LinkedList(java.util.LinkedList) JMSException(javax.jms.JMSException) IntrospectionException(java.beans.IntrospectionException) QueueGraph(org.jaffa.qm.apis.data.QueueGraph) PropertyFilter(org.jaffa.qm.util.PropertyFilter)

Example 7 with QueueInfo

use of org.jaffa.modules.messaging.services.configdomain.QueueInfo in project jaffa-framework by jaffa-projects.

the class JmsBrowser method getPendingMessages.

/**
 * Returns the unconsumed messages in the input queue.
 *
 * @param queueName the queue name.
 * @param filter used for searching within the header information of the retrieved messages. The syntax for the filter will be based on a subset of the SQL92 conditional expression syntax.
 * @return the unconsumed messages in the input queue.
 * @throws FrameworkException in case any internal error occurs.
 * @throws ApplicationExceptions Indicates application error(s).
 */
public static Message[] getPendingMessages(String queueName, String filter) throws FrameworkException, ApplicationExceptions {
    Session session = null;
    try {
        List<Message> messages = new LinkedList<Message>();
        QueueInfo queueInfo = ConfigurationService.getInstance().getQueueInfo(queueName);
        if (hasBrowseQueueAccess(queueInfo)) {
            // Obtain a Connection with the JMS provider
            Connection connection = JaffaConnectionFactory.obtainConnection();
            // Creates a Session from the Connection
            session = JmsClientHelper.obtainSession(connection, false);
            // Creates a QueueBrowser from the Session, using the filter, if supplied
            QueueBrowser qb = session.createBrowser(JmsClientHelper.obtainQueue(queueName), filter);
            if (log.isDebugEnabled())
                log.debug("QueueBrowser created for " + queueName + " with filter " + filter);
            // Check if the user can access all messages in the queue
            boolean browseAllMessagesAccess = hasBrowseAllMessagesAccess(queueInfo);
            if (log.isDebugEnabled())
                log.debug("browseAllMessages access to " + queueName + " is " + browseAllMessagesAccess);
            for (Enumeration e = qb.getEnumeration(); e.hasMoreElements(); ) {
                Message message = (Message) e.nextElement();
                // If this is an error queue, perform the check against the original queue
                if (queueInfo.isErrorQueue() && message.getStringProperty(HEADER_ORIGINAL_QUEUE_NAME) != null) {
                    String originalQueueName = message.getStringProperty(HEADER_ORIGINAL_QUEUE_NAME);
                    if (hasBrowseAllMessagesAccess(originalQueueName) || isMessageOwner(message))
                        messages.add(message);
                } else if (browseAllMessagesAccess || isMessageOwner(message)) {
                    messages.add(message);
                }
            }
            // Close the QueueBrowser
            qb.close();
        } else {
            if (log.isDebugEnabled())
                log.debug("No browseQueue access to " + queueName);
        }
        // Returns an array of Messages from the QueueBrowser's enumeration
        Message[] output = messages.toArray(new Message[messages.size()]);
        if (log.isDebugEnabled()) {
            StringBuilder buf = new StringBuilder("<output>");
            for (Message m : output) buf.append("<message>").append(m).append("</message>");
            buf.append("</output>");
            log.debug(output.length + " messages are being returned: " + buf.toString());
        }
        return output;
    } catch (InvalidSelectorException e) {
        if (log.isDebugEnabled())
            log.debug("Invalid filter: \"" + filter + '"', e);
        throw new ApplicationExceptions(new JaffaMessagingApplicationException(JaffaMessagingApplicationException.INVALID_SELECTOR));
    } catch (JMSException e) {
        log.error("Error in reading JMS Messages", e);
        throw new JaffaMessagingFrameworkException(JaffaMessagingFrameworkException.BROWSE_ERROR, new Object[] { queueName }, e);
    } finally {
        if (session != null) {
            try {
                session.close();
            } catch (JMSException e) {
                log.warn("Error in closing a JMS Session", e);
            }
        }
    }
}
Also used : QueueInfo(org.jaffa.modules.messaging.services.configdomain.QueueInfo) Enumeration(java.util.Enumeration) InvalidSelectorException(javax.jms.InvalidSelectorException) ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) LinkedList(java.util.LinkedList) QueueBrowser(javax.jms.QueueBrowser) Session(javax.jms.Session)

Aggregations

QueueInfo (org.jaffa.modules.messaging.services.configdomain.QueueInfo)7 LinkedList (java.util.LinkedList)4 JMSException (javax.jms.JMSException)4 TextMessage (javax.jms.TextMessage)4 Enumeration (java.util.Enumeration)3 Message (javax.jms.Message)3 DisplayParam (org.jaffa.modules.messaging.services.configdomain.DisplayParam)3 IntrospectionException (java.beans.IntrospectionException)2 LinkedHashMap (java.util.LinkedHashMap)2 DateTime (org.jaffa.datatypes.DateTime)2 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)2 BeanInfo (java.beans.BeanInfo)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 TreeMap (java.util.TreeMap)1 Connection (javax.jms.Connection)1 InvalidSelectorException (javax.jms.InvalidSelectorException)1 QueueBrowser (javax.jms.QueueBrowser)1 Session (javax.jms.Session)1 FrameworkException (org.jaffa.exceptions.FrameworkException)1 HeaderElementDto (org.jaffa.modules.messaging.components.messageviewer.dto.HeaderElementDto)1