Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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