Search in sources :

Example 1 with MethodCallWorkItem

use of org.apache.qpid.qmf2.agent.MethodCallWorkItem in project qpid by apache.

the class Agent method onMessage.

//                                          MessageListener
// ********************************************************************************************************
/**
     * MessageListener for QMF2 Console requests.
     *
     * @param message the JMS Message passed to the listener.
     */
public final void onMessage(final Message message) {
    try {
        String agentName = QmfData.getString(message.getObjectProperty("qmf.agent"));
        String content = QmfData.getString(message.getObjectProperty("qmf.content"));
        String opcode = QmfData.getString(message.getObjectProperty("qmf.opcode"));
        //String routingKey = ((javax.jms.Topic)message.getJMSDestination()).getTopicName();
        //String contentType = ((org.apache.qpid.client.message.AbstractJMSMessage)message).getContentType();
        //System.out.println();
        //System.out.println("agentName = " + agentName);
        //System.out.println("content = " + content);
        //System.out.println("opcode = " + opcode);
        //System.out.println("routingKey = " + routingKey);
        //System.out.println("contentType = " + contentType);
        Handle handle = new Handle(message.getJMSCorrelationID(), message.getJMSReplyTo());
        if (opcode.equals("_agent_locate_request")) {
            handleLocateRequest(handle);
        } else if (opcode.equals("_method_request")) {
            if (AMQPMessage.isAMQPMap(message)) {
                _eventListener.onEvent(new MethodCallWorkItem(handle, new MethodCallParams(AMQPMessage.getMap(message))));
            } else {
                _log.info("onMessage() Received Method Request message in incorrect format");
            }
        } else if (opcode.equals("_query_request")) {
            if (AMQPMessage.isAMQPMap(message)) {
                try {
                    QmfQuery query = new QmfQuery(AMQPMessage.getMap(message));
                    handleQueryRequest(handle, query);
                } catch (QmfException qmfe) {
                    raiseException(handle, "Query Request failed, invalid Query: " + qmfe.getMessage());
                }
            } else {
                _log.info("onMessage() Received Query Request message in incorrect format");
            }
        } else if (opcode.equals("_subscribe_request")) {
            if (AMQPMessage.isAMQPMap(message)) {
                try {
                    SubscriptionParams subscriptionParams = new SubscriptionParams(handle, AMQPMessage.getMap(message));
                    if (this instanceof AgentExternal) {
                        _eventListener.onEvent(new SubscribeRequestWorkItem(handle, subscriptionParams));
                    } else {
                        Subscription subscription = new Subscription(this, subscriptionParams);
                        String subscriptionId = subscription.getSubscriptionId();
                        _subscriptions.put(subscriptionId, subscription);
                        _timer.schedule(subscription, 0, subscriptionParams.getPublishInterval());
                        subscriptionResponse(handle, subscription.getConsoleHandle(), subscriptionId, subscription.getDuration(), subscription.getInterval(), null);
                    }
                } catch (QmfException qmfe) {
                    raiseException(handle, "Subscribe Request failed, invalid Query: " + qmfe.getMessage());
                }
            } else {
                _log.info("onMessage() Received Subscribe Request message in incorrect format");
            }
        } else if (opcode.equals("_subscribe_refresh_indication")) {
            if (AMQPMessage.isAMQPMap(message)) {
                ResubscribeParams resubscribeParams = new ResubscribeParams(AMQPMessage.getMap(message));
                if (this instanceof AgentExternal) {
                    _eventListener.onEvent(new ResubscribeRequestWorkItem(handle, resubscribeParams));
                } else {
                    String subscriptionId = resubscribeParams.getSubscriptionId();
                    Subscription subscription = _subscriptions.get(subscriptionId);
                    if (subscription != null) {
                        subscription.refresh(resubscribeParams);
                        subscriptionResponse(handle, subscription.getConsoleHandle(), subscription.getSubscriptionId(), subscription.getDuration(), subscription.getInterval(), null);
                    }
                }
            } else {
                _log.info("onMessage() Received Resubscribe Request message in incorrect format");
            }
        } else if (opcode.equals("_subscribe_cancel_indication")) {
            if (AMQPMessage.isAMQPMap(message)) {
                QmfData qmfSubscribe = new QmfData(AMQPMessage.getMap(message));
                String subscriptionId = qmfSubscribe.getStringValue("_subscription_id");
                if (this instanceof AgentExternal) {
                    _eventListener.onEvent(new UnsubscribeRequestWorkItem(subscriptionId));
                } else {
                    Subscription subscription = _subscriptions.get(subscriptionId);
                    if (subscription != null) {
                        subscription.cancel();
                    }
                }
            } else {
                _log.info("onMessage() Received Subscribe Cancel Request message in incorrect format");
            }
        }
    } catch (JMSException jmse) {
        _log.info("JMSException {} caught in onMessage()", jmse.getMessage());
    }
}
Also used : QmfData(org.apache.qpid.qmf2.common.QmfData) JMSException(javax.jms.JMSException) Handle(org.apache.qpid.qmf2.common.Handle) QmfQuery(org.apache.qpid.qmf2.common.QmfQuery) QmfException(org.apache.qpid.qmf2.common.QmfException)

