Search in sources :

Example 21 with Agent

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

the class Agent method handleQueryRequest.

/**
     * Handle the query request and send the response back to the Console.
     * @param handle the reply handle that contains the replyTo Address.
     * @param query the inbound query from the Console.
     */
@SuppressWarnings("unchecked")
private final void handleQueryRequest(final Handle handle, final QmfQuery query) {
    QmfQueryTarget target = query.getTarget();
    if (target == QmfQueryTarget.SCHEMA_ID) {
        List<Map> results = new ArrayList<Map>(_schemaCache.size());
        // Look up all SchemaClassId objects
        for (SchemaClassId classId : _schemaCache.keySet()) {
            results.add(classId.mapEncode());
        }
        // Send the response back to the Console.
        queryResponse(handle, results, "_schema_id");
    } else if (target == QmfQueryTarget.SCHEMA) {
        List<Map> results = new ArrayList<Map>(1);
        // Look up a SchemaClass object by the SchemaClassId obtained from the query
        SchemaClassId classId = query.getSchemaClassId();
        SchemaClass schema = _schemaCache.get(classId);
        if (schema != null) {
            results.add(schema.mapEncode());
        }
        // Send the response back to the Console.
        queryResponse(handle, results, "_schema");
    } else if (target == QmfQueryTarget.OBJECT_ID) {
        List<Map> results = new ArrayList<Map>(_objectIndex.size());
        // Look up all ObjectId objects
        for (ObjectId objectId : _objectIndex.keySet()) {
            results.add(objectId.mapEncode());
        }
        // Send the response back to the Console.
        queryResponse(handle, results, "_object_id");
    } else if (target == QmfQueryTarget.OBJECT) {
        // If this is implementing the AgentExternal model we pass the QmfQuery on in a QueryWorkItem
        if (this instanceof AgentExternal) {
            _eventListener.onEvent(new QueryWorkItem(handle, query));
            return;
        } else {
            //qmfContentType = "_data";
            if (query.getObjectId() != null) {
                List<Map> results = new ArrayList<Map>(1);
                // Look up a QmfAgentData object by the ObjectId obtained from the query
                ObjectId objectId = query.getObjectId();
                QmfAgentData object = _objectIndex.get(objectId);
                if (object != null && !object.isDeleted()) {
                    results.add(object.mapEncode());
                }
                // Send the response back to the Console.
                queryResponse(handle, results, "_data");
            } else {
                // Look up QmfAgentData objects by the SchemaClassId obtained from the query
                // This is implemented by a linear search and allows searches with only the className specified.
                // Linear searches clearly don't scale brilliantly, but the number of QmfAgentData objects managed
                // by an Agent is generally fairly small, so it should be OK. Note that this is the same approach
                // taken by the C++ broker ManagementAgent, so if it's a problem here........
                // N.B. the results list declared here is a generic List of Objects. We *must* only pass a List of
                // Map to queryResponse(), but conversely if the response items are sortable we need to sort them
                // before doing mapEncode(). Unfortunately we don't know if the items are sortable a priori so
                // we either add a Map or we add a QmfAgentData, then sort then mapEncode() each item. I'm not
                // sure of a more elegant way to do this without creating two lists, which might not be so bad
                // but we don't know the size of the list a priori either.
                List results = new ArrayList(_objectIndex.size());
                // It's unlikely that evaluating this query will return a mixture of sortable and notSortable 
                // QmfAgentData objects, but it's best to check if that has occurred as accidentally passing a
                // List of QmfAgentData instead of a List of Map to queryResponse() will break things.
                boolean sortable = false;
                boolean notSortable = false;
                for (QmfAgentData object : _objectIndex.values()) {
                    if (!object.isDeleted() && query.evaluate(object)) {
                        if (object.isSortable()) {
                            // If QmfAgentData is marked sortable we add the QmfAgentData object to the List
                            // so we can sort first before mapEncoding.
                            results.add(object);
                            sortable = true;
                        } else {
                            // If QmfAgentData is not marked sortable we mapEncode immediately and add the Map to List.
                            results.add(object.mapEncode());
                            notSortable = true;
                        }
                    }
                }
                // results List to avoid sending unconvertable data. Hopefully this condition should never occur.
                if (sortable && notSortable) {
                    _log.info("Query resulted in inconsistent mixture of sortable and non-sortable data.");
                    results.clear();
                } else if (sortable) {
                    Collections.sort(results);
                    int length = results.size();
                    for (int i = 0; i < length; i++) {
                        QmfAgentData object = (QmfAgentData) results.get(i);
                        results.set(i, object.mapEncode());
                    }
                }
                // Send the response back to the Console.
                queryResponse(handle, results, "_data");
            }
        }
    } else {
        raiseException(handle, "Query for _what => '" + target + "' not supported");
        return;
    }
}
Also used : ObjectId(org.apache.qpid.qmf2.common.ObjectId) ArrayList(java.util.ArrayList) QmfQueryTarget(org.apache.qpid.qmf2.common.QmfQueryTarget) SchemaClassId(org.apache.qpid.qmf2.common.SchemaClassId) SchemaClass(org.apache.qpid.qmf2.common.SchemaClass) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 22 with Agent

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

the class Agent method registerEventClass.

/**
     * Register a schema for an event class with the Agent.
     * <p>
     * The Agent must have a registered schema for an event class before it can handle events of that class.
     *
     * @param schema the SchemaEventClass to be registered
     */
public final void registerEventClass(final SchemaEventClass schema) {
    SchemaClassId classId = schema.getClassId();
    _schemaCache.put(classId, schema);
}
Also used : SchemaClassId(org.apache.qpid.qmf2.common.SchemaClassId)

