use of org.apache.qpid.qmf2.common.QmfQuery in project qpid by apache.
the class Test4 method evaluateDataQuery.
public List<QmfConsoleData> evaluateDataQuery(QmfQuery query) {
List<QmfConsoleData> results = new ArrayList<QmfConsoleData>();
if (query.getObjectId() != null) {
// Look up a QmfAgentData object by the ObjectId obtained from the query
ObjectId objectId = query.getObjectId();
QmfAgentData object = _objectIndex.get(objectId);
if (object != null && !object.isDeleted()) {
results.add(new QmfConsoleData(object.mapEncode(), null));
}
} else {
for (QmfAgentData object : _objectIndex.values()) {
if (!object.isDeleted() && query.evaluate(object)) {
results.add(new QmfConsoleData(object.mapEncode(), null));
}
}
}
return results;
}
use of org.apache.qpid.qmf2.common.QmfQuery in project qpid by apache.
the class QpidQueueStats method createQueueSubscription.
/**
* Create a Subscription to query for all queue objects
*/
private void createQueueSubscription() {
try {
// This QmfQuery simply does an ID query for objects with the className "queue"
QmfQuery query = new QmfQuery(QmfQueryTarget.OBJECT, new SchemaClassId("queue"));
SubscribeParams params = _console.createSubscription(_broker, query, "queueStatsHandle");
_subscriptionId = params.getSubscriptionId();
// Subtract 10 as we want to refresh before it times out
_subscriptionDuration = params.getLifetime() - 10;
_startTime = System.currentTimeMillis();
} catch (QmfException qmfe) {
}
}
use of org.apache.qpid.qmf2.common.QmfQuery in project qpid by apache.
the class Agent method onMessage.
// MessageListener
// ********************************************************************************************************
/**
* MessageListener for QMF2 Console requests.
*
* @param message the JMS Message passed to the listener.
*/
public final void onMessage(final Message message) {
try {
String agentName = QmfData.getString(message.getObjectProperty("qmf.agent"));
String content = QmfData.getString(message.getObjectProperty("qmf.content"));
String opcode = QmfData.getString(message.getObjectProperty("qmf.opcode"));
// String routingKey = ((javax.jms.Topic)message.getJMSDestination()).getTopicName();
// String contentType = ((org.apache.qpid.client.message.AbstractJMSMessage)message).getContentType();
// System.out.println();
// System.out.println("agentName = " + agentName);
// System.out.println("content = " + content);
// System.out.println("opcode = " + opcode);
// System.out.println("routingKey = " + routingKey);
// System.out.println("contentType = " + contentType);
Handle handle = new Handle(message.getJMSCorrelationID(), message.getJMSReplyTo());
if (opcode.equals("_agent_locate_request")) {
handleLocateRequest(handle);
} else if (opcode.equals("_method_request")) {
if (AMQPMessage.isAMQPMap(message)) {
_eventListener.onEvent(new MethodCallWorkItem(handle, new MethodCallParams(AMQPMessage.getMap(message))));
} else {
_log.info("onMessage() Received Method Request message in incorrect format");
}
} else if (opcode.equals("_query_request")) {
if (AMQPMessage.isAMQPMap(message)) {
try {
QmfQuery query = new QmfQuery(AMQPMessage.getMap(message));
handleQueryRequest(handle, query);
} catch (QmfException qmfe) {
raiseException(handle, "Query Request failed, invalid Query: " + qmfe.getMessage());
}
} else {
_log.info("onMessage() Received Query Request message in incorrect format");
}
} else if (opcode.equals("_subscribe_request")) {
if (AMQPMessage.isAMQPMap(message)) {
try {
SubscriptionParams subscriptionParams = new SubscriptionParams(handle, AMQPMessage.getMap(message));
if (this instanceof AgentExternal) {
_eventListener.onEvent(new SubscribeRequestWorkItem(handle, subscriptionParams));
} else {
Subscription subscription = new Subscription(this, subscriptionParams);
String subscriptionId = subscription.getSubscriptionId();
_subscriptions.put(subscriptionId, subscription);
_timer.schedule(subscription, 0, subscriptionParams.getPublishInterval());
subscriptionResponse(handle, subscription.getConsoleHandle(), subscriptionId, subscription.getDuration(), subscription.getInterval(), null);
}
} catch (QmfException qmfe) {
raiseException(handle, "Subscribe Request failed, invalid Query: " + qmfe.getMessage());
}
} else {
_log.info("onMessage() Received Subscribe Request message in incorrect format");
}
} else if (opcode.equals("_subscribe_refresh_indication")) {
if (AMQPMessage.isAMQPMap(message)) {
ResubscribeParams resubscribeParams = new ResubscribeParams(AMQPMessage.getMap(message));
if (this instanceof AgentExternal) {
_eventListener.onEvent(new ResubscribeRequestWorkItem(handle, resubscribeParams));
} else {
String subscriptionId = resubscribeParams.getSubscriptionId();
Subscription subscription = _subscriptions.get(subscriptionId);
if (subscription != null) {
subscription.refresh(resubscribeParams);
subscriptionResponse(handle, subscription.getConsoleHandle(), subscription.getSubscriptionId(), subscription.getDuration(), subscription.getInterval(), null);
}
}
} else {
_log.info("onMessage() Received Resubscribe Request message in incorrect format");
}
} else if (opcode.equals("_subscribe_cancel_indication")) {
if (AMQPMessage.isAMQPMap(message)) {
QmfData qmfSubscribe = new QmfData(AMQPMessage.getMap(message));
String subscriptionId = qmfSubscribe.getStringValue("_subscription_id");
if (this instanceof AgentExternal) {
_eventListener.onEvent(new UnsubscribeRequestWorkItem(subscriptionId));
} else {
Subscription subscription = _subscriptions.get(subscriptionId);
if (subscription != null) {
subscription.cancel();
}
}
} else {
_log.info("onMessage() Received Subscribe Cancel Request message in incorrect format");
}
}
} catch (JMSException jmse) {
_log.info("JMSException {} caught in onMessage()", jmse.getMessage());
}
}
use of org.apache.qpid.qmf2.common.QmfQuery in project qpid by apache.
the class Agent method evaluateQuery.
/**
* This method evaluates a QmfQuery over the Agent's data on behalf of a Subscription.
*
* @param query the QmfQuery that the Subscription wants to be evaluated over the Agent's data.
* @return a List of QmfAgentData objects that match the specified QmfQuery.
*/
public final List<QmfAgentData> evaluateQuery(final QmfQuery query) {
List<QmfAgentData> results = new ArrayList<QmfAgentData>(_objectIndex.size());
if (query.getTarget() == QmfQueryTarget.OBJECT) {
// destroyed we asynchronously publish its new state to subscribers, see QmfAgentData.destroy() method.
if (query.getObjectId() != null) {
// Look up a QmfAgentData object by the ObjectId obtained from the query
ObjectId objectId = query.getObjectId();
QmfAgentData object = _objectIndex.get(objectId);
if (object != null && !object.isDeleted()) {
results.add(object);
}
} else {
// Look up QmfAgentData objects evaluating the query
for (QmfAgentData object : _objectIndex.values()) {
if (!object.isDeleted() && query.evaluate(object)) {
results.add(object);
}
}
}
}
return results;
}
use of org.apache.qpid.qmf2.common.QmfQuery in project qpid by apache.
the class Console method onMessage.
/**
* MessageListener for QMF2 Agent Events, Hearbeats and Asynchronous data indications
*
* @param message the JMS Message passed to the listener
*/
public void onMessage(Message message) {
try {
String agentName = QmfData.getString(message.getObjectProperty("qmf.agent"));
String content = QmfData.getString(message.getObjectProperty("qmf.content"));
String opcode = QmfData.getString(message.getObjectProperty("qmf.opcode"));
if (opcode.equals("_agent_heartbeat_indication") || opcode.equals("_agent_locate_response")) {
// This block handles Agent lifecycle information (discover, register, delete)
if (_agents.containsKey(agentName)) {
// This block handles Agents that have previously been registered
Agent agent = _agents.get(agentName);
long originalEpoch = agent.getEpoch();
// If we already know about an Agent we simply update the Agent's state using initialise()
agent.initialise(AMQPMessage.getMap(message));
// If the Epoch has changed it means the Agent has been restarted so we send a notification
if (agent.getEpoch() != originalEpoch) {
// Clear cache to force a lookup
agent.clearSchemaCache();
List<SchemaClassId> classes = getClasses(agent);
// Discover the schema for this Agent and cache it
getSchema(classes, agent);
_log.info("Agent {} has been restarted", agentName);
if (_discoverAgents && (_agentQuery == null || _agentQuery.evaluate(agent))) {
_eventListener.onEvent(new AgentRestartedWorkItem(agent));
}
} else {
// Otherwise just send a heartbeat notification
_log.info("Agent {} heartbeat", agent.getName());
if (_discoverAgents && (_agentQuery == null || _agentQuery.evaluate(agent))) {
_eventListener.onEvent(new AgentHeartbeatWorkItem(agent));
}
}
} else {
// This block handles Agents that haven't already been registered
Agent agent = new Agent(AMQPMessage.getMap(message), this);
List<SchemaClassId> classes = getClasses(agent);
// Discover the schema for this Agent and cache it
getSchema(classes, agent);
_agents.put(agentName, agent);
_log.info("Adding Agent {}", agentName);
// the Agent more "user friendly" than using the full Agent name.
if (agent.getVendor().equals("apache.org") && agent.getProduct().equals("qpidd")) {
_log.info("Recording {} as _brokerAgentName", agentName);
_brokerAgentName = agentName;
}
// wait for the broker Agent to become available.
if (_brokerAgentName != null) {
synchronized (this) {
_agentAvailable = true;
notifyAll();
}
}
if (_discoverAgents && (_agentQuery == null || _agentQuery.evaluate(agent))) {
_eventListener.onEvent(new AgentAddedWorkItem(agent));
}
}
// The broker Agent sends periodic heartbeats and that Agent should *always* be available given
// a running broker, so we should get here every "--mgmt-pub-interval" seconds or so, so it's
// a good place to periodically check for the expiry of any other Agents.
handleAgentExpiry();
return;
}
if (!_agents.containsKey(agentName)) {
_log.info("Ignoring Event from unregistered Agent {}", agentName);
return;
}
Agent agent = _agents.get(agentName);
if (!agent.eventsEnabled()) {
_log.info("{} has disabled Event reception, ignoring Event", agentName);
return;
}
// If we get to here the Agent from whence the Event came should be registered and should
// have Event reception enabled, so we should be able to send events to the EventListener
Handle handle = new Handle(message.getJMSCorrelationID());
if (opcode.equals("_method_response") || opcode.equals("_exception")) {
if (AMQPMessage.isAMQPMap(message)) {
_eventListener.onEvent(new MethodResponseWorkItem(handle, new MethodResult(AMQPMessage.getMap(message))));
} else {
_log.info("onMessage() Received Method Response message in incorrect format");
}
}
// refresh() call on QmfConsoleData so the number of results in the returned list *should* be one.
if (opcode.equals("_query_response") && content.equals("_data")) {
if (AMQPMessage.isAMQPList(message)) {
List<Map> list = AMQPMessage.getList(message);
for (Map m : list) {
_eventListener.onEvent(new ObjectUpdateWorkItem(handle, new QmfConsoleData(m, agent)));
}
} else {
_log.info("onMessage() Received Query Response message in incorrect format");
}
}
// This block handles responses to createSubscription and refreshSubscription
if (opcode.equals("_subscribe_response")) {
if (AMQPMessage.isAMQPMap(message)) {
String correlationId = message.getJMSCorrelationID();
SubscribeParams params = new SubscribeParams(correlationId, AMQPMessage.getMap(message));
String subscriptionId = params.getSubscriptionId();
if (subscriptionId != null && correlationId != null) {
SubscriptionManager subscription = _subscriptionById.get(subscriptionId);
if (subscription == null) {
// This is a createSubscription response so the correlationId should be the consoleHandle
subscription = _subscriptionByHandle.get(correlationId);
if (subscription != null) {
_subscriptionById.put(subscriptionId, subscription);
subscription.setSubscriptionId(subscriptionId);
subscription.setDuration(params.getLifetime());
String replyHandle = subscription.getReplyHandle();
if (replyHandle == null) {
subscription.signal();
} else {
_eventListener.onEvent(new SubscribeResponseWorkItem(new Handle(replyHandle), params));
}
}
} else {
// This is a refreshSubscription response
params.setConsoleHandle(subscription.getConsoleHandle());
subscription.setDuration(params.getLifetime());
subscription.refresh();
_eventListener.onEvent(new SubscribeResponseWorkItem(handle, params));
}
}
} else {
_log.info("onMessage() Received Subscribe Response message in incorrect format");
}
}
// Subscription Indication - in other words the asynchronous results of a Subscription
if (opcode.equals("_data_indication") && content.equals("_data")) {
if (AMQPMessage.isAMQPList(message)) {
String consoleHandle = handle.getCorrelationId();
if (consoleHandle != null && _subscriptionByHandle.containsKey(consoleHandle)) {
// If we have a valid consoleHandle the data has come from a "real" Subscription.
List<Map> list = AMQPMessage.getList(message);
List<QmfConsoleData> resultList = new ArrayList<QmfConsoleData>(list.size());
for (Map m : list) {
resultList.add(new QmfConsoleData(m, agent));
}
_eventListener.onEvent(new SubscriptionIndicationWorkItem(new SubscribeIndication(consoleHandle, resultList)));
} else if (_subscriptionEmulationEnabled && agentName.equals(_brokerAgentName)) {
// If the data has come from is the broker Agent we emulate a Subscription on the Console
for (SubscriptionManager subscription : _subscriptionByHandle.values()) {
QmfQuery query = subscription.getQuery();
if (subscription.getAgent().getName().equals(_brokerAgentName) && query.getTarget() == QmfQueryTarget.OBJECT) {
// Only evaluate broker Agent subscriptions with QueryTarget == OBJECT on the Console.
long objectEpoch = 0;
consoleHandle = subscription.getConsoleHandle();
List<Map> list = AMQPMessage.getList(message);
List<QmfConsoleData> resultList = new ArrayList<QmfConsoleData>(list.size());
for (Map m : list) {
// Evaluate the QmfConsoleData object against the query
QmfConsoleData object = new QmfConsoleData(m, agent);
if (query.evaluate(object)) {
long epoch = object.getObjectId().getAgentEpoch();
objectEpoch = (epoch > objectEpoch && !object.isDeleted()) ? epoch : objectEpoch;
resultList.add(object);
}
}
if (resultList.size() > 0) {
// data from the restarted Agent (in case they need to reset any state).
if (objectEpoch > agent.getEpoch()) {
agent.setEpoch(objectEpoch);
// Clear cache to force a lookup
agent.clearSchemaCache();
List<SchemaClassId> classes = getClasses(agent);
// Discover the schema for this Agent and cache it
getSchema(classes, agent);
_log.info("Agent {} has been restarted", agentName);
if (_discoverAgents && (_agentQuery == null || _agentQuery.evaluate(agent))) {
_eventListener.onEvent(new AgentRestartedWorkItem(agent));
}
}
_eventListener.onEvent(new SubscriptionIndicationWorkItem(new SubscribeIndication(consoleHandle, resultList)));
}
}
}
}
} else {
_log.info("onMessage() Received Subscribe Indication message in incorrect format");
}
}
// The results of an Event delivered from an Agent
if (opcode.equals("_data_indication") && content.equals("_event")) {
// There are differences in the type of message sent by Qpid 0.8 and 0.10 onwards.
if (AMQPMessage.isAMQPMap(message)) {
// 0.8 broker passes Events as amqp/map encoded as MapMessages (we convert into java.util.Map)
_eventListener.onEvent(new EventReceivedWorkItem(agent, new QmfEvent(AMQPMessage.getMap(message))));
} else if (AMQPMessage.isAMQPList(message)) {
// 0.10 and above broker passes Events as amqp/list encoded as BytesMessage (needs decoding)
// 0.20 encodes amqp/list in a MapMessage!!?? AMQPMessage hopefully abstracts this detail.
List<Map> list = AMQPMessage.getList(message);
for (Map m : list) {
_eventListener.onEvent(new EventReceivedWorkItem(agent, new QmfEvent(m)));
}
} else {
_log.info("onMessage() Received Event message in incorrect format");
}
}
} catch (JMSException jmse) {
_log.info("JMSException {} caught in onMessage()", jmse.getMessage());
}
}
Aggregations