Example 2 with MethodCallWorkItem

use of org.apache.qpid.qmf2.agent.MethodCallWorkItem in project qpid by apache.

the class JSON method fromWorkItem.

/**
     * Serialise a WorkItem Object to JSON.
     * @param data the WorkItem that we wish to serialise to JSON.
     * @return the JSON String encoding.
     */
public static final String fromWorkItem(final WorkItem data) {
    // TODO There are a couple of WorkItem types that won't serialise correctly - SubscriptionIndicationWorkItem
    // and MethodCallWorkItem. Their params require a custom serialiser - though they probably won't be used
    // from a REST API so they've been parked for now.
    String type = "\"_type\":\"" + data.getType() + "\",";
    Handle handle = data.getHandle();
    String handleString = handle == null ? "" : "\"_handle\":\"" + handle.getCorrelationId() + "\",";
    String params = "\"_params\":" + fromObject(data.getParams());
    return "{" + type + handleString + params + "}";
}
Also used : Handle(org.apache.qpid.qmf2.common.Handle)

Example 3 with MethodCallWorkItem

use of org.apache.qpid.qmf2.agent.MethodCallWorkItem in project qpid by apache.

the class QmfManagementAgent method onEvent.

// ******************************* QmfEventListener implementation method *******************************
/**
     * Callback method triggered when the underlying QMF2 Agent has WorkItems available for processing.
     * The purpose of this method is mainly to handle the METHOD_CALL WorkItem and demultiplex & delegate
     * to the invokeMethod() call on the relevant concrete QmfAgentData Object.
     * @param wi the WorkItem that has been passed by the QMF2 Agent to be processed here (mainly METHOD_CALL).
     */
@Override
public void onEvent(final WorkItem wi) {
    if (wi.getType() == METHOD_CALL) {
        MethodCallWorkItem item = (MethodCallWorkItem) wi;
        MethodCallParams methodCallParams = item.getMethodCallParams();
        String methodName = methodCallParams.getName();
        ObjectId objectId = methodCallParams.getObjectId();
        // Look up QmfAgentData by ObjectId from the Agent's internal Object store.
        QmfAgentData object = _agent.getObject(objectId);
        if (object == null) {
            _agent.raiseException(item.getHandle(), "No object found with ID=" + objectId);
        } else {
            // other classes yet.
            if (object instanceof org.apache.qpid.server.qmf2.agentdata.Broker) {
                org.apache.qpid.server.qmf2.agentdata.Broker broker = (org.apache.qpid.server.qmf2.agentdata.Broker) object;
                broker.invokeMethod(_agent, item.getHandle(), methodName, methodCallParams.getArgs());
            } else if (object instanceof org.apache.qpid.server.qmf2.agentdata.Queue) {
                org.apache.qpid.server.qmf2.agentdata.Queue queue = (org.apache.qpid.server.qmf2.agentdata.Queue) object;
                queue.invokeMethod(_agent, item.getHandle(), methodName, methodCallParams.getArgs());
            } else {
                _agent.raiseException(item.getHandle(), "Unknown Method " + methodName + " on " + object.getClass().getSimpleName());
            }
        }
    }
}
Also used : MethodCallWorkItem(org.apache.qpid.qmf2.agent.MethodCallWorkItem) Broker(org.apache.qpid.server.model.Broker) ObjectId(org.apache.qpid.qmf2.common.ObjectId) MethodCallParams(org.apache.qpid.qmf2.agent.MethodCallParams) QmfAgentData(org.apache.qpid.qmf2.agent.QmfAgentData) Queue(org.apache.qpid.server.model.Queue)

