use of org.apache.qpid.qmf2.console.SubscribeResponseWorkItem 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());
}
}
use of org.apache.qpid.qmf2.console.SubscribeResponseWorkItem 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:<value>, publishInterval:<value>, replyHandle:<value>, timeout:<value>}"</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());
}
}
use of org.apache.qpid.qmf2.console.SubscribeResponseWorkItem in project qpid by apache.
the class AgentSubscriptionTestConsole method onEvent.
public void onEvent(WorkItem wi) {
System.out.println("WorkItem type: " + wi.getType());
if (wi.getType() == AGENT_HEARTBEAT) {
AgentHeartbeatWorkItem item = (AgentHeartbeatWorkItem) wi;
Agent agent = item.getAgent();
System.out.println(agent.getName());
}
if (wi.getType() == EVENT_RECEIVED) {
EventReceivedWorkItem item = (EventReceivedWorkItem) wi;
Agent agent = item.getAgent();
QmfEvent event = item.getEvent();
String className = event.getSchemaClassId().getClassName();
System.out.println("Event: " + className);
//event.listValues();
}
if (wi.getType() == METHOD_RESPONSE) {
MethodResponseWorkItem item = (MethodResponseWorkItem) wi;
MethodResult result = item.getMethodResult();
String correlationId = item.getHandle().getCorrelationId();
System.out.println("correlationId = " + correlationId);
System.out.println(result.getStringValue("message"));
}
if (wi.getType() == OBJECT_UPDATE) {
ObjectUpdateWorkItem item = (ObjectUpdateWorkItem) wi;
QmfConsoleData object = item.getQmfConsoleData();
ObjectId objectId = object.getObjectId();
String correlationId = item.getHandle().getCorrelationId();
System.out.println("correlationId = " + correlationId);
System.out.println("objectId = " + objectId);
System.out.println("MethodCount = " + object.getLongValue("methodCount"));
}
if (wi.getType() == SUBSCRIBE_RESPONSE) {
SubscribeResponseWorkItem item = (SubscribeResponseWorkItem) wi;
SubscribeParams params = item.getSubscribeParams();
System.out.println("duration = " + params.getLifetime());
System.out.println("interval = " + params.getPublishInterval());
System.out.println("subscriptionId = " + params.getSubscriptionId());
System.out.println("consoleHandle = " + params.getConsoleHandle());
String correlationId = item.getHandle().getCorrelationId();
System.out.println("correlationId = " + correlationId);
}
if (wi.getType() == SUBSCRIPTION_INDICATION) {
SubscriptionIndicationWorkItem item = (SubscriptionIndicationWorkItem) wi;
SubscribeIndication indication = item.getSubscribeIndication();
String correlationId = indication.getConsoleHandle();
System.out.println("correlationId = " + correlationId);
List<QmfConsoleData> objects = indication.getData();
for (QmfConsoleData object : objects) {
if (object.isDeleted()) {
System.out.println("object has been deleted");
}
System.out.println("offset = " + object.getValue("offset"));
}
}
}
use of org.apache.qpid.qmf2.console.SubscribeResponseWorkItem in project qpid by apache.
the class BrokerSubscriptionTestConsole method onEvent.
public void onEvent(WorkItem wi) {
System.out.println("WorkItem type: " + wi.getType());
if (wi.getType() == AGENT_HEARTBEAT) {
AgentHeartbeatWorkItem item = (AgentHeartbeatWorkItem) wi;
Agent agent = item.getAgent();
System.out.println(agent.getName());
}
if (wi.getType() == EVENT_RECEIVED) {
EventReceivedWorkItem item = (EventReceivedWorkItem) wi;
Agent agent = item.getAgent();
QmfEvent event = item.getEvent();
String className = event.getSchemaClassId().getClassName();
System.out.println("Event: " + className);
//event.listValues();
}
if (wi.getType() == METHOD_RESPONSE) {
MethodResponseWorkItem item = (MethodResponseWorkItem) wi;
MethodResult result = item.getMethodResult();
String correlationId = item.getHandle().getCorrelationId();
System.out.println("correlationId = " + correlationId);
System.out.println(result.getStringValue("message"));
}
if (wi.getType() == OBJECT_UPDATE) {
ObjectUpdateWorkItem item = (ObjectUpdateWorkItem) wi;
QmfConsoleData object = item.getQmfConsoleData();
ObjectId objectId = object.getObjectId();
String correlationId = item.getHandle().getCorrelationId();
System.out.println("correlationId = " + correlationId);
System.out.println("objectId = " + objectId);
System.out.println("MethodCount = " + object.getLongValue("methodCount"));
}
if (wi.getType() == SUBSCRIBE_RESPONSE) {
SubscribeResponseWorkItem item = (SubscribeResponseWorkItem) wi;
SubscribeParams params = item.getSubscribeParams();
System.out.println("duration = " + params.getLifetime());
System.out.println("interval = " + params.getPublishInterval());
System.out.println("subscriptionId = " + params.getSubscriptionId());
System.out.println("consoleHandle = " + params.getConsoleHandle());
String correlationId = item.getHandle().getCorrelationId();
System.out.println("correlationId = " + correlationId);
}
if (wi.getType() == SUBSCRIPTION_INDICATION) {
SubscriptionIndicationWorkItem item = (SubscriptionIndicationWorkItem) wi;
SubscribeIndication indication = item.getSubscribeIndication();
String correlationId = indication.getConsoleHandle();
System.out.println("correlationId = " + correlationId);
List<QmfConsoleData> objects = indication.getData();
for (QmfConsoleData object : objects) {
if (object.isDeleted()) {
System.out.println("object has been deleted");
}
String className = object.getSchemaClassId().getClassName();
System.out.println("object class = " + className);
if (className.equals("queue") || className.equals("exchange")) {
if (object.hasValue("name")) {
System.out.println("property update, name = " + object.getStringValue("name"));
} else {
_objectId = object.getObjectId();
System.out.println("statistic update, oid = " + _objectId);
}
}
}
}
}
use of org.apache.qpid.qmf2.console.SubscribeResponseWorkItem 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:<value>, replyHandle:<value>, timeout:<value>}"</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());
}
}
Aggregations