public class JMXTest extends Object
JMXTest.IFoo
interface and expose a JMXTest.Foo
implementation of that
interface as a StandardMBean
. You can run main(String[])
and start jconsole
and inspect/set the "foo" attribute and see
the change on the read-only "bar" attribute. No fuss, no muss.
There are some caveats. In general, you can declare interfaces having getters and directly expose the services implementing those interfaces as MBeans. If the service supports a dynamic change to the value of an attribute, then you can also put the settor on the interface to be exposed as an MBean.
The get/set methods need to be synchronized
so that changes
made by a setter will become immediately visible to other code also
synchronized on the same reference. If the service internally uses the
getter, then consider having district objects on which you synchronize for
get() methods which are heavily used in order to minimize contention for the
object's monitor. The other option is to make the field for the attribute
volatile
.
Since MBean methods do not declare RMI exceptions there can be a conflict in the interface if the method is also exposed for RMI, e.g., for a jini-based service. You need to have two interfaces for this case.
There may be some cases where an attribute needs to be broken into a current value and a target value, much like adjusting clocks to network time.
Modifier and Type | Class and Description |
---|---|
static class |
JMXTest.Clock |
static class |
JMXTest.Foo
Object implementing that interface.
|
static interface |
JMXTest.IClock |
static interface |
JMXTest.IFoo
Interface to be exposed as an MBean.
|
Constructor and Description |
---|
JMXTest() |
Modifier and Type | Method and Description |
---|---|
static void |
main(String[] args)
When you run this class it registers an instance of
JMXTest.Foo as an
MBean and then waits forever. |
public static void main(String[] args) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, MalformedObjectNameException, NullPointerException, InterruptedException
JMXTest.Foo
as an
MBean and then waits forever. You can run jconsole
and
verify that you can see the MBean and its attributes and change attribute
values using the setter(s) declared on the JMXTest.IFoo
interface.args
- Not used.InstanceAlreadyExistsException
MBeanRegistrationException
NotCompliantMBeanException
MalformedObjectNameException
NullPointerException
InterruptedException
Copyright © 2006–2019 SYSTAP, LLC DBA Blazegraph. All rights reserved.