Example 4 with MethodCallWorkItem

use of org.apache.qpid.qmf2.agent.MethodCallWorkItem in project qpid by apache.

the class AgentTest method onEvent.

public void onEvent(WorkItem wi) {
    System.out.println("WorkItem type: " + wi.getType());
    _control.incValue("methodCount", 1);
    if (wi.getType() == METHOD_CALL) {
        MethodCallWorkItem item = (MethodCallWorkItem) wi;
        MethodCallParams methodCallParams = item.getMethodCallParams();
        String methodName = methodCallParams.getName();
        ObjectId objectId = methodCallParams.getObjectId();
        String userId = methodCallParams.getUserId();
        userId = userId.equals("") ? "anonymous" : userId;
        QmfData inArgs = methodCallParams.getArgs();
        ObjectId controlAddress = _control.getObjectId();
        System.out.println("Method Call User ID = " + userId);
        try {
            if (objectId == null) {
                // Method invoked directly on Agent
                if (methodName.equals("toString")) {
                    QmfData outArgs = new QmfData();
                    outArgs.setValue("string", _agent.toString());
                    _agent.methodResponse(methodName, item.getHandle(), outArgs, null);
                }
            } else if (objectId.equals(controlAddress)) {
                if (methodName.equals("stop")) {
                    System.out.println("Invoked stop method");
                    String message = inArgs.getStringValue("message");
                    System.out.println("Stopping: message = " + message);
                    _agent.methodResponse(methodName, item.getHandle(), null, null);
                    _agent.destroy();
                    System.exit(1);
                } else if (methodName.equals("echo")) {
                    System.out.println("Invoked echo method");
                    _agent.methodResponse(methodName, item.getHandle(), inArgs, null);
                } else if (methodName.equals("event")) {
                    System.out.println("Invoked event method");
                    QmfEvent event = new QmfEvent(_eventSchema);
                    event.setSeverity((int) inArgs.getLongValue("severity"));
                    event.setValue("text", inArgs.getStringValue("text"));
                    _agent.raiseEvent(event);
                    _agent.methodResponse(methodName, item.getHandle(), null, null);
                } else if (methodName.equals("fail")) {
                    System.out.println("Invoked fail method");
                    QmfData error = new QmfData();
                    if (inArgs.getBooleanValue("useString")) {
                        error.setValue("error_text", inArgs.getStringValue("stringVal"));
                    } else {
                        error.setValue("whatHappened", "It Failed");
                        error.setValue("howBad", 75);
                        error.setValue("details", inArgs.getValue("details"));
                    }
                    _agent.methodResponse(methodName, item.getHandle(), null, error);
                } else if (methodName.equals("create_child")) {
                    System.out.println("Invoked create_child method");
                    String childName = inArgs.getStringValue("name");
                    System.out.println("childName = " + childName);
                    QmfAgentData child = new QmfAgentData(_childSchema);
                    child.setValue("name", childName);
                    _agent.addObject(child);
                    QmfData outArgs = new QmfData();
                    // Set subtype just to test
                    outArgs.setRefValue("childAddr", child.getObjectId(), "reference");
                    _agent.methodResponse(methodName, item.getHandle(), outArgs, null);
                }
            }
        } catch (QmfException qmfe) {
            System.err.println("QmfException " + qmfe.getMessage() + " caught: AgentTest failed");
            QmfData error = new QmfData();
            error.setValue("error_text", qmfe.getMessage());
            _agent.methodResponse(methodName, item.getHandle(), null, error);
        }
    }
}
Also used : MethodCallWorkItem(org.apache.qpid.qmf2.agent.MethodCallWorkItem) QmfData(org.apache.qpid.qmf2.common.QmfData) ObjectId(org.apache.qpid.qmf2.common.ObjectId) MethodCallParams(org.apache.qpid.qmf2.agent.MethodCallParams) QmfEvent(org.apache.qpid.qmf2.common.QmfEvent) QmfAgentData(org.apache.qpid.qmf2.agent.QmfAgentData) QmfException(org.apache.qpid.qmf2.common.QmfException)

