| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
import gls.CompilableTestSupport |
| 20 |
|
| 21 |
class JmxTest extends CompilableTestSupport { |
| 22 |
|
| 23 |
void testIntroduction() { |
| 24 |
shouldCompile ''' |
| 25 |
import javax.management.Attribute |
| 26 |
import javax.management.MBeanServerConnection |
| 27 |
|
| 28 |
MBeanServerConnection server = null |
| 29 |
String beanName = 'Person' |
| 30 |
|
| 31 |
// tag::introduction_example[] |
| 32 |
println server.getAttribute(beanName, 'Age') |
| 33 |
server.setAttribute(beanName, new Attribute('Name', 'New name')) |
| 34 |
Object[] params = [5, 20] |
| 35 |
String[] signature = [Integer.TYPE, Integer.TYPE] |
| 36 |
println server.invoke(beanName, 'add', params, signature) |
| 37 |
// end::introduction_example[] |
| 38 |
|
| 39 |
// tag::introduction_simplified_example[] |
| 40 |
def mbean = new GroovyMBean(server, beanName) |
| 41 |
println mbean.Age |
| 42 |
mbean.Name = 'New name' |
| 43 |
println mbean.add(5, 20) |
| 44 |
// end::introduction_simplified_example[] |
| 45 |
''' |
| 46 |
} |
| 47 |
|
| 48 |
void testTheJvm() { |
| 49 |
shouldCompile ''' |
| 50 |
// tag::the_jvm[] |
| 51 |
import java.lang.management.* |
| 52 |
|
| 53 |
def os = ManagementFactory.operatingSystemMXBean |
| 54 |
println """OPERATING SYSTEM: |
| 55 |
\tarchitecture = $os.arch |
| 56 |
\tname = $os.name |
| 57 |
\tversion = $os.version |
| 58 |
\tprocessors = $os.availableProcessors |
| 59 |
""" |
| 60 |
|
| 61 |
def rt = ManagementFactory.runtimeMXBean |
| 62 |
println """RUNTIME: |
| 63 |
\tname = $rt.name |
| 64 |
\tspec name = $rt.specName |
| 65 |
\tvendor = $rt.specVendor |
| 66 |
\tspec version = $rt.specVersion |
| 67 |
\tmanagement spec version = $rt.managementSpecVersion |
| 68 |
""" |
| 69 |
|
| 70 |
def cl = ManagementFactory.classLoadingMXBean |
| 71 |
println """CLASS LOADING SYSTEM: |
| 72 |
\tisVerbose = ${cl.isVerbose()} |
| 73 |
\tloadedClassCount = $cl.loadedClassCount |
| 74 |
\ttotalLoadedClassCount = $cl.totalLoadedClassCount |
| 75 |
\tunloadedClassCount = $cl.unloadedClassCount |
| 76 |
""" |
| 77 |
|
| 78 |
def comp = ManagementFactory.compilationMXBean |
| 79 |
println """COMPILATION: |
| 80 |
\ttotalCompilationTime = $comp.totalCompilationTime |
| 81 |
""" |
| 82 |
|
| 83 |
def mem = ManagementFactory.memoryMXBean |
| 84 |
def heapUsage = mem.heapMemoryUsage |
| 85 |
def nonHeapUsage = mem.nonHeapMemoryUsage |
| 86 |
println """MEMORY: |
| 87 |
HEAP STORAGE: |
| 88 |
\tcommitted = $heapUsage.committed |
| 89 |
\tinit = $heapUsage.init |
| 90 |
\tmax = $heapUsage.max |
| 91 |
\tused = $heapUsage.used |
| 92 |
NON-HEAP STORAGE: |
| 93 |
\tcommitted = $nonHeapUsage.committed |
| 94 |
\tinit = $nonHeapUsage.init |
| 95 |
\tmax = $nonHeapUsage.max |
| 96 |
\tused = $nonHeapUsage.used |
| 97 |
""" |
| 98 |
|
| 99 |
ManagementFactory.memoryPoolMXBeans.each { mp -> |
| 100 |
println "\tname: " + mp.name |
| 101 |
String[] mmnames = mp.memoryManagerNames |
| 102 |
mmnames.each{ mmname -> |
| 103 |
println "\t\tManager Name: $mmname" |
| 104 |
} |
| 105 |
println "\t\tmtype = $mp.type" |
| 106 |
println "\t\tUsage threshold supported = " + mp.isUsageThresholdSupported() |
| 107 |
} |
| 108 |
println() |
| 109 |
|
| 110 |
def td = ManagementFactory.threadMXBean |
| 111 |
println "THREADS:" |
| 112 |
td.allThreadIds.each { tid -> |
| 113 |
println "\tThread name = ${td.getThreadInfo(tid).threadName}" |
| 114 |
} |
| 115 |
println() |
| 116 |
|
| 117 |
println "GARBAGE COLLECTION:" |
| 118 |
ManagementFactory.garbageCollectorMXBeans.each { gc -> |
| 119 |
println "\tname = $gc.name" |
| 120 |
println "\t\tcollection count = $gc.collectionCount" |
| 121 |
println "\t\tcollection time = $gc.collectionTime" |
| 122 |
String[] mpoolNames = gc.memoryPoolNames |
| 123 |
mpoolNames.each { mpoolName -> |
| 124 |
println "\t\tmpool name = $mpoolName" |
| 125 |
} |
| 126 |
} |
| 127 |
// end::the_jvm[] |
| 128 |
''' |
| 129 |
} |
| 130 |
|
| 131 |
void testTomcat() { |
| 132 |
shouldCompile ''' |
| 133 |
@Grapes([@Grab('org.jfree:jfreechart:1.0.15'), @Grab('org.codehaus.groovy:groovy-swing:2.1.6')]) |
| 134 |
|
| 135 |
// tag::tomcat[] |
| 136 |
import groovy.swing.SwingBuilder |
| 137 |
|
| 138 |
import javax.management.ObjectName |
| 139 |
import javax.management.remote.JMXConnectorFactory as JmxFactory |
| 140 |
import javax.management.remote.JMXServiceURL as JmxUrl |
| 141 |
import javax.swing.WindowConstants as WC |
| 142 |
|
| 143 |
import org.jfree.chart.ChartFactory |
| 144 |
import org.jfree.data.category.DefaultCategoryDataset as Dataset |
| 145 |
import org.jfree.chart.plot.PlotOrientation as Orientation |
| 146 |
|
| 147 |
def serverUrl = 'service:jmx:rmi:///jndi/rmi://localhost:9004/jmxrmi' |
| 148 |
def server = JmxFactory.connect(new JmxUrl(serverUrl)).MBeanServerConnection |
| 149 |
def serverInfo = new GroovyMBean(server, 'Catalina:type=Server').serverInfo |
| 150 |
println "Connected to: $serverInfo" |
| 151 |
|
| 152 |
def query = new ObjectName('Catalina:*') |
| 153 |
String[] allNames = server.queryNames(query, null) |
| 154 |
def modules = allNames.findAll { name -> |
| 155 |
name.contains('j2eeType=WebModule') |
| 156 |
}.collect{ new GroovyMBean(server, it) } |
| 157 |
|
| 158 |
println "Found ${modules.size()} web modules. Processing ..." |
| 159 |
def dataset = new Dataset() |
| 160 |
|
| 161 |
modules.each { m -> |
| 162 |
println m.name() |
| 163 |
dataset.addValue m.processingTime, 0, m.path |
| 164 |
} |
| 165 |
|
| 166 |
def labels = ['Time per Module', 'Module', 'Time'] |
| 167 |
def options = [false, true, true] |
| 168 |
def chart = ChartFactory.createBarChart(*labels, dataset, |
| 169 |
Orientation.VERTICAL, *options) |
| 170 |
def swing = new SwingBuilder() |
| 171 |
def frame = swing.frame(title:'Catalina Module Processing Time', defaultCloseOperation:WC.EXIT_ON_CLOSE) { |
| 172 |
panel(id:'canvas') { rigidArea(width:600, height:250) } |
| 173 |
} |
| 174 |
frame.pack() |
| 175 |
frame.show() |
| 176 |
chart.draw(swing.canvas.graphics, swing.canvas.bounds) |
| 177 |
// end::tomcat[] |
| 178 |
''' |
| 179 |
} |
| 180 |
|
| 181 |
void testWebLogic() { |
| 182 |
shouldCompile ''' |
| 183 |
// tag::weblogic[] |
| 184 |
import javax.management.remote.* |
| 185 |
import javax.management.* |
| 186 |
import javax.naming.Context |
| 187 |
|
| 188 |
def urlRuntime = '/jndi/weblogic.management.mbeanservers.runtime' |
| 189 |
def urlBase = 'service:jmx:t3://localhost:7001' |
| 190 |
|
| 191 |
def serviceURL = new JMXServiceURL(urlBase + urlRuntime) |
| 192 |
def h = new Hashtable() |
| 193 |
h.put(Context.SECURITY_PRINCIPAL, 'weblogic') |
| 194 |
h.put(Context.SECURITY_CREDENTIALS, 'weblogic') |
| 195 |
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, 'weblogic.management.remote') |
| 196 |
def server = JMXConnectorFactory.connect(serviceURL, h).MBeanServerConnection |
| 197 |
def domainName = new ObjectName('com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean') |
| 198 |
def rtName = server.getAttribute(domainName, 'ServerRuntime') |
| 199 |
def rt = new GroovyMBean(server, rtName) |
| 200 |
println "Server: name=$rt.Name, state=$rt.State, version=$rt.WeblogicVersion" |
| 201 |
def destFilter = Query.match(Query.attr('Type'), Query.value('JMSDestinationRuntime')) |
| 202 |
server.queryNames(new ObjectName('com.bea:*'), destFilter).each { name -> |
| 203 |
def jms = new GroovyMBean(server, name) |
| 204 |
println "JMS Destination: name=$jms.Name, type=$jms.DestinationType, messages=$jms.MessagesReceivedCount" |
| 205 |
} |
| 206 |
// end::weblogic[] |
| 207 |
''' |
| 208 |
} |
| 209 |
|
| 210 |
void testSpringClasses() { |
| 211 |
shouldCompile ''' |
| 212 |
@Grab('org.springframework:spring-context:3.2.0.RELEASE') |
| 213 |
// tag::spring_classes[] |
| 214 |
import org.springframework.jmx.export.annotation.* |
| 215 |
|
| 216 |
@ManagedResource(objectName="bean:name=calcMBean", description="Calculator MBean") |
| 217 |
public class Calculator { |
| 218 |
|
| 219 |
private int invocations |
| 220 |
|
| 221 |
@ManagedAttribute(description="The Invocation Attribute") |
| 222 |
public int getInvocations() { |
| 223 |
return invocations |
| 224 |
} |
| 225 |
|
| 226 |
private int base = 10 |
| 227 |
|
| 228 |
@ManagedAttribute(description="The Base to use when adding strings") |
| 229 |
public int getBase() { |
| 230 |
return base |
| 231 |
} |
| 232 |
|
| 233 |
@ManagedAttribute(description="The Base to use when adding strings") |
| 234 |
public void setBase(int base) { |
| 235 |
this.base = base |
| 236 |
} |
| 237 |
|
| 238 |
@ManagedOperation(description="Add two numbers") |
| 239 |
@ManagedOperationParameters([ |
| 240 |
@ManagedOperationParameter(name="x", description="The first number"), |
| 241 |
@ManagedOperationParameter(name="y", description="The second number")]) |
| 242 |
public int add(int x, int y) { |
| 243 |
invocations++ |
| 244 |
return x + y |
| 245 |
} |
| 246 |
|
| 247 |
@ManagedOperation(description="Add two strings representing numbers of a particular base") |
| 248 |
@ManagedOperationParameters([ |
| 249 |
@ManagedOperationParameter(name="x", description="The first number"), |
| 250 |
@ManagedOperationParameter(name="y", description="The second number")]) |
| 251 |
public String addStrings(String x, String y) { |
| 252 |
invocations++ |
| 253 |
def result = Integer.valueOf(x, base) + Integer.valueOf(y, base) |
| 254 |
return Integer.toString(result, base) |
| 255 |
} |
| 256 |
} |
| 257 |
// end::spring_classes[] |
| 258 |
''' |
| 259 |
} |
| 260 |
|
| 261 |
void testSpringUsage() { |
| 262 |
shouldCompile ''' |
| 263 |
@Grab('org.springframework:spring-context:3.2.0.RELEASE') |
| 264 |
// tag::spring_usage[] |
| 265 |
import org.springframework.context.support.ClassPathXmlApplicationContext |
| 266 |
import java.lang.management.ManagementFactory |
| 267 |
import javax.management.ObjectName |
| 268 |
import javax.management.Attribute |
| 269 |
|
| 270 |
// get normal bean |
| 271 |
def ctx = new ClassPathXmlApplicationContext("beans.xml") |
| 272 |
def calc = ctx.getBean("calcBean") |
| 273 |
|
| 274 |
Thread.start { |
| 275 |
// access bean via JMX, use a separate thread just to |
| 276 |
// show that we could access remotely if we wanted |
| 277 |
def server = ManagementFactory.platformMBeanServer |
| 278 |
def mbean = new GroovyMBean(server, 'bean:name=calcMBean') |
| 279 |
sleep 1000 |
| 280 |
assert 8 == mbean.add(7, 1) |
| 281 |
mbean.Base = 8 |
| 282 |
assert '10' == mbean.addStrings('7', '1') |
| 283 |
mbean.Base = 16 |
| 284 |
sleep 2000 |
| 285 |
println "Number of invocations: $mbean.Invocations" |
| 286 |
println mbean |
| 287 |
} |
| 288 |
|
| 289 |
assert 15 == calc.add(9, 6) |
| 290 |
assert '11' == calc.addStrings('10', '1') |
| 291 |
sleep 2000 |
| 292 |
assert '20' == calc.addStrings('1f', '1') |
| 293 |
// end::spring_usage[] |
| 294 |
''' |
| 295 |
} |
| 296 |
|
| 297 |
void testTroubleshooting() { |
| 298 |
shouldCompile ''' |
| 299 |
import javax.management.remote.* |
| 300 |
|
| 301 |
def password = '123' |
| 302 |
def serverUrl = 'service:jmx://localhost:8080' |
| 303 |
|
| 304 |
// tag::troubleshooting[] |
| 305 |
def jmxEnv = null |
| 306 |
if (password != null) { |
| 307 |
jmxEnv = [(JMXConnector.CREDENTIALS): (String[])["monitor", password]] |
| 308 |
} |
| 309 |
def connector = JMXConnectorFactory.connect(new JMXServiceURL(serverUrl), jmxEnv) |
| 310 |
// end::troubleshooting[] |
| 311 |
''' |
| 312 |
} |
| 313 |
|
| 314 |
void testJmxBuilder() { |
| 315 |
shouldCompile ''' |
| 316 |
@Grab('org.codehaus.groovy:groovy-jmx:2.1.6') |
| 317 |
import groovy.jmx.builder.JmxBuilder |
| 318 |
import javax.management.ObjectName |
| 319 |
|
| 320 |
// tag::instantiating_jmxbuilder[] |
| 321 |
def jmx = new JmxBuilder() |
| 322 |
// end::instantiating_jmxbuilder[] |
| 323 |
|
| 324 |
// tag::connector_server[] |
| 325 |
jmx.connectorServer(port: 9000).start() |
| 326 |
// end::connector_server[] |
| 327 |
|
| 328 |
// tag::connector_server_and_local_registry[] |
| 329 |
import java.rmi.registry.LocateRegistry |
| 330 |
//... |
| 331 |
|
| 332 |
LocateRegistry.createRegistry(9000) |
| 333 |
jmx.connectorServer(port: 9000).start() |
| 334 |
// end::connector_server_and_local_registry[] |
| 335 |
|
| 336 |
// tag::client_connector[] |
| 337 |
def client = jmx.connectorClient(port: 9000) |
| 338 |
client.connect() |
| 339 |
// end::client_connector[] |
| 340 |
|
| 341 |
// tag::client_connector_usage[] |
| 342 |
client.getMBeanServerConnection() |
| 343 |
// end::client_connector_usage[] |
| 344 |
|
| 345 |
class Foo {} |
| 346 |
class Bar {} |
| 347 |
class SomeBar {} |
| 348 |
if (true) { |
| 349 |
// tag::jmxbuilder_export[] |
| 350 |
def beans = jmx.export { |
| 351 |
bean(new Foo()) |
| 352 |
bean(new Bar()) |
| 353 |
bean(new SomeBar()) |
| 354 |
} |
| 355 |
// end::jmxbuilder_export[] |
| 356 |
} |
| 357 |
|
| 358 |
// tag::request_controller[] |
| 359 |
class RequestController { |
| 360 |
// constructors |
| 361 |
RequestController() { super() } |
| 362 |
RequestController(Map resource) { } |
| 363 |
|
| 364 |
// attributes |
| 365 |
boolean isStarted() { true } |
| 366 |
int getRequestCount() { 0 } |
| 367 |
int getResourceCount() { 0 } |
| 368 |
void setRequestLimit(int limit) { } |
| 369 |
int getRequestLimit() { 0 } |
| 370 |
|
| 371 |
// operations |
| 372 |
void start() { } |
| 373 |
void stop() { } |
| 374 |
void putResource(String name, Object resource) { } |
| 375 |
void makeRequest(String res) { } |
| 376 |
void makeRequest() { } |
| 377 |
} |
| 378 |
// end::request_controller[] |
| 379 |
|
| 380 |
if (true) { |
| 381 |
// tag::implicit_export[] |
| 382 |
jmx.export { |
| 383 |
bean(new RequestController(resource: "Hello World")) |
| 384 |
} |
| 385 |
// end::implicit_export[] |
| 386 |
} |
| 387 |
|
| 388 |
if (true) { |
| 389 |
// tag::using_bean[] |
| 390 |
def ctrl = new RequestController(resource:"Hello World") |
| 391 |
def beans = jmx.export { |
| 392 |
bean(target: ctrl, name: "jmx.tutorial:type=Object") |
| 393 |
} |
| 394 |
// end::using_bean[] |
| 395 |
} |
| 396 |
|
| 397 |
if (true) { |
| 398 |
// tag::export_all_attributes[] |
| 399 |
def objName = new ObjectName("jmx.tutorial:type=Object") |
| 400 |
def beans = jmx.export { |
| 401 |
bean(target: new RequestController(), |
| 402 |
name: objName, |
| 403 |
attributes: "*") |
| 404 |
} |
| 405 |
// end::export_all_attributes[] |
| 406 |
} |
| 407 |
|
| 408 |
if (true) { |
| 409 |
// tag::export_attribute_list[] |
| 410 |
def objName = new ObjectName("jmx.tutorial:type=Object") |
| 411 |
def beans = jmx.export { |
| 412 |
bean( |
| 413 |
target: new RequestController(), |
| 414 |
name: objName, |
| 415 |
attributes: ["Resource", "RequestCount"] |
| 416 |
) |
| 417 |
} |
| 418 |
// end::export_attribute_list[] |
| 419 |
} |
| 420 |
|
| 421 |
if (true) { |
| 422 |
// tag::export_attribute_with_explicit_descriptors[] |
| 423 |
def objName = new ObjectName("jmx.tutorial:type=Object") |
| 424 |
def beans = jmx.export { |
| 425 |
bean( |
| 426 |
target: new RequestController(), |
| 427 |
name: objName, |
| 428 |
attributes: [ |
| 429 |
"Resource": [desc: "The resource to request.", readable: true, writable: true, defaultValue: "Hello"], |
| 430 |
"RequestCount": "*" |
| 431 |
] |
| 432 |
) |
| 433 |
} |
| 434 |
// end::export_attribute_with_explicit_descriptors[] |
| 435 |
} |
| 436 |
|
| 437 |
if (true) { |
| 438 |
// tag::export_all_constructors[] |
| 439 |
def objName = new ObjectName("jmx.tutorial:type=Object") |
| 440 |
def beans = jmx.export { |
| 441 |
bean( |
| 442 |
target: new RequestController(), |
| 443 |
name: objName, |
| 444 |
constructors: "*" |
| 445 |
) |
| 446 |
} |
| 447 |
// end::export_all_constructors[] |
| 448 |
} |
| 449 |
|
| 450 |
if (true) { |
| 451 |
// tag::export_constructors_using_parameter_descriptor[] |
| 452 |
def objName = new ObjectName("jmx.tutorial:type=Object") |
| 453 |
def beans = jmx.export { |
| 454 |
bean( |
| 455 |
target: new RequestController(), |
| 456 |
name: objName, |
| 457 |
constructors: [ |
| 458 |
"RequestController": ["Object"] |
| 459 |
] |
| 460 |
) |
| 461 |
} |
| 462 |
// end::export_constructors_using_parameter_descriptor[] |
| 463 |
} |
| 464 |
|
| 465 |
if (true) { |
| 466 |
// tag::export_constructor_with_explicit_descriptors[] |
| 467 |
def objName = new ObjectName("jmx.tutorial:type=Object") |
| 468 |
def beans = jmx.export { |
| 469 |
bean(target: new RequestController(), name: objName, |
| 470 |
constructors: [ |
| 471 |
"RequestController": [ |
| 472 |
desc: "Constructor takes param", |
| 473 |
params: ["Object" : [name: "Resource", desc: "Resource for controller"]] |
| 474 |
] |
| 475 |
] |
| 476 |
) |
| 477 |
} |
| 478 |
// end::export_constructor_with_explicit_descriptors[] |
| 479 |
} |
| 480 |
|
| 481 |
if (true) { |
| 482 |
// tag::export_all_operations[] |
| 483 |
def objName = new ObjectName("jmx.tutorial:type=Object") |
| 484 |
def beans = jmx.export { |
| 485 |
bean( |
| 486 |
target: new RequestController(), |
| 487 |
name: objName, |
| 488 |
operations: "*" |
| 489 |
) |
| 490 |
} |
| 491 |
// end::export_all_operations[] |
| 492 |
} |
| 493 |
|
| 494 |
if (true) { |
| 495 |
// tag::export_operation_list[] |
| 496 |
def objName = new ObjectName("jmx.tutorial:type=Object") |
| 497 |
def beans = jmx.export { |
| 498 |
bean( |
| 499 |
target: new RequestController(), |
| 500 |
name: objName, |
| 501 |
operations: ["start", "stop"] |
| 502 |
) |
| 503 |
} |
| 504 |
// end::export_operation_list[] |
| 505 |
} |
| 506 |
|
| 507 |
if (true) { |
| 508 |
// tag::export_operations_by_signature[] |
| 509 |
def objName = new ObjectName("jmx.tutorial:type=Object") |
| 510 |
def beans = jmx.export { |
| 511 |
bean( |
| 512 |
target: new RequestController(), |
| 513 |
name: objName, |
| 514 |
operations: [ |
| 515 |
"makeRequest": ["String"] |
| 516 |
] |
| 517 |
) |
| 518 |
} |
| 519 |
// end::export_operations_by_signature[] |
| 520 |
} |
| 521 |
|
| 522 |
if (true) { |
| 523 |
// tag::export_operations_with_explicit_descriptors[] |
| 524 |
def objName = new ObjectName("jmx.tutorial:type=Object") |
| 525 |
def beans = jmx.export { |
| 526 |
bean(target: new RequestController(), name: objName, |
| 527 |
operations: [ |
| 528 |
"start": [desc: "Starts request controller"], |
| 529 |
"stop": [desc: "Stops the request controller"], |
| 530 |
"setResource": [params: ["Object"]], |
| 531 |
"makeRequest": [ |
| 532 |
desc: "Executes the request.", |
| 533 |
params: [ |
| 534 |
"String": [name: "Resource", desc: "The resource to request"] |
| 535 |
] |
| 536 |
] |
| 537 |
] |
| 538 |
) |
| 539 |
} |
| 540 |
// end::export_operations_with_explicit_descriptors[] |
| 541 |
} |
| 542 |
|
| 543 |
// tag::embedding_descriptor[] |
| 544 |
class RequestControllerGroovy { |
| 545 |
// attributes |
| 546 |
boolean started |
| 547 |
int requestCount |
| 548 |
int resourceCount |
| 549 |
int requestLimit |
| 550 |
Map resources |
| 551 |
|
| 552 |
// operations |
| 553 |
void start() { } |
| 554 |
void stop(){ } |
| 555 |
void putResource(String name, Object resource) { } |
| 556 |
void makeRequest(String res) { } |
| 557 |
void makeRequest() { } |
| 558 |
|
| 559 |
static descriptor = [ |
| 560 |
name: "jmx.builder:type=EmbeddedObject", |
| 561 |
operations: ["start", "stop", "putResource"], |
| 562 |
attributes: "*" |
| 563 |
] |
| 564 |
} |
| 565 |
|
| 566 |
// export |
| 567 |
jmx.export( |
| 568 |
bean(new RequestControllerGroovy()) |
| 569 |
) |
| 570 |
// end::embedding_descriptor[] |
| 571 |
|
| 572 |
// tag::exporting_timer[] |
| 573 |
def timer = jmx.timer(name: "jmx.builder:type=Timer", event: "heartbeat", period: "1s") |
| 574 |
timer.start() |
| 575 |
// end::exporting_timer[] |
| 576 |
|
| 577 |
if (true) { |
| 578 |
// tag::exporting_timer_beans[] |
| 579 |
def beans = jmx.export { |
| 580 |
timer(name: "jmx.builder:type=Timer1", event: "event.signal", period: "1s") |
| 581 |
timer(name: "jmx.builder:type=Timer2", event: "event.log", period: "1s") |
| 582 |
} |
| 583 |
beans[0].start() |
| 584 |
beans[1].start() |
| 585 |
// end::exporting_timer_beans[] |
| 586 |
} |
| 587 |
|
| 588 |
def callback |
| 589 |
// tag::event_handling_closures[] |
| 590 |
callback = { -> |
| 591 |
// event handling code here. |
| 592 |
} |
| 593 |
// end::event_handling_closures[] |
| 594 |
|
| 595 |
// tag::event_handling_closures_event[] |
| 596 |
callback = { event -> |
| 597 |
// event handling code |
| 598 |
} |
| 599 |
// end::event_handling_closures_event[] |
| 600 |
|
| 601 |
// tag::handling_attribute_onchange_event[] |
| 602 |
jmx.export { |
| 603 |
bean( |
| 604 |
target: new RequestController(), name: "jmx.tutorial:type=Object", |
| 605 |
attributes: [ |
| 606 |
"Resource": [ |
| 607 |
readable: true, writable: true, |
| 608 |
onChange: { e -> |
| 609 |
println e.oldValue |
| 610 |
println e.newValue |
| 611 |
} |
| 612 |
] |
| 613 |
] |
| 614 |
) |
| 615 |
} |
| 616 |
// end::handling_attribute_onchange_event[] |
| 617 |
|
| 618 |
// tag::event_handler[] |
| 619 |
class EventHandler { |
| 620 |
void handleStart(e){ |
| 621 |
println e |
| 622 |
} |
| 623 |
} |
| 624 |
// end::event_handler[] |
| 625 |
|
| 626 |
if (true) { |
| 627 |
// tag::event_handler_usage[] |
| 628 |
def handler = new EventHandler() |
| 629 |
|
| 630 |
def beans = jmx.export { |
| 631 |
bean(target: new RequestController(), name: "jmx.tutorial:type=Object", |
| 632 |
operations: [ |
| 633 |
"start": [ |
| 634 |
desc:"Starts request controller", |
| 635 |
onCall:handler.&handleStart |
| 636 |
] |
| 637 |
] |
| 638 |
) |
| 639 |
} |
| 640 |
// end::event_handler_usage[] |
| 641 |
} |
| 642 |
|
| 643 |
if (true) { |
| 644 |
// tag::listener_mbean[] |
| 645 |
def beans = jmx.export { |
| 646 |
timer(name: "jmx.builder:type=Timer", event: "heartbeat", period: "1s").start() |
| 647 |
bean(target: new RequestController(), name: "jmx.tutorial:type=Object", |
| 648 |
operations: "*", |
| 649 |
listeners: [ |
| 650 |
heartbeat: [ |
| 651 |
from: "jmx.builder:type=Timer", |
| 652 |
call: { e -> |
| 653 |
println e |
| 654 |
} |
| 655 |
] |
| 656 |
] |
| 657 |
) |
| 658 |
} |
| 659 |
// end::listener_mbean[] |
| 660 |
} |
| 661 |
|
| 662 |
// tag::jmxbuilders_listener[] |
| 663 |
jmx.timer(name: "jmx.builder:type=Timer", period: "1s").start() |
| 664 |
|
| 665 |
jmx.listener( |
| 666 |
from: "jmx.builder:type=Timer", |
| 667 |
call: { e -> |
| 668 |
println "beep..." |
| 669 |
} |
| 670 |
) |
| 671 |
// end::jmxbuilders_listener[] |
| 672 |
|
| 673 |
// tag::declare_emitter[] |
| 674 |
def emitter = jmx.emitter() |
| 675 |
// end::declare_emitter[] |
| 676 |
|
| 677 |
// tag::emitter_broadcast_event[] |
| 678 |
emitter.send() |
| 679 |
// end::emitter_broadcast_event[] |
| 680 |
|
| 681 |
// tag::emitter_broadcast_event_with_objects[] |
| 682 |
emitter.send("Hello!") |
| 683 |
// end::emitter_broadcast_event_with_objects[] |
| 684 |
''' |
| 685 |
} |
| 686 |
} |