Search in sources :

Example 11 with Agent

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

the class Agent method setConnection.

/**
     * Connect the Agent to the AMQP cloud.
     * <p>
     * This is an extension to the standard QMF2 API allowing the user to specify address options in order to allow
     * finer control over the Agent's ingest queue, such as an explicit name, non-default size or durability.
     *
     * @param conn a javax.jms.Connection.
     * @param addressOptions options String giving finer grained control of the receiver queue.
     * <p>
     * As an example the following gives the Agent's ingest queue the name test-agent, size = 500000000 and ring policy.
     * <pre>
     * " ; {link: {name:'test-agent', x-declare: {arguments: {'qpid.policy_type': ring, 'qpid.max_size': 500000000}}}}"
     * </pre>
     */
public final void setConnection(final Connection conn, final String addressOptions) throws QmfException {
    // to the same Agent instance at the same time.
    synchronized (this) {
        if (_connection != null) {
            throw new QmfException("Multiple connections per Agent is not supported");
        }
        _connection = conn;
    }
    if (_name == null || _vendor == null || _product == null) {
        throw new QmfException("The vendor, product or name is not set");
    }
    setValue("_epoch", _epoch);
    setValue("_heartbeat_interval", _heartbeatInterval);
    setValue("_name", _name);
    setValue("_product", _product);
    setValue("_vendor", _vendor);
    setValue("_instance", _instance);
    try {
        _asyncSession = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        _syncSession = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // Create a Destination for the QMF direct address, mainly used for request/response
        String directBase = "qmf." + _domain + ".direct";
        _quotedDirectBase = "'" + directBase + "'";
        _directAddress = _syncSession.createQueue(directBase);
        // Create a Destination for the QMF topic address used to broadcast Events & Heartbeats.
        String topicBase = "qmf." + _domain + ".topic";
        _quotedTopicBase = "'" + topicBase + "'";
        _topicAddress = _syncSession.createQueue(topicBase);
        // Create an unidentified MessageProducer for sending to various destinations.
        _producer = _syncSession.createProducer(null);
        // TODO it should be possible to bind _locateConsumer, _mainConsumer and _aliasConsumer to the
        // same queue if I can figure out the correct AddressString to use, probably not a big deal though.
        // Set up MessageListener on the Agent Locate Address
        Destination locateAddress = _asyncSession.createQueue(topicBase + "/console.request.agent_locate");
        _locateConsumer = _asyncSession.createConsumer(locateAddress);
        _locateConsumer.setMessageListener(this);
        // Set up MessageListener on the Agent address
        String address = directBase + "/" + _name + addressOptions;
        Destination agentAddress = _asyncSession.createQueue(address);
        _mainConsumer = _asyncSession.createConsumer(agentAddress);
        _mainConsumer.setMessageListener(this);
        // alias address rather than the discovered address when talking to the broker ManagementAgent.
        if (_product.equals("qpidd")) {
            String alias = directBase + "/broker";
            _log.info("Creating address {} as an alias address for the broker Agent", alias);
            Destination aliasAddress = _asyncSession.createQueue(alias);
            _aliasConsumer = _asyncSession.createConsumer(aliasAddress);
            _aliasConsumer.setMessageListener(this);
        }
        _connection.start();
        // Schedule a Heartbeat every _heartbeatInterval seconds sending the first one immediately
        _timer = new Timer(true);
        _timer.schedule(new Heartbeat(), 0, _heartbeatInterval * 1000);
    } catch (JMSException jmse) {
        // If we can't create the QMF Destinations there's not much else we can do
        _log.info("JMSException {} caught in setConnection()", jmse.getMessage());
        throw new QmfException("Failed to create sessions or destinations " + jmse.getMessage());
    }
}
Also used : Destination(javax.jms.Destination) Timer(java.util.Timer) JMSException(javax.jms.JMSException) QmfException(org.apache.qpid.qmf2.common.QmfException)

Example 12 with Agent

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