Example 5 with MethodCallWorkItem

use of org.apache.qpid.qmf2.agent.MethodCallWorkItem in project qpid by apache.

the class BigPayloadAgentTest method onEvent.

public void onEvent(WorkItem wi) {
    System.out.println("WorkItem type: " + wi.getType());
    if (wi.getType() == METHOD_CALL) {
        MethodCallWorkItem item = (MethodCallWorkItem) wi;
        MethodCallParams methodCallParams = item.getMethodCallParams();
        String methodName = methodCallParams.getName();
        ObjectId objectId = methodCallParams.getObjectId();
        QmfData inArgs = methodCallParams.getArgs();
        ObjectId controlAddress = _control.getObjectId();
        if (objectId.equals(controlAddress)) {
            if (methodName.equals("processPayload")) {
                System.out.println("Invoked processPayload method");
                byte[] parameter = inArgs.getValue("parameter");
                System.out.println("payload size = " + parameter.length);
                QmfData outArgs = new QmfData();
                outArgs.setValue("return", parameter);
                _agent.methodResponse(methodName, item.getHandle(), outArgs, null);
            }
        }
    }
}
Also used : MethodCallWorkItem(org.apache.qpid.qmf2.agent.MethodCallWorkItem) QmfData(org.apache.qpid.qmf2.common.QmfData) ObjectId(org.apache.qpid.qmf2.common.ObjectId) MethodCallParams(org.apache.qpid.qmf2.agent.MethodCallParams)

Aggregations

MethodCallParams (org.apache.qpid.qmf2.agent.MethodCallParams)4 MethodCallWorkItem (org.apache.qpid.qmf2.agent.MethodCallWorkItem)4 ObjectId (org.apache.qpid.qmf2.common.ObjectId)4 QmfData (org.apache.qpid.qmf2.common.QmfData)4 QmfAgentData (org.apache.qpid.qmf2.agent.QmfAgentData)3 Handle (org.apache.qpid.qmf2.common.Handle)3 QmfException (org.apache.qpid.qmf2.common.QmfException)3 QmfEvent (org.apache.qpid.qmf2.common.QmfEvent)2 QmfQuery (org.apache.qpid.qmf2.common.QmfQuery)2 JMSException (javax.jms.JMSException)1 QueryWorkItem (org.apache.qpid.qmf2.agent.QueryWorkItem)1 ResubscribeParams (org.apache.qpid.qmf2.agent.ResubscribeParams)1 ResubscribeRequestWorkItem (org.apache.qpid.qmf2.agent.ResubscribeRequestWorkItem)1 SubscribeRequestWorkItem (org.apache.qpid.qmf2.agent.SubscribeRequestWorkItem)1 Subscription (org.apache.qpid.qmf2.agent.Subscription)1 SubscriptionParams (org.apache.qpid.qmf2.agent.SubscriptionParams)1 UnsubscribeRequestWorkItem (org.apache.qpid.qmf2.agent.UnsubscribeRequestWorkItem)1 Broker (org.apache.qpid.server.model.Broker)1 Queue (org.apache.qpid.server.model.Queue)1