3

I have configured the James server and added some user and domains to it.

From the Jconsole i am able to get the list of users as shown in below image.

Can anyone please provide me the code snippet to get same through the JMX

as James documentation specify this To add user Programatically by JMX

Somehow i manage to get the code snippet work but unable to find how to call the operations of Mbean without any parameter.

This code is printing attributes of Mbean

    String url = "service:jmx:rmi://localhost/jndi/rmi://localhost:9999/jmxrmi";
    JMXServiceURL serviceUrl = new JMXServiceURL(url);
    JMXConnector jmxConnector = JMXConnectorFactory.connect(serviceUrl, null);
    try {
        MBeanServerConnection mbeanConn = jmxConnector.getMBeanServerConnection();
        ObjectName mbeanName = new ObjectName("org.apache.james:type=component,name=usersrepository");
        MBeanInfo info = mbeanConn.getMBeanInfo(mbeanName);
        MBeanAttributeInfo[] attributes = info.getAttributes();
        for (MBeanAttributeInfo attr : attributes)
        {
            System.out.println(attr.getDescription() + " " + mbeanConn.getAttribute(mbeanName,attr.getName()));
        }
    } finally {
        jmxConnector.close();

    }

Please help in getting this code work to get the Users List.

This is the Jconsole screen for getting the Users list from James Server

Shashank.gupta40
  • 915
  • 1
  • 8
  • 26

1 Answers1

0

When invoking operations on a bean via JMX, the calls are proxied through the MBeanServer. You request that the MBeanServer invokes some method on a managed bean with the ObjectName. In your code, you access the MBeanServer through a MBeanServerConnection.

To invoke a blank method you would:

MBeanServerConnection mbeanConn = jmxConnector.getMBeanServerConnection();
ObjectName mbeanName = new ObjectName("org.apache.james:type=component,name=usersrepository");

// since you have no parameters, the types and values are null
mbeanConn.invoke(mbeanName, "MethodToInvoke", null, null)

Using the MBeanServer to invoke methods can be cumbersome, so it might be easier to use a JMX Proxy Object. This just has the local connection construct a java.lang.reflect.Proxy object that uses the MBeanServerConnection.invoke method in it's InvocationHandler. Then you can use the Proxy object like a normal instance of your class. For this approach, your target MBean has to implement an interface that you can use to generate the local proxy.

import javax.management.JMX;
import org.apache.james.user.api.UsersRepository;
...

UsersRepository proxy = JMX.newMBeanProxy(mbeanConn, mbeanName, UsersRepository.class);
Iterator<String> userList = proxy.list();

Either one of these ways should allow you to invoke methods without or without parameters on the user repository bean.