Created
September 7, 2016 01:08
-
-
Save gregw/431324f9c853b80358eccf18ac98b3f2 to your computer and use it in GitHub Desktop.
JMX application example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.eclipse.jetty.jmx; | |
import java.lang.management.ManagementFactory; | |
import java.util.concurrent.CountDownLatch; | |
import org.eclipse.jetty.util.annotation.ManagedAttribute; | |
import org.eclipse.jetty.util.annotation.ManagedObject; | |
import org.eclipse.jetty.util.annotation.ManagedOperation; | |
import org.eclipse.jetty.util.component.ContainerLifeCycle; | |
import org.eclipse.jetty.util.component.LifeCycle; | |
public class JmxAppExample | |
{ | |
@ManagedObject("this is some doco") | |
public static class MyMainObject extends ContainerLifeCycle | |
{ | |
private int _value; | |
private NestedObject _nested; | |
@ManagedAttribute("Some value that can be set and got from") | |
public int getValue() | |
{ | |
return _value; | |
} | |
public void setValue(int value) | |
{ | |
_value = value; | |
} | |
@ManagedOperation | |
public void doSomething() | |
{ | |
System.err.println("something!"); | |
if (_nested!=null) | |
_nested.doSomething(); | |
} | |
@ManagedAttribute() | |
public NestedObject getNested() | |
{ | |
return _nested; | |
} | |
public void setNested(NestedObject nested) | |
{ | |
updateBean(_nested,nested); | |
_nested = nested; | |
} | |
} | |
@ManagedObject | |
public static class NestedObject | |
{ | |
private long _readOnly; | |
@ManagedAttribute("Some value that is read only") | |
public long getReadOnly() | |
{ | |
return _readOnly; | |
} | |
@ManagedOperation | |
public void doSomething() | |
{ | |
_readOnly=System.currentTimeMillis(); | |
} | |
} | |
public static void main(String... args) throws Exception | |
{ | |
MBeanContainer mbeanContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer()); | |
MyMainObject main = new MyMainObject(); | |
main.addBean(mbeanContainer); | |
main.start(); | |
main.setNested(new NestedObject()); | |
// TODO add default methods to Listener | |
final CountDownLatch stopped = new CountDownLatch(1); | |
main.addLifeCycleListener(new LifeCycle.Listener() | |
{ | |
@Override | |
public void lifeCycleStopping(LifeCycle event) | |
{ | |
} | |
@Override | |
public void lifeCycleStopped(LifeCycle event) | |
{ | |
stopped.countDown(); | |
} | |
@Override | |
public void lifeCycleStarting(LifeCycle event) | |
{ | |
} | |
@Override | |
public void lifeCycleStarted(LifeCycle event) | |
{ | |
} | |
@Override | |
public void lifeCycleFailure(LifeCycle event, Throwable cause) | |
{ | |
stopped.countDown(); | |
} | |
}); | |
stopped.await(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment