Search in sources :

Example 1 with SubscribeRequestWorkItem

use of org.apache.qpid.qmf2.agent.SubscribeRequestWorkItem 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 SubscribeRequestWorkItem

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

the class AgentExternalTest method onEvent.

public void onEvent(WorkItem wi) {
    System.out.println("WorkItem type: " + wi.getType());
    if (wi.getType() == METHOD_CALL) {
        _control.incValue("methodCount", 1);
        MethodCallWorkItem item = (MethodCallWorkItem) wi;
        MethodCallParams methodCallParams = item.getMethodCallParams();
        String methodName = methodCallParams.getName();
        ObjectId objectId = methodCallParams.getObjectId();
        String userId = methodCallParams.getUserId();
        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);
                    addObject(child);
                    QmfData outArgs = new QmfData();
                    // Set suptype 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: AgentExternalTest failed");
            QmfData error = new QmfData();
            error.setValue("error_text", qmfe.getMessage());
            _agent.methodResponse(methodName, item.getHandle(), null, error);
        }
    }
    if (wi.getType() == QUERY) {
        QueryWorkItem item = (QueryWorkItem) wi;
        QmfQuery query = item.getQmfQuery();
        System.out.println("Query User ID = " + item.getUserId());
        if (query.getObjectId() != null) {
            // 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()) {
                _agent.queryResponse(item.getHandle(), object);
            }
            _agent.queryComplete(item.getHandle(), 0);
        } else {
            // taken by the C++ broker ManagementAgent, so if it's a problem here........
            for (QmfAgentData object : _objectIndex.values()) {
                if (!object.isDeleted() && query.evaluate(object)) {
                    _agent.queryResponse(item.getHandle(), object);
                }
            }
            _agent.queryComplete(item.getHandle(), 0);
        }
    }
    if (wi.getType() == SUBSCRIBE_REQUEST) {
        SubscribeRequestWorkItem item = (SubscribeRequestWorkItem) wi;
        SubscriptionParams params = item.getSubscriptionParams();
        Handle handle = item.getHandle();
        System.out.println("Subscribe Request User ID = " + params.getUserId());
        try {
            Subscription subscription = new Subscription(this, params);
            _subscriptions.put(subscription.getSubscriptionId(), subscription);
            _timer.schedule(subscription, 0, params.getPublishInterval());
            if (subscription == null) {
                System.out.println("Requested Subscription has already expired or been cancelled");
                QmfData error = new QmfData();
                error.setValue("error_text", "Requested Subscription has already expired or been cancelled");
                _agent.subscriptionResponse(handle, subscription.getConsoleHandle(), null, 0, 0, error);
            } else {
                _agent.subscriptionResponse(handle, subscription.getConsoleHandle(), subscription.getSubscriptionId(), subscription.getDuration(), subscription.getInterval(), null);
            }
        } catch (QmfException qmfe) {
            _agent.raiseException(handle, "Subscribe Request failed, invalid Query: " + qmfe.getMessage());
        }
    }
    if (wi.getType() == RESUBSCRIBE_REQUEST) {
        ResubscribeRequestWorkItem item = (ResubscribeRequestWorkItem) wi;
        ResubscribeParams params = item.getResubscribeParams();
        Handle handle = item.getHandle();
        System.out.println("Resubscribe Request User ID = " + params.getUserId());
        String subscriptionId = params.getSubscriptionId();
        Subscription subscription = _subscriptions.get(subscriptionId);
        if (subscription != null) {
            subscription.refresh(params);
            _agent.subscriptionResponse(handle, subscription.getConsoleHandle(), subscription.getSubscriptionId(), subscription.getDuration(), subscription.getInterval(), null);
        } else {
            System.out.println("Requested Subscription has already expired or been cancelled");
            QmfData error = new QmfData();
            error.setValue("error_text", "Requested Subscription has already expired or been cancelled");
            _agent.subscriptionResponse(handle, subscription.getConsoleHandle(), null, 0, 0, error);
        }
    }
    if (wi.getType() == UNSUBSCRIBE_REQUEST) {
        UnsubscribeRequestWorkItem item = (UnsubscribeRequestWorkItem) wi;
        String subscriptionId = item.getSubscriptionId();
        System.out.println("Received cancellation request for " + subscriptionId);
        Subscription subscription = _subscriptions.get(subscriptionId);
        if (subscription != null) {
            subscription.cancel();
        }
    }
}
Also used : MethodCallWorkItem(org.apache.qpid.qmf2.agent.MethodCallWorkItem) QmfData(org.apache.qpid.qmf2.common.QmfData) UnsubscribeRequestWorkItem(org.apache.qpid.qmf2.agent.UnsubscribeRequestWorkItem) ObjectId(org.apache.qpid.qmf2.common.ObjectId) QmfEvent(org.apache.qpid.qmf2.common.QmfEvent) SubscribeRequestWorkItem(org.apache.qpid.qmf2.agent.SubscribeRequestWorkItem) Handle(org.apache.qpid.qmf2.common.Handle) SubscriptionParams(org.apache.qpid.qmf2.agent.SubscriptionParams) ResubscribeParams(org.apache.qpid.qmf2.agent.ResubscribeParams) MethodCallParams(org.apache.qpid.qmf2.agent.MethodCallParams) QmfAgentData(org.apache.qpid.qmf2.agent.QmfAgentData) QueryWorkItem(org.apache.qpid.qmf2.agent.QueryWorkItem) Subscription(org.apache.qpid.qmf2.agent.Subscription) QmfQuery(org.apache.qpid.qmf2.common.QmfQuery) ResubscribeRequestWorkItem(org.apache.qpid.qmf2.agent.ResubscribeRequestWorkItem) QmfException(org.apache.qpid.qmf2.common.QmfException)

Aggregations

Handle (org.apache.qpid.qmf2.common.Handle)2 QmfData (org.apache.qpid.qmf2.common.QmfData)2 QmfException (org.apache.qpid.qmf2.common.QmfException)2 QmfQuery (org.apache.qpid.qmf2.common.QmfQuery)2 JMSException (javax.jms.JMSException)1 MethodCallParams (org.apache.qpid.qmf2.agent.MethodCallParams)1 MethodCallWorkItem (org.apache.qpid.qmf2.agent.MethodCallWorkItem)1 QmfAgentData (org.apache.qpid.qmf2.agent.QmfAgentData)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 ObjectId (org.apache.qpid.qmf2.common.ObjectId)1 QmfEvent (org.apache.qpid.qmf2.common.QmfEvent)1