Package org.apache.qpid.qmf2.common

Examples of org.apache.qpid.qmf2.common.QmfException


     */
    public SubscribeParams refreshSubscription(String subscriptionId, final String options) throws QmfException
    {
        if (subscriptionId == null)
        {
            throw new QmfException("Called refreshSubscription() with null subscriptionId");
        }
        SubscriptionManager subscription = _subscriptionById.get(subscriptionId);
        if (subscription == null)
        {
            throw new QmfException("Called refreshSubscription() with invalid subscriptionId");
        }
        String consoleHandle = subscription.getConsoleHandle();
        Agent agent = subscription.getAgent();
        if (!agent.isActive())
        {
            throw new QmfException("Called refreshSubscription() with inactive agent");
        }
        String agentName = agent.getName();

        // Initialise optional values to defaults;
        long lifetime = 0;
        long timeout = _replyTimeout;
        String replyHandle = null;

        if (options != null)
        { // We wrap the Map in a QmfData object to avoid potential class cast issues with the parsed options
            QmfData optMap = new QmfData(new AddressParser(options).map());
            if (optMap.hasValue("lifetime"))
            {
                lifetime = optMap.getLongValue("lifetime");
            }

            if (optMap.hasValue("timeout"))
            {
                timeout = optMap.getLongValue("timeout");
            }

            if (optMap.hasValue("replyHandle"))
            {
                replyHandle = optMap.getStringValue("replyHandle");
            }
        }

        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", "_subscribe_refresh_indication");
            request.setStringProperty("qpid.subject", agentName);

            request.setObject("_subscription_id", subscriptionId);
            if (lifetime > 0)
            {
                request.setObject("_duration", lifetime);
            }

            // Wrap request & response in synchronized block in case any other threads invoke a request
            // it would be somewhat unfortunate if their response got interleaved with ours!!
            synchronized(this)
            {
                if (_subscriptionEmulationEnabled && agentName.equals(_brokerAgentName))
                { // If the Agent is the broker Agent we emulate the Subscription on the Console
                    subscription.refresh();
                    final SubscribeParams params = new SubscribeParams(consoleHandle, subscription.mapEncode());
                    if (replyHandle == null)
                    {
                        return params;
                    }
                    else
                    {
                        final String handle = replyHandle;
                        Thread thread = new Thread()
                        {
                            public void run()
                            {
                                _eventListener.onEvent(new SubscribeResponseWorkItem(new Handle(handle), params));
                            }
                        };
                        thread.start();
                    }
                    return null;
                }

                _requester.send(request);
                if (replyHandle == null)
                { // If this is an synchronous request get the response
                    Message response = _responder.receive(timeout*1000);
                    if (response == null)
                    {
                        subscription.cancel();
                        _log.info("No response received in refreshSubscription()");
                        throw new QmfException("No response received for Console.refreshSubscription()");
                    }
                    SubscribeParams result = new SubscribeParams(consoleHandle, AMQPMessage.getMap(response));
                    subscriptionId = result.getSubscriptionId();
                    if (subscriptionId == null)
                    {
                        subscription.cancel();
                    }
                    else
                    {
                        subscription.setDuration(result.getLifetime());
                        subscription.refresh();
                    }
                    return result;
                }
            }
            // If this is an asynchronous request return without waiting for a response
            return null;
        }
        catch (JMSException jmse)
        {
            _log.info("JMSException {} caught in refreshSubscription()", jmse.getMessage());
            throw new QmfException(jmse.getMessage());
        }
    } // end of refreshSubscription()
