Search in sources :

Example 1 with AddressParser

use of org.apache.qpid.messaging.util.AddressParser in project qpid by apache.

the class Console method createSubscription.

/**
 * Creates a subscription to the agent using the given Query.
 * <p>
 * The consoleHandle is an application-provided handle that will accompany each subscription update sent from
 * the Agent. Subscription updates will appear as SUBSCRIPTION_INDICATION WorkItems on the Console's work queue.
 * <p>
 * The publishInterval is the requested time interval in seconds on which the Agent should publish updates.
 * <p>
 * The lifetime parameter is the requested time interval in seconds for which this subscription should remain in
 * effect. Both the requested lifetime and publishInterval may be overridden by the Agent, as indicated in the
 * subscription response.
 * <p>
 * This method may be called asynchronously by providing a replyHandle argument. When called
 * asynchronously, the result of this method call is returned in a SUBSCRIBE_RESPONSE WorkItem with a
 * handle matching the value of replyHandle.
 * <p>
 * Timeout can be used to override the console's default reply timeout.
 * <p>
 * When called synchronously, this method returns a SubscribeParams object containing the result of the
 * subscription request.
 *
 * @param agent the Agent on which to create the subscription.
 * @param query the Query to perform on the Agent
 * @param consoleHandle an application-provided handle that will accompany each subscription update sent
 *        from the Agent.
 * @param options a String representation of a Map containing the options in the form
 *        <pre>"{lifetime:&lt;value&gt;, publishInterval:&lt;value&gt;, replyHandle:&lt;value&gt;, timeout:&lt;value&gt;}"</pre>
 *        they are optional and may appear in any order.
 * <pre>
 *        <b>lifetime</b> the requested time interval in seconds for which this subscription should remain in effect.
 *        <b>publishInterval</b> the requested time interval in seconds on which the Agent should publish updates
 *        <b>replyHandle</b> the correlation handle used to tie asynchronous method requests with responses.
 *        <b>timeout</b> the time to wait for a reply from the Agent.
 * </pre>
 */
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();
        // Deliberately forcing all replies to the _asyncReplyAddress
        request.setJMSReplyTo(_asyncReplyAddress);
        // Deliberately using consoleHandle not replyHandle here
        request.setJMSCorrelationID(consoleHandle);
        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());
    }
}
Also used : QmfData(org.apache.qpid.qmf2.common.QmfData) AddressParser(org.apache.qpid.messaging.util.AddressParser) MapMessage(javax.jms.MapMessage) JMSException(javax.jms.JMSException) QmfException(org.apache.qpid.qmf2.common.QmfException) Handle(org.apache.qpid.qmf2.common.Handle)

Example 2 with AddressParser

use of org.apache.qpid.messaging.util.AddressParser in project qpid by apache.

the class Console method refreshSubscription.

/**
 * Renews a subscription identified by SubscriptionId.
 * <p>
 * The Console may request a new subscription duration by providing a requested lifetime. This method may be called
 * asynchronously by providing a replyHandle argument.
 * <p>
 * When called asynchronously, the result of this method call is returned in a SUBSCRIBE_RESPONSE WorkItem.
 * <p>
 * Timeout can be used to override the console's default reply timeout.
 * <p>
 * When called synchronously, this method returns a class SubscribeParams object containing the result of the
 * subscription request.
 *
 * @param subscriptionId the ID of the subscription to be refreshed
 * @param options a String representation of a Map containing the options in the form
 *        <pre>"{lifetime:&lt;value&gt;, replyHandle:&lt;value&gt;, timeout:&lt;value&gt;}"</pre>
 *        they are optional and may appear in any order.
 * <pre>
 *        <b>lifetime</b> requests a new subscription duration.
 *        <b>replyHandle</b> the correlation handle used to tie asynchronous method requests with responses.
 *        <b>timeout</b> the time to wait for a reply from the Agent.
 * </pre>
 */
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);
        }
        // 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());
    }
}
Also used : Destination(javax.jms.Destination) QmfData(org.apache.qpid.qmf2.common.QmfData) AddressParser(org.apache.qpid.messaging.util.AddressParser) AMQPMessage(org.apache.qpid.qmf2.common.AMQPMessage) MapMessage(javax.jms.MapMessage) Message(javax.jms.Message) MapMessage(javax.jms.MapMessage) JMSException(javax.jms.JMSException) Handle(org.apache.qpid.qmf2.common.Handle) QmfException(org.apache.qpid.qmf2.common.QmfException)

