Skip to content

Instantly share code, notes, and snippets.

@aledsage
Created July 16, 2012 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aledsage/3122026 to your computer and use it in GitHub Desktop.
Save aledsage/3122026 to your computer and use it in GitHub Desktop.
Example brooklyn groovy code to poll an underlying resource, to update attributes (aka sensors)
// Polls a given URL, and uses the HTTP response to set the
// two Brooklyn entity attributes SERVICE_UP and REQUEST_COUNT
def http = sensorRegistry.register(
new HttpSensorAdapter("http://$host:$port/management/subsystem/web/connector/http/read-resource",
period: 200*TimeUnit.MILLISECONDS).
vars("include-runtime":true) )
with(http) {
poll(SERVICE_UP) { responseCode==200 }
poll(REQUEST_COUNT) { json.requestCount }
// etc
}
// Subscribes to JMX attribute-change events for given attributes on an given MBean,
// to set the Brooklyn entity attributes ERROR_COUNT and REQUEST_COUNT
JmxSensorAdapter jmx = sensorRegistry.register(new JmxSensorAdapter(period: 500*TimeUnit.MILLISECONDS));
jmx.objectName("jboss.web:type=GlobalRequestProcessor,name=http-*").with {
attribute("errorCount").subscribe(ERROR_COUNT)
attribute("requestCount").subscribe(REQUEST_COUNT)
// etc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment