Search in sources :

Example 26 with MapMessage

use of javax.jms.MapMessage in project apex-malhar by apache.

the class ActiveMQMultiTypeMessageListener method onMessage.

@Override
public void onMessage(Message message) {
    super.onMessage(message);
    if (message instanceof TextMessage) {
        TextMessage txtMsg = (TextMessage) message;
        String msg = null;
        try {
            msg = txtMsg.getText();
            receivedData.put(countMessages, msg);
        } catch (JMSException ex) {
            logger.debug(ex.getLocalizedMessage());
        }
        logger.debug("Received a TextMessage: {}", msg);
    } else if (message instanceof MapMessage) {
        MapMessage mapMsg = (MapMessage) message;
        Map map = new HashMap();
        try {
            Enumeration en = mapMsg.getMapNames();
            while (en.hasMoreElements()) {
                String key = (String) en.nextElement();
                map.put(key, mapMsg.getObject(key));
            }
            receivedData.put(countMessages, map);
        } catch (JMSException ex) {
            logger.debug(ex.getLocalizedMessage());
        }
        logger.debug("Received a MapMessage: {}", map);
    } else if (message instanceof BytesMessage) {
        BytesMessage bytesMsg = (BytesMessage) message;
        try {
            byte[] byteArr = new byte[(int) bytesMsg.getBodyLength()];
            bytesMsg.readBytes(byteArr);
            receivedData.put(countMessages, byteArr);
        } catch (JMSException ex) {
            logger.debug(ex.getLocalizedMessage());
        }
        logger.debug("Received a ByteMessage: {}", bytesMsg);
    } else if (message instanceof ObjectMessage) {
        ObjectMessage objMsg = (ObjectMessage) message;
        Object msg = null;
        try {
            msg = objMsg.getObject();
            receivedData.put(countMessages, msg);
        } catch (JMSException ex) {
            logger.debug(ex.getLocalizedMessage());
        }
        logger.debug("Received an ObjectMessage: {}", msg);
    } else {
        throw new IllegalArgumentException("Unhandled message type " + message.getClass().getName());
    }
}
Also used : Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) ObjectMessage(javax.jms.ObjectMessage) MapMessage(javax.jms.MapMessage) JMSException(javax.jms.JMSException) BytesMessage(javax.jms.BytesMessage) Map(java.util.Map) HashMap(java.util.HashMap) TextMessage(javax.jms.TextMessage)

Example 27 with MapMessage

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

the class Agent method raiseException.

/**
 * Send an exception back to the Console.
 * @param handle the reply handle that contains the replyTo Address.
 * @param message the exception message.
 */
public final void raiseException(final Handle handle, final String message) {
    try {
        MapMessage response = _syncSession.createMapMessage();
        response.setJMSCorrelationID(handle.getCorrelationId());
        response.setStringProperty("x-amqp-0-10.app-id", "qmf2");
        response.setStringProperty("method", "response");
        response.setStringProperty("qmf.opcode", "_exception");
        response.setStringProperty("qmf.agent", _name);
        response.setStringProperty("qpid.subject", handle.getRoutingKey());
        QmfData exception = new QmfData();
        exception.setValue("error_text", message);
        response.setObject("_values", exception.mapEncode());
        sendResponse(handle, response);
    } catch (JMSException jmse) {
        _log.info("JMSException {} caught in handleLocateRequest()", jmse.getMessage());
    }
}
Also used : QmfData(org.apache.qpid.qmf2.common.QmfData) MapMessage(javax.jms.MapMessage) JMSException(javax.jms.JMSException)

Example 28 with MapMessage

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

the class Agent method subscriptionResponse.

/**
 * If the subscription request is successful, the Agent application must provide a unique subscriptionId.
 * <p>
 * If replying to a sucessful subscription refresh, the original subscriptionId must be supplied.
 * <p>
 * If the subscription or refresh fails, the subscriptionId should be set to null and error may be set to
 * an application-specific QmfData instance that describes the error.
 * <p>
 * Should a refresh request fail, the consoleHandle may be set to null if unknown.
 *
 * @param handle the handle from the WorkItem.
 * @param consoleHandle the console reply handle.
 * @param subscriptionId a unique handle for the subscription supplied by the Agent.
 * @param lifetime should be set to the duration of the subscription in seconds.
 * @param publishInterval should be set to the time interval in seconds between successive publications
 *          on this subscription.
 * @param error an application-specific QmfData instance that describes the error.
 */
public final void subscriptionResponse(final Handle handle, final Handle consoleHandle, final String subscriptionId, final long lifetime, final long publishInterval, final QmfData error) {
    try {
        MapMessage response = _syncSession.createMapMessage();
        response.setJMSCorrelationID(handle.getCorrelationId());
        response.setStringProperty("x-amqp-0-10.app-id", "qmf2");
        response.setStringProperty("method", "response");
        response.setStringProperty("qmf.opcode", "_subscribe_response");
        response.setStringProperty("qmf.agent", _name);
        response.setStringProperty("qpid.subject", handle.getRoutingKey());
        if (error == null) {
            response.setObject("_subscription_id", subscriptionId);
            response.setObject("_duration", lifetime);
            response.setObject("_interval", publishInterval);
        } else {
            Map<String, Object> errorMap = error.mapEncode();
            for (Map.Entry<String, Object> entry : errorMap.entrySet()) {
                response.setObject(entry.getKey(), entry.getValue());
            }
        }
        sendResponse(handle, response);
    } catch (JMSException jmse) {
        _log.info("JMSException {} caught in subscriptionResponse()", jmse.getMessage());
    }
}
Also used : MapMessage(javax.jms.MapMessage) JMSException(javax.jms.JMSException) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 29 with MapMessage

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

