How to get a list of users registered on the James server via JMX

I set up James' server and added some users and domains to it.

From Jconsole, I can get the list of users as shown in the picture below.

Can someone please provide me with a code snippet to get the same via JMX

like James documentation, point this To add user programmatically with JMX

Somehow I manage to get a piece of code but can't find how to invoke Mbean operations without any parameters.

This code prints Mbean attributes

    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 get this code for a list of users.

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

+3


source to share


1 answer


When calling bean operations through JMX, the calls are proxied through the MBeanServer. You are asking the MBeanServer to call some method on the managed bean with ObjectName. In your code, you are accessing the MBeanServer through the MBeanServerConnection.

To call an empty method, you must:

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 MBeanServer to call methods can be cumbersome, so it would be easier to use a JMX proxy object. It just has a local connection construct java.lang.reflect.Proxy object that uses the MBeanServerConnection.invoke method in its InvocationHandler. Then you can use the Proxy object like a regular instance of your class. For this approach, your target MBean must implement an interface that you can use to create a 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 way should allow you to call methods with or without parameters in the user's bean repository.

0


source







All Articles