Search in sources :

Example 31 with MapMessage

use of javax.jms.MapMessage in project qpid by apache.

the class Console method cancelSubscription.

// end of refreshSubscription()
/**
 * Terminates the given subscription.
 *
 * @param subscriptionId the ID of the subscription to be cancelled
 */
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 {
        MapMessage request = _syncSession.createMapMessage();
        request.setStringProperty("x-amqp-0-10.app-id", "qmf2");
        request.setStringProperty("method", "request");
        request.setStringProperty("qmf.opcode", "_subscribe_cancel_indication");
        request.setStringProperty("qpid.subject", agentName);
        request.setObject("_subscription_id", subscriptionId);
        synchronized (this) {
            if (!_subscriptionEmulationEnabled || !agentName.equals(_brokerAgentName)) {
                _requester.send(request);
            }
        }
        subscription.cancel();
    } catch (JMSException jmse) {
        _log.info("JMSException {} caught in cancelSubscription()", jmse.getMessage());
    }
}
Also used : MapMessage(javax.jms.MapMessage) JMSException(javax.jms.JMSException) QmfException(org.apache.qpid.qmf2.common.QmfException)

Example 32 with MapMessage

use of javax.jms.MapMessage in project qpid by apache.

the class Console method getClasses.

/**
 * Return a list of SchemaClassIds for all available Schema for the specified Agent.
 * @param agent the Agent being queried
 * @return a list of SchemaClassIds for all available Schema for the specified Agent.
 */
public List<SchemaClassId> getClasses(final Agent agent) {
    // First look to see if there are cached results and if there are return those.
    List<SchemaClassId> results = agent.getClasses();
    if (results.size() > 0) {
        return results;
    }
    String agentName = agent.getName();
    results = new ArrayList<SchemaClassId>();
    try {
        MapMessage request = _syncSession.createMapMessage();
        request.setJMSReplyTo(_replyAddress);
        request.setStringProperty("x-amqp-0-10.app-id", "qmf2");
        request.setStringProperty("method", "request");
        request.setStringProperty("qmf.opcode", "_query_request");
        request.setStringProperty("qpid.subject", agentName);
        // Create a QMF Query for an "SCHEMA_ID" target
        request.setObject("_what", "SCHEMA_ID");
        // it would be somewhat unfortunate if their response got interleaved with ours!!
        synchronized (this) {
            _requester.send(request);
            Message response = _responder.receive(_replyTimeout * 1000);
            if (response == null) {
                _log.info("No response received in getClasses()");
                return Collections.emptyList();
            }
            if (AMQPMessage.isAMQPList(response)) {
                List<Map> mapResults = AMQPMessage.getList(response);
                for (Map content : mapResults) {
                    // new SchemaClassId(content).listValues();
                    results.add(new SchemaClassId(content));
                }
            } else if (AMQPMessage.isAMQPMap(response)) {
            // Error responses are returned as MapMessages, though they are being ignored here.
            // System.out.println("Console.getClasses() no results for " + agentName);
            // QmfData exception = new QmfData(AMQPMessage.getMap(response));
            // System.out.println(agentName + " " + exception.getStringValue("error_text"));
            } else {
                _log.info("getClasses() Received response message in incorrect format");
            }
        }
    } catch (JMSException jmse) {
        _log.info("JMSException {} caught in getClasses()", jmse.getMessage());
    }
    agent.setClasses(results);
    return results;
}
Also used : AMQPMessage(org.apache.qpid.qmf2.common.AMQPMessage) MapMessage(javax.jms.MapMessage) Message(javax.jms.Message) MapMessage(javax.jms.MapMessage) JMSException(javax.jms.JMSException) SchemaClassId(org.apache.qpid.qmf2.common.SchemaClassId) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 33 with MapMessage

use of javax.jms.MapMessage 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)

Aggregations

MapMessage (javax.jms.MapMessage)33 JMSException (javax.jms.JMSException)24 HashMap (java.util.HashMap)13 TextMessage (javax.jms.TextMessage)11 Map (java.util.Map)10 BytesMessage (javax.jms.BytesMessage)10 Message (javax.jms.Message)8 ObjectMessage (javax.jms.ObjectMessage)8 Session (javax.jms.Session)8 Destination (javax.jms.Destination)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 Test (org.junit.Test)6 AMQPMessage (org.apache.qpid.qmf2.common.AMQPMessage)5 Connection (javax.jms.Connection)4 MessageProducer (javax.jms.MessageProducer)4 QmfException (org.apache.qpid.qmf2.common.QmfException)4 Enumeration (java.util.Enumeration)3 MessageFormatException (javax.jms.MessageFormatException)3 StreamMessage (javax.jms.StreamMessage)3 MOMException (cern.cmw.mom.pubsub.MOMException)2