View Full Code Here


     */
    public void cancelSubscription(final String subscriptionId) throws QmfException
    {
        if (subscriptionId == null)
        {
            throw new QmfException("Called cancelSubscription() with null subscriptionId");
        }
        SubscriptionManager subscription = _subscriptionById.get(subscriptionId);
        if (subscription == null)
        {
            throw new QmfException("Called cancelSubscription() with invalid subscriptionId");
        }
        String consoleHandle = subscription.getConsoleHandle();
        Agent agent = subscription.getAgent();
        if (!agent.isActive())
        {
            throw new QmfException("Called cancelSubscription() with inactive agent");
        }
        String agentName = agent.getName();

        try
        {
View Full Code Here

    public MethodResult invokeMethod(final Agent agent, final Map<String, Object> content,
                                     final String replyHandle, int timeout) throws QmfException
    {
        if (!agent.isActive())
        {
            throw new QmfException("Called invokeMethod() with inactive agent");
        }
        String agentName = agent.getName();
        timeout = (timeout < 1) ? _replyTimeout : timeout;
        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", "_method_request");
            request.setStringProperty("qpid.subject", agentName);

            for (Map.Entry<String, Object> entry : content.entrySet())
            {
                request.setObject(entry.getKey(), entry.getValue());
            }

            // Wrap request & response in synchronized block in case any other threads invoke a request
            // it would be somewhat unfortunate if their response got interleaved with ours!!
            synchronized(this)
            {
                _requester.send(request);
                if (replyHandle == null)
                { // If this is a synchronous request get the response
                    Message response = _responder.receive(timeout*1000);
                    if (response == null)
                    {
                        _log.info("No response received in invokeMethod()");
                        throw new QmfException("No response received for Console.invokeMethod()");
                    }
                    MethodResult result = new MethodResult(AMQPMessage.getMap(response));
                    QmfException exception = result.getQmfException();
                    if (exception != null)
                    {
                        throw exception;
                    }
                    return result;
                }
            }
            // If this is an asynchronous request return without waiting for a response
            return null;
        }
        catch (JMSException jmse)
        {
            _log.info("JMSException {} caught in invokeMethod()", jmse.getMessage());
            throw new QmfException(jmse.getMessage());
        }
    }
View Full Code Here

        {
            _eventListener = (QmfEventListener)notifier;
        }
        else
        {
            throw new QmfException("QmfCallback listener must be either a Notifier or QmfEventListener");
        }

        if (options != null)
        { // We wrap the Map in a QmfData object to avoid potential class cast issues with the parsed options
            QmfData optMap = new QmfData(new AddressParser(options).map());
View Full Code Here

        // to the same Console instance at the same time.
        synchronized(this)
        {
            if (_connection != null)
            {
                throw new QmfException("Multiple connections per Console is not supported");
            }
            _connection = conn;
        }
        try
        {
            String syncReplyAddressOptions = addressOptions;
            String asyncReplyAddressOptions = addressOptions;
            String eventAddressOptions = addressOptions;

            if (!addressOptions.equals(""))
            { // If there are address options supplied we need to check if a name parameter is present.
                String[] split = addressOptions.split("name");
                if (split.length == 2)
                { // If options contains a name parameter we extract it and create variants for async and event queues.
                    split = split[1].split("[,}]"); // Look for the end of the key/value block
                    String nameValue = split[0].replaceAll("[ :'\"]", ""); // Remove initial colon, space any any quotes.
                    // Hopefully at this point nameValue is actually the value of the name parameter.
                    asyncReplyAddressOptions = asyncReplyAddressOptions.replace(nameValue, nameValue + "-async");
                    eventAddressOptions = eventAddressOptions.replace(nameValue, nameValue + "-event");
                }
            }

            String topicBase  = "qmf." + _domain + ".topic";
            _syncSession = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            // Create a MessageProducer for the QMF topic address used to broadcast requests
            Destination topicAddress = _syncSession.createQueue(topicBase);
            _broadcaster = _syncSession.createProducer(topicAddress);

            // If Asynchronous Behaviour is enabled we create the Queues used to receive async responses
            // Data Indications, QMF Events, Heartbeats etc. from the broker (or other Agents).
            if (!_disableEvents)
            {
                // TODO it should be possible to bind _eventConsumer and _asyncResponder to the same queue
                // if I can figure out the correct AddressString to use, probably not a big deal though.

                _asyncSession = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

                // Set up MessageListener on the Event Address
                Destination eventAddress = _asyncSession.createQueue(topicBase + "/agent.ind.#" + eventAddressOptions);
                _eventConsumer = _asyncSession.createConsumer(eventAddress);
                _eventConsumer.setMessageListener(this);

                // Create the asynchronous JMSReplyTo _replyAddress and MessageConsumer
                _asyncReplyAddress = _asyncSession.createQueue(_address + ".async" + asyncReplyAddressOptions);
                _asyncResponder = _asyncSession.createConsumer(_asyncReplyAddress);
                _asyncResponder.setMessageListener(this);
            }

            // I've extended the synchronized block to include creating the _requester and _responder. I don't believe
            // that this is strictly necessary, but it stops findbugs moaning about inconsistent synchronization
            // so makes sense if only to get that warm and fuzzy feeling of keeping findbugs happy :-)
            synchronized(this)
            {
                // Create a MessageProducer for the QMF direct address, mainly used for request/response
                Destination directAddress = _syncSession.createQueue("qmf." + _domain + ".direct");
                _requester = _syncSession.createProducer(directAddress);

                // Create the JMSReplyTo _replyAddress and MessageConsumer
                _replyAddress = _syncSession.createQueue(_address + syncReplyAddressOptions);
                _responder = _syncSession.createConsumer(_replyAddress);

                _connection.start();

                // If Asynchronous Behaviour is disabled we create an Agent instance to represent the broker
                // ManagementAgent the only info that needs to be populated is the _name and we can use the
                // "broker" synonym. We populate this fake Agent so getObjects() behaviour is consistent whether
                // we've any received *real* Agent updates or not.
                if (_disableEvents)
                {
                    _brokerAgentName = "broker";
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("_name", _brokerAgentName);
                    Agent agent = new Agent(map, this);
                    _agents.put(_brokerAgentName, agent);
                    _agentAvailable = true;
                }
                else
                {
                    // If Asynchronous Behaviour is enabled Broadcast an Agent Locate message to get Agent info quickly.
                    broadcastAgentLocate();
                }

                // Wait until the Broker Agent has been located (this should generally be pretty quick)
                while (!_agentAvailable)
                {
                    long startTime = System.currentTimeMillis();
                    try
                    {
                        wait(_replyTimeout*1000);
                    }
                    catch (InterruptedException ie)
                    {
                        continue;
                    }
                    // Measure elapsed time to test against spurious wakeups and ensure we really have timed out
                    long elapsedTime = (System.currentTimeMillis() - startTime)/1000;
                    if (!_agentAvailable && elapsedTime >= _replyTimeout)
                    {
                        _log.info("Broker Agent not found");
                        throw new QmfException("Broker Agent not found");
                    }
                }

                // Timer used for tidying up Subscriptions.
                _timer = new Timer(true);
            }
        }
        catch (JMSException jmse)
        {
            // If we can't create the QMF Destinations there's not much else we can do
            _log.info("JMSException {} caught in addConnection()", jmse.getMessage());
            throw new QmfException("Failed to create sessions or destinations " + jmse.getMessage());
        }
    }
View Full Code Here

     */
    public void removeConnection(final Connection conn) throws QmfException
    {
        if (conn != _connection)
        {
            throw new QmfException("Attempt to delete unknown connection");
        }

        try
        {
            _timer.cancel();
            _connection.close(); // Should we close() the connection here or just stop() it ???
        }
        catch (JMSException jmse)
        {
            throw new QmfException("Failed to remove connection, caught JMSException " + jmse.getMessage());
        }
        _connection = null;
    }
View Full Code Here

    public synchronized SubscribeParams createSubscription(final Agent agent, final QmfQuery query,
                                                final String consoleHandle, final String options) throws QmfException
    {
        if (consoleHandle == null)
        {
            throw new QmfException("Called createSubscription() with null consoleHandle");
        }
        if (_subscriptionByHandle.get(consoleHandle) != null)
        {
            throw new QmfException("Called createSubscription() with a consoleHandle that is already in use");
        }
        if (agent == null)
        {
            throw new QmfException("Called createSubscription() with null agent");
        }
        if (!agent.isActive())
        {
            throw new QmfException("Called createSubscription() with inactive agent");
        }
        String agentName = agent.getName();

        // Initialise optional values to defaults;
        long lifetime = _subscriptionDuration;
        long publishInterval = 10000;
        long timeout = _replyTimeout;
        String replyHandle = null;

        if (options != null)
        { // We wrap the Map in a QmfData object to avoid potential class cast issues with the parsed options
            QmfData optMap = new QmfData(new AddressParser(options).map());
            if (optMap.hasValue("lifetime"))
            {
                lifetime = optMap.getLongValue("lifetime");
            }

            if (optMap.hasValue("publishInterval"))
            { // Multiply publishInterval by 1000 because the QMF2 protocol spec says interval is
              // "The request time (in milliseconds) between periodic updates of data in this subscription"
                publishInterval = 1000*optMap.getLongValue("publishInterval");
            }

            if (optMap.hasValue("timeout"))
            {
                timeout = optMap.getLongValue("timeout");
            }

            if (optMap.hasValue("replyHandle"))
            {
                replyHandle = optMap.getStringValue("replyHandle");
            }
        }

        try
        {
            MapMessage request = _syncSession.createMapMessage();
            request.setJMSReplyTo(_asyncReplyAddress)// Deliberately forcing all replies to the _asyncReplyAddress
            request.setJMSCorrelationID(consoleHandle); // Deliberately using consoleHandle not replyHandle here
            request.setStringProperty("x-amqp-0-10.app-id", "qmf2");
            request.setStringProperty("method", "request");
            request.setStringProperty("qmf.opcode", "_subscribe_request");
            request.setStringProperty("qpid.subject", agentName);

            request.setObject("_query", query.mapEncode());
            request.setObject("_interval", publishInterval);
            request.setObject("_duration", lifetime);

            SubscriptionManager subscription =
                new SubscriptionManager(agent, query, consoleHandle, replyHandle, publishInterval, lifetime);
            _subscriptionByHandle.put(consoleHandle, subscription);
            _timer.schedule(subscription, 0, publishInterval);

            if (_subscriptionEmulationEnabled && agentName.equals(_brokerAgentName))
            { // If the Agent is the broker Agent we emulate the Subscription on the Console
                String subscriptionId = UUID.randomUUID().toString();
                _subscriptionById.put(subscriptionId, subscription);
                subscription.setSubscriptionId(subscriptionId);
                final SubscribeParams params = new SubscribeParams(consoleHandle, subscription.mapEncode());
                if (replyHandle == null)
                {
                    return params;
                }
                else
                {
                    final String handle = replyHandle;
                    Thread thread = new Thread()
                    {
                        public void run()
                        {
                            _eventListener.onEvent(new SubscribeResponseWorkItem(new Handle(handle), params));
                        }
                    };
                    thread.start();
                }
                return null;
            }

            _requester.send(request);
            if (replyHandle == null)
            { // If this is an synchronous request get the response
                subscription.await(timeout*1000);
                if (subscription.getSubscriptionId() == null)
                {
                    _log.info("No response received in createSubscription()");
                    throw new QmfException("No response received for Console.createSubscription()");
                }
                return new SubscribeParams(consoleHandle, subscription.mapEncode());
            }

            // If this is an asynchronous request return without waiting for a response
            return null;
        }
        catch (JMSException jmse)
        {
            _log.info("JMSException {} caught in createSubscription()", jmse.getMessage());
            throw new QmfException(jmse.getMessage());
        }
    } // end of createSubscription()
View Full Code Here

     */
    public SubscribeParams refreshSubscription(String subscriptionId, final String options) throws QmfException
    {
        if (subscriptionId == null)
        {
            throw new QmfException("Called refreshSubscription() with null subscriptionId");
        }
        SubscriptionManager subscription = _subscriptionById.get(subscriptionId);
        if (subscription == null)
        {
            throw new QmfException("Called refreshSubscription() with invalid subscriptionId");
        }
        String consoleHandle = subscription.getConsoleHandle();
        Agent agent = subscription.getAgent();
        if (!agent.isActive())
        {
            throw new QmfException("Called refreshSubscription() with inactive agent");
        }
        String agentName = agent.getName();

        // Initialise optional values to defaults;
        long lifetime = 0;
        long timeout = _replyTimeout;
        String replyHandle = null;

        if (options != null)
        { // We wrap the Map in a QmfData object to avoid potential class cast issues with the parsed options
            QmfData optMap = new QmfData(new AddressParser(options).map());
            if (optMap.hasValue("lifetime"))
            {
                lifetime = optMap.getLongValue("lifetime");
            }

            if (optMap.hasValue("timeout"))
            {
                timeout = optMap.getLongValue("timeout");
            }

            if (optMap.hasValue("replyHandle"))
            {
                replyHandle = optMap.getStringValue("replyHandle");
            }
        }

        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", "_subscribe_refresh_indication");
            request.setStringProperty("qpid.subject", agentName);

            request.setObject("_subscription_id", subscriptionId);
            if (lifetime > 0)
            {
                request.setObject("_duration", lifetime);
            }

            // Wrap request & response in synchronized block in case any other threads invoke a request
            // it would be somewhat unfortunate if their response got interleaved with ours!!
            synchronized(this)
            {
                if (_subscriptionEmulationEnabled && agentName.equals(_brokerAgentName))
                { // If the Agent is the broker Agent we emulate the Subscription on the Console
                    subscription.refresh();
                    final SubscribeParams params = new SubscribeParams(consoleHandle, subscription.mapEncode());
                    if (replyHandle == null)
                    {
                        return params;
                    }
                    else
                    {
                        final String handle = replyHandle;
                        Thread thread = new Thread()
                        {
                            public void run()
                            {
                                _eventListener.onEvent(new SubscribeResponseWorkItem(new Handle(handle), params));
                            }
                        };
                        thread.start();
                    }
                    return null;
                }

                _requester.send(request);
                if (replyHandle == null)
                { // If this is an synchronous request get the response
                    Message response = _responder.receive(timeout*1000);
                    if (response == null)
                    {
                        subscription.cancel();
                        _log.info("No response received in refreshSubscription()");
                        throw new QmfException("No response received for Console.refreshSubscription()");
                    }
                    SubscribeParams result = new SubscribeParams(consoleHandle, AMQPMessage.getMap(response));
                    subscriptionId = result.getSubscriptionId();
                    if (subscriptionId == null)
                    {
                        subscription.cancel();
                    }
                    else
                    {
                        subscription.setDuration(result.getLifetime());
                        subscription.refresh();
                    }
                    return result;
                }
            }
            // If this is an asynchronous request return without waiting for a response
            return null;
        }
        catch (JMSException jmse)
        {
            _log.info("JMSException {} caught in refreshSubscription()", jmse.getMessage());
            throw new QmfException(jmse.getMessage());
        }
    } // end of refreshSubscription()
