Skip to content

Instantly share code, notes, and snippets.

@gregw
Created September 7, 2016 01:13
Show Gist options
  • Save gregw/65c8d85ff979662a2b3da2c943a6b2ac to your computer and use it in GitHub Desktop.
Save gregw/65c8d85ff979662a2b3da2c943a6b2ac to your computer and use it in GitHub Desktop.
Alternative
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;
@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!");
}
@ManagedAttribute()
public NestedObject getNested()
{
return getBean(NestedObject.class);
}
public void setNested(NestedObject nested)
{
addBean(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