Example 23 with Agent

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

the class Agent method removeConnection.

// end of setConnection()
/**
     * Remove the AMQP connection from the Agent. Un-does the setConnection() operation.
     *
     * @param conn a javax.jms.Connection.
     */
public final void removeConnection(final Connection conn) throws QmfException {
    if (conn != _connection) {
        throw new QmfException("Attempt to delete unknown connection");
    }
    try {
        _timer.cancel();
        _connection.close();
    } catch (JMSException jmse) {
        throw new QmfException("Failed to remove connection, caught JMSException " + jmse.getMessage());
    }
    _connection = null;
}
Also used : JMSException(javax.jms.JMSException) QmfException(org.apache.qpid.qmf2.common.QmfException)

Example 24 with Agent

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

the class Agent method addObject.

/**
     * Passes a reference to an instance of a managed QMF object to the Agent.
     * <p>
     * The object's name must uniquely identify this object among all objects known to this Agent.
     * <p>
     * This method creates an ObjectId for the QmfAgentData being added, it does this by first checking
     * the schema.
     * <p>
     * If an associated schema exists we look for the set of property names that have been
     * specified as idNames. If idNames exists we look for their values within the object and use that
     * to create the objectName. If we can't create a sensible name we use a randomUUID.
     * @param object the QmfAgentData object to be added
     */
public void addObject(final QmfAgentData object) throws QmfException {
    // There are some cases where a QmfAgentData Object might have already set its ObjectId, for example where
    // it may need to have a "well known" ObjectId. This is the case with the Java Broker Management Agent
    // where tools such as qpid-config might have made assumptions about its ObjectId rather than doing "discovery".
    ObjectId addr = object.getObjectId();
    if (addr == null) {
        SchemaClassId classId = object.getSchemaClassId();
        SchemaClass schema = _schemaCache.get(classId);
        // Try to create an objectName using the property names that have been specified as idNames in the schema
        StringBuilder buf = new StringBuilder();
        // Initialise idNames as an empty array as we want to check if a key has been used to construct the name.
        String[] idNames = {};
        if (schema != null && schema instanceof SchemaObjectClass) {
            idNames = ((SchemaObjectClass) schema).getIdNames();
            for (String property : idNames) {
                buf.append(object.getStringValue(property));
            }
        }
        String objectName = buf.toString();
        // exchange has name == "")
        if (objectName.length() == 0 && idNames.length == 0)
            objectName = UUID.randomUUID().toString();
        // Finish up the name by incorporating package and class names
        objectName = classId.getPackageName() + ":" + classId.getClassName() + ":" + objectName;
        // Now we've got a good name for the object we create its ObjectId and add that to the object
        addr = new ObjectId(_name, objectName, _epoch);
        object.setObjectId(addr);
    }
    QmfAgentData foundObject = _objectIndex.get(addr);
    if (foundObject != null) {
        // If a duplicate object has actually been Deleted we can reuse the address.
        if (!foundObject.isDeleted()) {
            throw new QmfException("Duplicate QmfAgentData Address");
        }
    }
    _objectIndex.put(addr, object);
    // Does the new object match any Subscriptions? If so add a reference to the matching Subscription and publish.
    for (Subscription subscription : _subscriptions.values()) {
        QmfQuery query = subscription.getQuery();
        if (query.getObjectId() != null) {
            if (query.getObjectId().equals(addr)) {
                object.addSubscription(subscription.getSubscriptionId(), subscription);
                object.publish();
            }
        } else if (query.evaluate(object)) {
            object.addSubscription(subscription.getSubscriptionId(), subscription);
            object.publish();
        }
    }
}
Also used : ObjectId(org.apache.qpid.qmf2.common.ObjectId) SchemaClass(org.apache.qpid.qmf2.common.SchemaClass) SchemaObjectClass(org.apache.qpid.qmf2.common.SchemaObjectClass) SchemaClassId(org.apache.qpid.qmf2.common.SchemaClassId) QmfQuery(org.apache.qpid.qmf2.common.QmfQuery) QmfException(org.apache.qpid.qmf2.common.QmfException)

Example 25 with Agent

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

the class AgentSubscriptionTestConsole 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");
            }
            System.out.println("offset = " + object.getValue("offset"));
        }
    }
}
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)

Aggregations

QmfException (org.apache.qpid.qmf2.common.QmfException)14 ObjectId (org.apache.qpid.qmf2.common.ObjectId)13 JMSException (javax.jms.JMSException)12 Map (java.util.Map)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)10 HashMap (java.util.HashMap)9 SchemaClassId (org.apache.qpid.qmf2.common.SchemaClassId)9 Agent (org.apache.qpid.qmf2.console.Agent)9 MapMessage (javax.jms.MapMessage)8 QmfEvent (org.apache.qpid.qmf2.common.QmfEvent)8 ArrayList (java.util.ArrayList)6 Message (javax.jms.Message)6 QmfAgentData (org.apache.qpid.qmf2.agent.QmfAgentData)6 AMQPMessage (org.apache.qpid.qmf2.common.AMQPMessage)6 QmfData (org.apache.qpid.qmf2.common.QmfData)6 SchemaClass (org.apache.qpid.qmf2.common.SchemaClass)6 AgentHeartbeatWorkItem (org.apache.qpid.qmf2.console.AgentHeartbeatWorkItem)6 Destination (javax.jms.Destination)5 SchemaObjectClass (org.apache.qpid.qmf2.common.SchemaObjectClass)5 EventReceivedWorkItem (org.apache.qpid.qmf2.console.EventReceivedWorkItem)5