the class QmfManagementAgent method childAdded.

/**
     * ConfigurationChangeListener method called when a child ConfiguredObject is added.
     * <p>
     * This method checks the type of the child ConfiguredObject that has been added and creates the equivalent
     * QMF2 Management Object if one doesn't already exist. In most cases it's a one-to-one mapping, but for
     * Binding for example the Binding child is added to both Queue and Exchange so we only create the Binding
     * QMF2 Management Object once and add the queueRef and exchangeRef reference properties referencing the Queue
     * and Exchange parent Objects respectively, Similarly for Consumer (AKA Subscription).
     * <p>
     * This method is also responsible for raising the appropriate QMF2 Events when Management Objects are created.
     * @param object the parent object that the child is being added to.
     * @param child the child object being added.
     */
@Override
public void childAdded(final ConfiguredObject object, final ConfiguredObject child) {
    if (_log.isDebugEnabled()) {
        _log.debug("childAdded: " + child.getClass().getSimpleName() + "." + child.getName());
    }
    QmfAgentData data = null;
    if (child instanceof Broker) {
        data = new org.apache.qpid.server.qmf2.agentdata.Broker((Broker) child);
    } else if (child instanceof Connection) {
        if (!agentConnection && !_objects.containsKey(child)) {
            // If the parent object is the default vhost set it to null so that the Connection ignores it.
            VirtualHost vhost = (object.getName().equals(_defaultVirtualHost)) ? null : (VirtualHost) object;
            data = new org.apache.qpid.server.qmf2.agentdata.Connection(vhost, (Connection) child);
            _objects.put(child, data);
            // Raise a Client Connect Event.
            _agent.raiseEvent(((org.apache.qpid.server.qmf2.agentdata.Connection) data).createClientConnectEvent());
        }
        // Only ignore the first Connection, which is the one from the Agent. 
        agentConnection = false;
    } else if (child instanceof Session) {
        if (!_objects.containsKey(child)) {
            // Get the Connection QmfAgentData so we can get connectionRef.
            QmfAgentData ref = _objects.get(object);
            if (ref != null) {
                data = new org.apache.qpid.server.qmf2.agentdata.Session((Session) child, ref.getObjectId());
                _objects.put(child, data);
            }
        }
    } else if (child instanceof Exchange) {
        if (!_objects.containsKey(child)) {
            // If the parent object is the default vhost set it to null so that the Connection ignores it.
            VirtualHost vhost = (object.getName().equals(_defaultVirtualHost)) ? null : (VirtualHost) object;
            data = new org.apache.qpid.server.qmf2.agentdata.Exchange(vhost, (Exchange) child);
            _objects.put(child, data);
            // Raise an Exchange Declare Event.
            _agent.raiseEvent(((org.apache.qpid.server.qmf2.agentdata.Exchange) data).createExchangeDeclareEvent());
        }
    } else if (child instanceof Queue) {
        if (!_objects.containsKey(child)) {
            // If the parent object is the default vhost set it to null so that the Connection ignores it.
            VirtualHost vhost = (object.getName().equals(_defaultVirtualHost)) ? null : (VirtualHost) object;
            data = new org.apache.qpid.server.qmf2.agentdata.Queue(vhost, (Queue) child);
            _objects.put(child, data);
            // Raise a Queue Declare Event.
            _agent.raiseEvent(((org.apache.qpid.server.qmf2.agentdata.Queue) data).createQueueDeclareEvent());
        }
    } else if (child instanceof Binding) {
        // depending on whether Queue or Exchange was the parent of this addChild() call.
        if (!_objects.containsKey(child)) {
            data = new org.apache.qpid.server.qmf2.agentdata.Binding((Binding) child);
            _objects.put(child, data);
            String eName = ((Binding) child).getExchange().getName();
            if (// Don't send Event for Binding to default direct.
            !eName.equals("<<default>>")) {
                // Raise a Bind Event.
                _agent.raiseEvent(((org.apache.qpid.server.qmf2.agentdata.Binding) data).createBindEvent());
            }
        }
        org.apache.qpid.server.qmf2.agentdata.Binding binding = (org.apache.qpid.server.qmf2.agentdata.Binding) _objects.get(child);
        QmfAgentData ref = _objects.get(object);
        if (ref != null) {
            if (object instanceof Queue) {
                binding.setQueueRef(ref.getObjectId());
            } else if (object instanceof Exchange) {
                binding.setExchangeRef(ref.getObjectId());
            }
        }
    } else if (// AKA Subscription
    child instanceof Consumer) {
        // Session reference depending on whether Queue or Session was the parent of this addChild() call.
        if (!_objects.containsKey(child)) {
            data = new org.apache.qpid.server.qmf2.agentdata.Subscription((Consumer) child);
            _objects.put(child, data);
        }
        org.apache.qpid.server.qmf2.agentdata.Subscription subscription = (org.apache.qpid.server.qmf2.agentdata.Subscription) _objects.get(child);
        QmfAgentData ref = _objects.get(object);
        if (ref != null) {
            if (object instanceof Queue) {
                subscription.setQueueRef(ref.getObjectId(), (Queue) object);
                // Raise a Subscribe Event - N.B. Need to do it *after* we've set the queueRef.
                _agent.raiseEvent(subscription.createSubscribeEvent());
            } else if (object instanceof Session) {
                subscription.setSessionRef(ref.getObjectId());
            }
        }
    }
    try {
        // If we've created new QmfAgentData we register it with the Agent.
        if (data != null) {
            _agent.addObject(data);
        }
    } catch (QmfException qmfe) {
        _log.error("QmfException caught in QmfManagementAgent.addObject()", qmfe);
    }
    child.addChangeListener(this);
}
Also used : Binding(org.apache.qpid.server.model.Binding) Broker(org.apache.qpid.server.model.Broker) Connection(org.apache.qpid.server.model.Connection) Exchange(org.apache.qpid.server.model.Exchange) Consumer(org.apache.qpid.server.model.Consumer) QmfAgentData(org.apache.qpid.qmf2.agent.QmfAgentData) VirtualHost(org.apache.qpid.server.model.VirtualHost) Queue(org.apache.qpid.server.model.Queue) Session(org.apache.qpid.server.model.Session) QmfException(org.apache.qpid.qmf2.common.QmfException)

