3 Replies Latest reply on Mar 27, 2007 2:37 PM by brian.stansberry

    How to access TreeCache from MBean ?

    raameshwar

      Hi ,

      I am new to both JBoss Cache and MBeans.

      I am trying to deploy JBoss TreeCache as MBean in Sun Java Application Server 9. I am able to deploy it and start successfully and i am able to access the cache through the following code.

      ObjectInstance object = server.getObjectInstance(name);
      TreeCache cache = (TreeCache)server.instantiate(object.getClassName(),null);
      cache.createService();
      cache.put("/a/b/c", map);


      The problem i face is, i cannot access the same instance of cache if i access the MBean from other applications. What is the api i should use if i want to access the JBoss Cache from different applications. The server.instantiate() method always gives a new instance.

      Any pointers would be of great help.

      Thanks,
      Raameshwar.

        • 1. Re: How to access TreeCache from MBean ?
          brian.stansberry

          You're in luck in that a JBoss utility class for simplifying dealing with JMX happens to be in a jar that JBC depends on. So, assuming your cache was somehow created and registered in MBeanServer 'server' under ObjectName 'name':

          TreeCacheMBean cache = (TreeCacheMBean) org.jboss.mx.util.MBeanProxyExt.create(TreeCacheMBean.class, name.getCanonicalName(), server);
          
          // Only do this if whatever bound your cache in JMX didn't start it!!!
          cache.startService();
          
          cache.put("/a/b/c", map);
          


          What you're doing here is creating a proxy that delegates calls to the JMX server, which routes them to your mbean.

          • 2. Re: How to access TreeCache from MBean ?
            genman

            I don't know why you guys suggest that class, and not the standard one:

            http://java.sun.com/j2se/1.5.0/docs/api/javax/management/MBeanServerInvocationHandler.html

            
            
            If you have an MBean server mbs containing an MBean with ObjectName name, and if the MBean's management interface is described by the Java interface Intf, you can construct a proxy for the MBean like this:
            
             Intf proxy = (Intf)
             MBeanServerInvocationHandler.newProxyInstance(mbs,
             name,
             Intf.class,
             false);
            
            
            Suppose, for example, Intf looks like this:
            
             public interface Intf {
             public String getSomeAttribute();
             public void setSomeAttribute(String value);
             public void someOperation(String param1, int param2);
             }
            


            • 3. Re: How to access TreeCache from MBean ?
              brian.stansberry

              Because I looked for a standard one in the JDK 5 Javadoc for 10 mins and didn't find one, and that was my time budget for responding to the post. :-)

              Thanks for pointing it out.