View Full Code Here

     */
    public void cancelSubscription(final String subscriptionId) throws QmfException
    {
        if (subscriptionId == null)
        {
            throw new QmfException("Called cancelSubscription() with null subscriptionId");
        }
        SubscriptionManager subscription = _subscriptionById.get(subscriptionId);
        if (subscription == null)
        {
            throw new QmfException("Called cancelSubscription() with invalid subscriptionId");
        }
        String consoleHandle = subscription.getConsoleHandle();
        Agent agent = subscription.getAgent();
        if (!agent.isActive())
        {
            throw new QmfException("Called cancelSubscription() with inactive agent");
        }
        String agentName = agent.getName();

        try
        {
View Full Code Here

        if (name != null)
        {
            String[] split = name.split(":");
            if (split.length < 2 || split.length > 3)
            {
                throw new QmfException("Agent name must be in the format <vendor>:<product>[:<instance>]");
            }

            _vendor = split[0];
            _product = split[1];

            if (split.length == 3)
            {
                _instance = split[2];
            }

            _name = _vendor + ":" + _product + ":" + _instance;
        }

        _domain = (domain == null) ? "default" : domain;

        if (notifier == null)
        {
            _eventListener = new NullQmfEventListener();
        }
        else if (notifier instanceof Notifier)
        {
            _eventListener = new NotifierWrapper((Notifier)notifier, _workQueue);
        }
        else if (notifier instanceof QmfEventListener)
        {
            _eventListener = (QmfEventListener)notifier;
        }
        else
        {
            throw new QmfException("QmfCallback listener must be either a Notifier or QmfEventListener");
        }

        if (interval > 0)
        {
            _heartbeatInterval = interval;
View Full Code Here

TOP

Related Classes of org.apache.qpid.qmf2.common.QmfException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.