Example 13 with Agent

use of org.apache.qpid.qmf2.console.Agent 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 &amp; 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 14 with Agent

use of org.apache.qpid.qmf2.console.Agent 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 15 with Agent

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

the class BigPayloadAgentTest method setupSchema.

public void setupSchema() throws QmfException {
    System.out.println("*** BigPayloadAgentTest initialising the various Schema classes ***");
    // Create and register schema for this agent.
    String packageName = "com.test.bigagent";
    // Declare a control object to test methods against.
    _controlSchema = new SchemaObjectClass(packageName, "control");
    _controlSchema.addProperty(new SchemaProperty("name", QmfType.TYPE_STRING));
    _controlSchema.setIdNames("name");
    SchemaMethod createMethod = new SchemaMethod("processPayload", "Process a large payload");
    createMethod.addArgument(new SchemaProperty("parameter", QmfType.TYPE_STRING, "{dir:IN}"));
    createMethod.addArgument(new SchemaProperty("return", QmfType.TYPE_STRING, "{dir:OUT}"));
    _controlSchema.addMethod(createMethod);
    System.out.println("BigPayloadAgentTest Schema classes initialised OK");
    _agent.registerObjectClass(_controlSchema);
    System.out.println("BigPayloadAgentTest Schema classes registered OK");
}
Also used : SchemaProperty(org.apache.qpid.qmf2.common.SchemaProperty) SchemaMethod(org.apache.qpid.qmf2.common.SchemaMethod) SchemaObjectClass(org.apache.qpid.qmf2.common.SchemaObjectClass)

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