Search in sources :

Example 21 with QmfConsoleData

use of org.apache.qpid.qmf2.console.QmfConsoleData in project qpid by apache.

the class BrokerSubscriptionTestConsole method onEvent.

public void onEvent(WorkItem wi) {
    System.out.println("WorkItem type: " + wi.getType());
    if (wi.getType() == AGENT_HEARTBEAT) {
        AgentHeartbeatWorkItem item = (AgentHeartbeatWorkItem) wi;
        Agent agent = item.getAgent();
        System.out.println(agent.getName());
    }
    if (wi.getType() == EVENT_RECEIVED) {
        EventReceivedWorkItem item = (EventReceivedWorkItem) wi;
        Agent agent = item.getAgent();
        QmfEvent event = item.getEvent();
        String className = event.getSchemaClassId().getClassName();
        System.out.println("Event: " + className);
    // event.listValues();
    }
    if (wi.getType() == METHOD_RESPONSE) {
        MethodResponseWorkItem item = (MethodResponseWorkItem) wi;
        MethodResult result = item.getMethodResult();
        String correlationId = item.getHandle().getCorrelationId();
        System.out.println("correlationId = " + correlationId);
        System.out.println(result.getStringValue("message"));
    }
    if (wi.getType() == OBJECT_UPDATE) {
        ObjectUpdateWorkItem item = (ObjectUpdateWorkItem) wi;
        QmfConsoleData object = item.getQmfConsoleData();
        ObjectId objectId = object.getObjectId();
        String correlationId = item.getHandle().getCorrelationId();
        System.out.println("correlationId = " + correlationId);
        System.out.println("objectId = " + objectId);
        System.out.println("MethodCount = " + object.getLongValue("methodCount"));
    }
    if (wi.getType() == SUBSCRIBE_RESPONSE) {
        SubscribeResponseWorkItem item = (SubscribeResponseWorkItem) wi;
        SubscribeParams params = item.getSubscribeParams();
        System.out.println("duration = " + params.getLifetime());
        System.out.println("interval = " + params.getPublishInterval());
        System.out.println("subscriptionId = " + params.getSubscriptionId());
        System.out.println("consoleHandle = " + params.getConsoleHandle());
        String correlationId = item.getHandle().getCorrelationId();
        System.out.println("correlationId = " + correlationId);
    }
    if (wi.getType() == SUBSCRIPTION_INDICATION) {
        SubscriptionIndicationWorkItem item = (SubscriptionIndicationWorkItem) wi;
        SubscribeIndication indication = item.getSubscribeIndication();
        String correlationId = indication.getConsoleHandle();
        System.out.println("correlationId = " + correlationId);
        List<QmfConsoleData> objects = indication.getData();
        for (QmfConsoleData object : objects) {
            if (object.isDeleted()) {
                System.out.println("object has been deleted");
            }
            String className = object.getSchemaClassId().getClassName();
            System.out.println("object class = " + className);
            if (className.equals("queue") || className.equals("exchange")) {
                if (object.hasValue("name")) {
                    System.out.println("property update, name = " + object.getStringValue("name"));
                } else {
                    _objectId = object.getObjectId();
                    System.out.println("statistic update, oid = " + _objectId);
                }
            }
        }
    }
}
Also used : Agent(org.apache.qpid.qmf2.console.Agent) SubscriptionIndicationWorkItem(org.apache.qpid.qmf2.console.SubscriptionIndicationWorkItem) ObjectId(org.apache.qpid.qmf2.common.ObjectId) AgentHeartbeatWorkItem(org.apache.qpid.qmf2.console.AgentHeartbeatWorkItem) EventReceivedWorkItem(org.apache.qpid.qmf2.console.EventReceivedWorkItem) QmfEvent(org.apache.qpid.qmf2.common.QmfEvent) MethodResponseWorkItem(org.apache.qpid.qmf2.console.MethodResponseWorkItem) SubscribeIndication(org.apache.qpid.qmf2.console.SubscribeIndication) ObjectUpdateWorkItem(org.apache.qpid.qmf2.console.ObjectUpdateWorkItem) SubscribeParams(org.apache.qpid.qmf2.console.SubscribeParams) SubscribeResponseWorkItem(org.apache.qpid.qmf2.console.SubscribeResponseWorkItem) QmfConsoleData(org.apache.qpid.qmf2.console.QmfConsoleData) MethodResult(org.apache.qpid.qmf2.console.MethodResult)

Example 22 with QmfConsoleData

use of org.apache.qpid.qmf2.console.QmfConsoleData in project qpid by apache.

the class QpidQueueStats method onEvent.

/**
 * Main Event handler. Checks if the WorkItem is a SubscriptionIndicationWorkItem, if it is it stores the object
 * in a Map and uses this to maintain state so we can record deltas such as enqueue and dequeue rates.
 * <p>
 * The AgentHeartbeatWorkItem is used to periodically compare the elapsed time against the Subscription duration
 * so that we can refresh the Subscription (or create a new one if necessary) in order to continue receiving
 * queue Management Object data from the broker.
 * <p>
 * When the AgentRestartedWorkItem is received we clear the state to remove any stale queue Management Objects.
 * @param wi a QMF2 WorkItem object
 */
