Search in sources :

Example 1 with QueueCriteria

use of org.jaffa.qm.apis.data.QueueCriteria in project jaffa-framework by jaffa-projects.

the class QueueManager method messageQuery.

public MessageQueryResponse messageQuery(MessageCriteria criteria) throws ApplicationExceptions {
    try {
        if (log.isDebugEnabled())
            log.debug("Input to messageQuery: " + criteria);
        MessageQueryResponse aggregateResponse = new MessageQueryResponse();
        IQueueAdmin[] implementations = findImplementations();
        // Find which implementation of queue admin can be used to query messages for supplied queue
        IQueueAdmin queryImplementation = null;
        boolean foundMultiple = false;
        if (implementations != null && implementations.length > 0) {
            for (IQueueAdmin implementation : implementations) {
                QueueCriteria queueCriteria = new QueueCriteria();
                queueCriteria.setType(criteria.getType());
                queueCriteria.setQueueSystemId(criteria.getQueueSystemId());
                QueueQueryResponse queueResponse = implementation.queueQuery(queueCriteria);
                if (queueResponse != null && queueResponse.getGraphs() != null && queueResponse.getGraphs().length == 1) {
                    if (queryImplementation != null)
                        foundMultiple = true;
                    queryImplementation = implementation;
                } else if (queueResponse.getGraphs() != null && queueResponse.getGraphs().length > 1) {
                    foundMultiple = true;
                }
            }
        }
        if (foundMultiple) {
            log.error("Found multiple IQueueAdmin implementations for message query");
            throw new ApplicationExceptions(new QueueAdminException(QueueAdminException.MULTIPLE_QUEUES));
        }
        // Use correct queue admin to retrieve messages. Admin class should perform sorting and paging.
        if (queryImplementation != null) {
            MessageQueryResponse response = queryImplementation.messageQuery(criteria);
            if (response != null && response.getErrors() != null && response.getErrors().length > 0) {
                aggregateResponse.setErrors(response.getErrors());
            } else if (response != null && response.getGraphs() != null && response.getGraphs().length > 0) {
                aggregateResponse.setGraphs(concatenate(aggregateResponse.getGraphs(), response.getGraphs()));
            }
            if (response.getTotalRecords() != null && response.getTotalRecords() > 0)
                aggregateResponse.setTotalRecords(response.getTotalRecords());
        }
        if (aggregateResponse.getGraphs() == null)
            aggregateResponse.setGraphs(new MessageGraph[0]);
        if (log.isDebugEnabled())
            log.debug("Response from messageQuery: " + aggregateResponse);
        return aggregateResponse;
    } catch (Exception e) {
        log.error("Error in obtaining IQueueAdmin implementations", e);
        return new MessageQueryResponse(null, ServiceError.generate(e));
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) QueueCriteria(org.jaffa.qm.apis.data.QueueCriteria) QueueQueryResponse(org.jaffa.qm.apis.data.QueueQueryResponse) MessageGraph(org.jaffa.qm.apis.data.MessageGraph) MessageQueryResponse(org.jaffa.qm.apis.data.MessageQueryResponse)

Aggregations

ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)1 MessageGraph (org.jaffa.qm.apis.data.MessageGraph)1 MessageQueryResponse (org.jaffa.qm.apis.data.MessageQueryResponse)1 QueueCriteria (org.jaffa.qm.apis.data.QueueCriteria)1 QueueQueryResponse (org.jaffa.qm.apis.data.QueueQueryResponse)1