JMX Operations

514 views
Skip to first unread message

rakstar

unread,
Sep 1, 2007, 12:36:49 AM9/1/07
to google-guice
Is there a way to make Guice expose MBean operations? For example, I
have the following class:

public class HelloMBean {
public void sayHello() {
System.out.println("hello world");
}
}

in my module, i have:

bind(HelloMBean.class).asEagerSingleton();

and after creating my injector, i have:

Manager.manage("hello", injector);

This exposes my MBean (along with with all my other Guice bindings) in
JConsole but doesn't give me access to the "sayHello" operation. Am I
missing something or am I misunderstanding the purpose of Guice's JMX
support?

Bob Lee

unread,
Sep 1, 2007, 12:43:32 AM9/1/07
to google...@googlegroups.com
Guice's JMX support only exposes binding information. You'll have to manually register your MBean with the server.

It's easy though. Create a binding for MBeanServer and inject it into your object:

public class HelloMBean {
  @Inject
  HelloMBean(MBeanServer server) {
    server.registerMBean(this, ...);

  }

  public void sayHello() {
    System.out.println("hello world");
  }
}

Bob

rakstar

unread,
Sep 1, 2007, 1:04:33 PM9/1/07
to google-guice
Wow, that worked perfectly!
Didn't think it would be that easy... thanks!

On Sep 1, 12:43 am, "Bob Lee" <crazy...@crazybob.org> wrote:
> Guice's JMX support only exposes binding information. You'll have to
> manually register your MBean with the server.
>
> It's easy though. Create a binding for MBeanServer and inject it into your
> object:
>
> public class HelloMBean {
> @Inject
> HelloMBean(MBeanServer server) {
> server.registerMBean(this, ...);
> }
>
> public void sayHello() {
> System.out.println("hello world");
> }
>
> }
>
> Bob
>

Reply all
Reply to author
Forward
0 new messages