the class Console method getObjects.

/**
 * Perform a query for QmfConsoleData objects. Returns a list (possibly empty) of matching objects.
 * If replyHandle is null this method will block until the agent replies, or the timeout expires.
 * Once the timeout expires, all data retrieved to date is returned. If replyHandle is non-null an
 * asynchronous request is performed
 *
 * @param agent the Agent being queried
 * @param query the ObjectId or SchemaClassId being queried for.
 * @param replyHandle the correlation handle used to tie asynchronous method requests with responses
 * @param timeout the time to wait for a reply from the Agent, a value of -1 means use the default timeout
 * @return a List of QMF Objects describing that class
 */
private List<QmfConsoleData> getObjects(final Agent agent, final QmfData query, final String replyHandle, int timeout) {
    String agentName = agent.getName();
    timeout = (timeout < 1) ? _replyTimeout : timeout;
    List<QmfConsoleData> results = Collections.emptyList();
    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", "_query_request");
        request.setStringProperty("qpid.subject", agentName);
        // Create a QMF Query for an "OBJECT" target using either a schema ID or object ID
        String queryType = (query instanceof SchemaClassId) ? "_schema_id" : "_object_id";
        request.setObject("_what", "OBJECT");
        request.setObject(queryType, query.mapEncode());
        // it would be somewhat unfortunate if their response got interleaved with ours!!
        synchronized (this) {
            _requester.send(request);
            if (replyHandle == null) {
                boolean lastResult = true;
                ArrayList<QmfConsoleData> partials = new ArrayList<QmfConsoleData>();
                do {
                    // Wrap in a do/while loop to cater for the case where the Agent may send partial results.
                    Message response = _responder.receive(timeout * 1000);
                    if (response == null) {
                        _log.info("No response received in getObjects()");
                        return partials;
                    }
                    lastResult = !response.propertyExists("partial");
                    if (AMQPMessage.isAMQPList(response)) {
                        List<Map> mapResults = AMQPMessage.getList(response);
                        partials.ensureCapacity(partials.size() + mapResults.size());
                        for (Map content : mapResults) {
                            partials.add(new QmfConsoleData(content, agent));
                        }
                    } else if (AMQPMessage.isAMQPMap(response)) {
                    // Error responses are returned as MapMessages, though they are being ignored here.
                    // QmfData exception = new QmfData(AMQPMessage.getMap(response));
                    // System.out.println(agentName + " " + exception.getStringValue("error_text"));
                    } else {
                        _log.info("getObjects() Received response message in incorrect format");
                    }
                } while (!lastResult);
                results = partials;
            }
        }
    } catch (JMSException jmse) {
        _log.info("JMSException {} caught in getObjects()", jmse.getMessage());
    }
    return results;
}
Also used : Destination(javax.jms.Destination) AMQPMessage(org.apache.qpid.qmf2.common.AMQPMessage) MapMessage(javax.jms.MapMessage) Message(javax.jms.Message) MapMessage(javax.jms.MapMessage) ArrayList(java.util.ArrayList) 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 30 with MapMessage

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

the class Console method getSchema.

/**
 * Return a list of all available class SchemaClass from a specified Agent.
 * <p>
 * This call will return cached information if it is available.  If not, it will send a query message
 * to the remote agent and block waiting for a response. The timeout argument specifies the maximum time
 * to wait for a response from the agent.
 *
 * @param schemaClassId the SchemaClassId we wish to return schema information for.
 * @param agent the Agent we want to retrieve the schema from
 */
public List<SchemaClass> getSchema(final SchemaClassId schemaClassId, final Agent agent) {
    // First look to see if there are cached results and if there are return those.
    List<SchemaClass> results = agent.getSchema(schemaClassId);
    if (results.size() > 0) {
        return results;
    }
    String agentName = agent.getName();
    // System.out.println("getSchema for agent " + agentName);
    results = new ArrayList<SchemaClass>();
    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" target
        request.setObject("_what", "SCHEMA");
        request.setObject("_schema_id", schemaClassId.mapEncode());
        // 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 getSchema()");
                return Collections.emptyList();
            }
            if (AMQPMessage.isAMQPList(response)) {
                List<Map> mapResults = AMQPMessage.getList(response);
                for (Map content : mapResults) {
                    SchemaClass schema = new SchemaObjectClass(content);
                    if (schema.getClassId().getType().equals("_event")) {
                        schema = new SchemaEventClass(content);
                    }
                    // schema.listValues();
                    results.add(schema);
                }
            } else if (AMQPMessage.isAMQPMap(response)) {
            // Error responses are returned as MapMessages, though they are being ignored here.
            // System.out.println("Console.getSchema() no results for " + agentName);
            // QmfData exception = new QmfData(AMQPMessage.getMap(response));
            // System.out.println(agentName + " " + exception.getStringValue("error_text"));
            } else {
                _log.info("getSchema() Received response message in incorrect format");
            }
        }
    } catch (JMSException jmse) {
        _log.info("JMSException {} caught in getSchema()", jmse.getMessage());
    }
    agent.setSchema(schemaClassId, results);
    return results;
}
Also used : AMQPMessage(org.apache.qpid.qmf2.common.AMQPMessage) MapMessage(javax.jms.MapMessage) Message(javax.jms.Message) SchemaEventClass(org.apache.qpid.qmf2.common.SchemaEventClass) SchemaClass(org.apache.qpid.qmf2.common.SchemaClass) MapMessage(javax.jms.MapMessage) JMSException(javax.jms.JMSException) SchemaObjectClass(org.apache.qpid.qmf2.common.SchemaObjectClass) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

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