public void onEvent(final WorkItem wi) {
    if (wi instanceof AgentHeartbeatWorkItem && _subscriptionId != null) {
        long elapsed = (long) Math.round((System.currentTimeMillis() - _startTime) / 1000.0f);
        if (elapsed > _subscriptionDuration) {
            try {
                _console.refreshSubscription(_subscriptionId);
                _startTime = System.currentTimeMillis();
            } catch (QmfException qmfe) {
                System.err.println("QmfException " + qmfe.getMessage() + " caught in QpidQueueStats onEvent");
                createQueueSubscription();
            }
        }
    } else if (wi instanceof AgentRestartedWorkItem) {
        _objects.clear();
    } else if (wi instanceof SubscriptionIndicationWorkItem) {
        SubscriptionIndicationWorkItem item = (SubscriptionIndicationWorkItem) wi;
        SubscribeIndication indication = item.getSubscribeIndication();
        String correlationId = indication.getConsoleHandle();
        if (correlationId.equals("queueStatsHandle")) {
            // If it is (and it should be!!) then it's our queue object Subscription
            List<QmfConsoleData> data = indication.getData();
            for (QmfConsoleData record : data) {
                ObjectId id = record.getObjectId();
                if (record.isDeleted()) {
                    // If the object was deleted by the Agent we remove it from out Map
                    _objects.remove(id);
                } else {
                    if (_objects.containsKey(id)) {
                        // If the object is already in the Map it's likely to be a statistics push from the broker.
                        Stats stats = _objects.get(id);
                        String name = stats.getName();
                        boolean matches = false;
                        for (Pattern x : _filter) {
                            // Check the queue name against the regexes in the filter List (if any)
                            Matcher m = x.matcher(name);
                            if (m.find()) {
                                matches = true;
                                break;
                            }
                        }
                        if (_filter.isEmpty() || matches) {
                            // If there's no filter enabled or the filter matches the queue name we display statistics.
                            QmfConsoleData lastSample = stats.getData();
                            stats.setData(record);
                            float deltaTime = record.getUpdateTime() - lastSample.getUpdateTime();
                            if (deltaTime > 1000000000.0f) {
                                float deltaEnqueues = record.getLongValue("msgTotalEnqueues") - lastSample.getLongValue("msgTotalEnqueues");
                                float deltaDequeues = record.getLongValue("msgTotalDequeues") - lastSample.getLongValue("msgTotalDequeues");
                                long msgDepth = record.getLongValue("msgDepth");
                                float enqueueRate = deltaEnqueues / (deltaTime / 1000000000.0f);
                                float dequeueRate = deltaDequeues / (deltaTime / 1000000000.0f);
                                System.out.printf("%-46s%10.2f%11d%13.2f%13.2f\n", name, deltaTime / 1000000000, msgDepth, enqueueRate, dequeueRate);
                            }
                        }
                    } else {
                        // If the object isn't in the Map it's likely to be a properties push from the broker.
                        if (!record.hasValue("name")) {
                            // This probably won't happen, but if it does we refresh the object to get its full state.
                            try {
                                record.refresh();
                            } catch (QmfException qmfe) {
                            }
                        }
                        String queueName = record.getStringValue("name");
                        _objects.put(id, new Stats(queueName, record));
                    }
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) AgentRestartedWorkItem(org.apache.qpid.qmf2.console.AgentRestartedWorkItem) SubscriptionIndicationWorkItem(org.apache.qpid.qmf2.console.SubscriptionIndicationWorkItem) ObjectId(org.apache.qpid.qmf2.common.ObjectId) Matcher(java.util.regex.Matcher) AgentHeartbeatWorkItem(org.apache.qpid.qmf2.console.AgentHeartbeatWorkItem) SubscribeIndication(org.apache.qpid.qmf2.console.SubscribeIndication) QmfConsoleData(org.apache.qpid.qmf2.console.QmfConsoleData) QmfException(org.apache.qpid.qmf2.common.QmfException)

Example 23 with QmfConsoleData

use of org.apache.qpid.qmf2.console.QmfConsoleData in project qpid by apache.

the class QueueFuse method updateQueueCache.

/**
 * Looks up queue objects and stores them in _queueCache keyed by the queue name
 */
private void updateQueueCache() {
    _queueCache.clear();
    List<QmfConsoleData> queues = _console.getObjects("org.apache.qpid.broker", "queue");
    for (QmfConsoleData queue : queues) {
        String queueName = queue.getStringValue("name");
        _queueCache.put(queueName, queue);
    }
}
Also used : QmfConsoleData(org.apache.qpid.qmf2.console.QmfConsoleData)

Example 24 with QmfConsoleData

use of org.apache.qpid.qmf2.console.QmfConsoleData in project qpid by apache.

the class Console method getObjects.

/**
 * Perform a query for QmfConsoleData objects. Returns a list (possibly empty) of matching objects.
 * If replyHandle is null this method will block until the agent replies, or the timeout expires.
 * Once the timeout expires, all data retrieved to date is returned. If replyHandle is non-null an
 * asynchronous request is performed
 *
 * @param agent the Agent being queried
 * @param query the ObjectId or SchemaClassId being queried for.
 * @param replyHandle the correlation handle used to tie asynchronous method requests with responses
 * @param timeout the time to wait for a reply from the Agent, a value of -1 means use the default timeout
 * @return a List of QMF Objects describing that class
 */
private List<QmfConsoleData> getObjects(final Agent agent, final QmfData query, final String replyHandle, int timeout) {
    String agentName = agent.getName();
    timeout = (timeout < 1) ? _replyTimeout : timeout;
    List<QmfConsoleData> results = Collections.emptyList();
    try {
        Destination destination = (replyHandle == null) ? _replyAddress : _asyncReplyAddress;
        MapMessage request = _syncSession.createMapMessage();
        request.setJMSReplyTo(destination);
        request.setJMSCorrelationID(replyHandle);
        request.setStringProperty("x-amqp-0-10.app-id", "qmf2");
        request.setStringProperty("method", "request");
        request.setStringProperty("qmf.opcode", "_query_request");
        request.setStringProperty("qpid.subject", agentName);
        // Create a QMF Query for an "OBJECT" target using either a schema ID or object ID
        String queryType = (query instanceof SchemaClassId) ? "_schema_id" : "_object_id";
        request.setObject("_what", "OBJECT");
        request.setObject(queryType, query.mapEncode());
        // it would be somewhat unfortunate if their response got interleaved with ours!!
        synchronized (this) {
            _requester.send(request);
            if (replyHandle == null) {
                boolean lastResult = true;
                ArrayList<QmfConsoleData> partials = new ArrayList<QmfConsoleData>();
                do {
                    // Wrap in a do/while loop to cater for the case where the Agent may send partial results.
                    Message response = _responder.receive(timeout * 1000);
                    if (response == null) {
                        _log.info("No response received in getObjects()");
                        return partials;
                    }
                    lastResult = !response.propertyExists("partial");
                    if (AMQPMessage.isAMQPList(response)) {
                        List<Map> mapResults = AMQPMessage.getList(response);
                        partials.ensureCapacity(partials.size() + mapResults.size());
                        for (Map content : mapResults) {
                            partials.add(new QmfConsoleData(content, agent));
                        }
                    } else if (AMQPMessage.isAMQPMap(response)) {
                    // Error responses are returned as MapMessages, though they are being ignored here.
                    // QmfData exception = new QmfData(AMQPMessage.getMap(response));
                    // System.out.println(agentName + " " + exception.getStringValue("error_text"));
                    } else {
                        _log.info("getObjects() Received response message in incorrect format");
                    }
                } while (!lastResult);
                results = partials;
            }
        }
    } catch (JMSException jmse) {
        _log.info("JMSException {} caught in getObjects()", jmse.getMessage());
    }
    return results;
}
Also used : Destination(javax.jms.Destination) AMQPMessage(org.apache.qpid.qmf2.common.AMQPMessage) MapMessage(javax.jms.MapMessage) Message(javax.jms.Message) MapMessage(javax.jms.MapMessage) ArrayList(java.util.ArrayList) JMSException(javax.jms.JMSException) SchemaClassId(org.apache.qpid.qmf2.common.SchemaClassId) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

QmfConsoleData (org.apache.qpid.qmf2.console.QmfConsoleData)20 ObjectId (org.apache.qpid.qmf2.common.ObjectId)16 Map (java.util.Map)11 HashMap (java.util.HashMap)10 QmfException (org.apache.qpid.qmf2.common.QmfException)5 Agent (org.apache.qpid.qmf2.console.Agent)5 Date (java.util.Date)4 QmfData (org.apache.qpid.qmf2.common.QmfData)4 QmfEvent (org.apache.qpid.qmf2.common.QmfEvent)4 SchemaClassId (org.apache.qpid.qmf2.common.SchemaClassId)4 AgentHeartbeatWorkItem (org.apache.qpid.qmf2.console.AgentHeartbeatWorkItem)4 MethodResult (org.apache.qpid.qmf2.console.MethodResult)4 ArrayList (java.util.ArrayList)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 EventReceivedWorkItem (org.apache.qpid.qmf2.console.EventReceivedWorkItem)3 MethodResponseWorkItem (org.apache.qpid.qmf2.console.MethodResponseWorkItem)3 ObjectUpdateWorkItem (org.apache.qpid.qmf2.console.ObjectUpdateWorkItem)3 SubscribeIndication (org.apache.qpid.qmf2.console.SubscribeIndication)3 SubscriptionIndicationWorkItem (org.apache.qpid.qmf2.console.SubscriptionIndicationWorkItem)3 IOException (java.io.IOException)2