Example 3 with AddressParser

use of org.apache.qpid.messaging.util.AddressParser in project qpid by apache.

the class ConnectionHelper method createConnectionURL.

/**
 * Creates a Java Connection URL from one of the other supported URL formats plus options.
 *
 * @param url an AMQP 0.10 URL, an extended AMQP 0-10 URL, a Broker URL or a Connection URL (the latter is simply
 *        returned untouched).
 * @param opts a String containing the options encoded using the same form as the C++ qpid::messaging
 *        Connection class.
 * @return a String containing the Java Connection URL.
 */
public static String createConnectionURL(String url, String opts) {
    // If a Java Connection URL has been passed in we simply return it.
    if (url.startsWith("amqp://") && url.contains("brokerlist")) {
        return url;
    }
    // Initialise options to default values
    String username = "";
    String password = "";
    String urlOptions = "";
    String brokerListOptions = "";
    // Get options from option String
    if (opts != null && opts.startsWith("{") && opts.endsWith("}")) {
        // Connection URL Options
        String maxprefetch = "";
        String sync_publish = "";
        String sync_ack = "";
        String use_legacy_map_msg_format = "";
        String failover = "";
        // Broker List Options
        String heartbeat = "";
        String retries = "";
        String connectdelay = "";
        String connecttimeout = "";
        String tcp_nodelay = "";
        String sasl_mechs = "";
        String sasl_encryption = "";
        String sasl_protocol = "";
        String sasl_server = "";
        String ssl = "";
        String ssl_verify_hostname = "";
        String ssl_cert_alias = "";
        String trust_store = "";
        String trust_store_password = "";
        String key_store = "";
        String key_store_password = "";
        Map options = new AddressParser(opts).map();
        if (options.containsKey("maxprefetch")) {
            maxprefetch = "&maxprefetch='" + options.get("maxprefetch").toString() + "'";
        }
        if (options.containsKey("sync_publish")) {
            sync_publish = "&sync_publish='" + options.get("sync_publish").toString() + "'";
        }
        if (options.containsKey("sync_ack")) {
            sync_ack = "&sync_ack='" + options.get("sync_ack").toString() + "'";
        }
        if (options.containsKey("use_legacy_map_msg_format")) {
            use_legacy_map_msg_format = "&use_legacy_map_msg_format='" + options.get("use_legacy_map_msg_format").toString() + "'";
        }
        if (options.containsKey("failover")) {
            if (options.containsKey("cyclecount")) {
                failover = "&failover='" + options.get("failover").toString() + "?cyclecount='" + options.get("cyclecount").toString() + "''";
            } else {
                failover = "&failover='" + options.get("failover").toString() + "'";
            }
        }
        if (options.containsKey("username")) {
            username = options.get("username").toString();
        }
        if (options.containsKey("password")) {
            password = options.get("password").toString();
        }
        if (options.containsKey("reconnect")) {
            String value = options.get("reconnect").toString();
            if (value.equalsIgnoreCase("true")) {
                retries = "&retries='" + Integer.MAX_VALUE + "'";
                connectdelay = "&connectdelay='5000'";
            }
        }
        if (options.containsKey("reconnect_limit")) {
            retries = "&retries='" + options.get("reconnect_limit").toString() + "'";
        }
        if (options.containsKey("reconnect_interval")) {
            connectdelay = "&connectdelay='" + options.get("reconnect_interval").toString() + "000'";
        }
        if (options.containsKey("reconnect_interval_min")) {
            connectdelay = "&connectdelay='" + options.get("reconnect_interval_min").toString() + "000'";
        }
        if (options.containsKey("reconnect_interval_max")) {
            connectdelay = "&connectdelay='" + options.get("reconnect_interval_max").toString() + "000'";
        }
        if (options.containsKey("reconnect_timeout")) {
            connecttimeout = "&connecttimeout='" + options.get("reconnect_timeout").toString() + "000'";
        }
        if (options.containsKey("heartbeat")) {
            heartbeat = "&heartbeat='" + options.get("heartbeat").toString() + "'";
        }
        if (options.containsKey("tcp-nodelay")) {
            tcp_nodelay = "&tcp_nodelay='" + options.get("tcp-nodelay").toString() + "'";
        }
        if (options.containsKey("sasl_mechanisms")) {
            sasl_mechs = "&sasl_mechs='" + options.get("sasl_mechanisms").toString() + "'";
        }
        if (options.containsKey("sasl_mechs")) {
            sasl_mechs = "&sasl_mechs='" + options.get("sasl_mechs").toString() + "'";
        }
        if (options.containsKey("sasl_encryption")) {
            sasl_encryption = "&sasl_encryption='" + options.get("sasl_encryption").toString() + "'";
        }
        if (options.containsKey("sasl_protocol")) {
            sasl_protocol = "&sasl_protocol='" + options.get("sasl_protocol").toString() + "'";
        }
        if (options.containsKey("sasl_server")) {
            sasl_server = "&sasl_server='" + options.get("sasl_server").toString() + "'";
        }
        if (options.containsKey("trust_store")) {
            trust_store = "&trust_store='" + options.get("trust_store").toString() + "'";
        }
        if (options.containsKey("trust_store_password")) {
            trust_store_password = "&trust_store_password='" + options.get("trust_store_password").toString() + "'";
        }
        if (options.containsKey("key_store")) {
            key_store = "&key_store='" + options.get("key_store").toString() + "'";
        }
        if (options.containsKey("key_store_password")) {
            key_store_password = "&key_store_password='" + options.get("key_store_password").toString() + "'";
        }
        if (options.containsKey("protocol")) {
            String value = options.get("protocol").toString();
            if (value.equalsIgnoreCase("ssl")) {
                ssl = "&ssl='true'";
                if (options.containsKey("ssl_verify_hostname")) {
                    ssl_verify_hostname = "&ssl_verify_hostname='" + options.get("ssl_verify_hostname").toString() + "'";
                }
                if (options.containsKey("ssl_cert_alias")) {
                    ssl_cert_alias = "&ssl_cert_alias='" + options.get("ssl_cert_alias").toString() + "'";
                }
            }
        }
        urlOptions = maxprefetch + sync_publish + sync_ack + use_legacy_map_msg_format + failover;
        brokerListOptions = heartbeat + retries + connectdelay + connecttimeout + tcp_nodelay + sasl_mechs + sasl_encryption + sasl_protocol + sasl_server + ssl + ssl_verify_hostname + ssl_cert_alias + trust_store + trust_store_password + key_store + key_store_password;
        if (brokerListOptions.startsWith("&")) {
            brokerListOptions = "?" + brokerListOptions.substring(1);
        }
    }
    return parseURL(url, username, password, urlOptions, brokerListOptions);
}
Also used : AddressParser(org.apache.qpid.messaging.util.AddressParser) Map(java.util.Map)

Aggregations

AddressParser (org.apache.qpid.messaging.util.AddressParser)3 JMSException (javax.jms.JMSException)2 MapMessage (javax.jms.MapMessage)2 Handle (org.apache.qpid.qmf2.common.Handle)2 QmfData (org.apache.qpid.qmf2.common.QmfData)2 QmfException (org.apache.qpid.qmf2.common.QmfException)2 Map (java.util.Map)1 Destination (javax.jms.Destination)1 Message (javax.jms.Message)1 AMQPMessage (org.apache.qpid.qmf2.common.AMQPMessage)1