Skip to content

Instantly share code, notes, and snippets.

@aledsage
Created July 16, 2012 11:00
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/3122130 to your computer and use it in GitHub Desktop.
Save aledsage/3122130 to your computer and use it in GitHub Desktop.
Proposed brooklyn Java code to poll an underlying resource, to update attributes (aka sensors)
// Polls the given JMX attribute every minute; transforms the result from tabular data
// into a java.util.Map and sets the given brooklyn sensor (aka attribute) accordingly.
// The onSuccess is passed the JMX attribute's value.
JmxSensorAdapter jmxAdapter = registry.register(new JmxSensorAdapter());
jmxAdapter.withObjectName(myJmxObjectName)
.pollAttribute(new JmxAttributePollConfig<Map>(MY_BROOKLYN_SENSOR)
.attributeName(myJmxAttributeName)
.period(1, TimeUnit.MINUTES)
.onSuccess(JmxResponseFunctions.tabularDataToMap()));
// Polls URLs, and uses the HTTP response to set a Brooklyn entity attribute (aka sensor).
//
// The argument to onSuccess is a HttpPollResponse, which has things like:
// getStatusLine (which as getStatusCode, getReasonPhrase, getProtocolVersion)
// getHeaderLists (which returns Map<String,List<String>>)
// getContents (which returns byte[])
String baseUrl = "http://"+host+":"+port+"/management/";
HttpSensorAdapter httpAdapter = registry.register(new HttpSensorAdapter2(baseUrl));
httpAdapter.withSubUrl("subsystem/web/connector/http/read-resource")
.poll(new HttpPollConfig<String>(MY_BROOKLYN_SENSOR)
.onSuccess(HttpResponseFunctions.stringContentsFunction())
.onError("blah blah");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment