简体   繁体   English

如何通过JMX获取在James Server中注册的用户列表

[英]How to get the List of Users registered in James Server through JMX

I have configured the James server and added some user and domains to it. 我已经配置了James服务器并为其添加了一些用户和域。

From the Jconsole i am able to get the list of users as shown in below image. 从Jconsole我可以获得用户列表,如下图所示。

Can anyone please provide me the code snippet to get same through the JMX 任何人都可以请我提供代码片段以通过JMX获得相同的代码片段

as James documentation specify this To add user Programatically by JMX 正如James文档中指定的那样, 以编程方式添加用户JMX

Somehow i manage to get the code snippet work but unable to find how to call the operations of Mbean without any parameter. 不知怎的,我设法让代码片段工作但无法找到如何在没有任何参数的情况下调用Mbean的操作。

This code is printing attributes of Mbean 此代码是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. 请帮助获取此代码以获取用户列表。

这是用于从James Server获取用户列表的Jconsole屏幕

When invoking operations on a bean via JMX, the calls are proxied through the MBeanServer. 通过JMX调用bean上的操作时,调用通过MBeanServer进行代理。 You request that the MBeanServer invokes some method on a managed bean with the ObjectName. 您请求MBeanServer使用ObjectName在托管bean上调用某个方法。 In your code, you access the MBeanServer through a MBeanServerConnection. 在您的代码中,您通过MBeanServerConnection访问MBeanServer。

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. 使用MBeanServer调用方法可能很麻烦,因此使用JMX代理对象可能更容易。 This just has the local connection construct a java.lang.reflect.Proxy object that uses the MBeanServerConnection.invoke method in it's InvocationHandler. 这只是本地连接构造一个java.lang.reflect.Proxy对象,该对象在其InvocationHandler中使用MBeanServerConnection.invoke方法。 Then you can use the Proxy object like a normal instance of your class. 然后,您可以像使用类的普通实例一样使用Proxy对象。 For this approach, your target MBean has to implement an interface that you can use to generate the local proxy. 对于此方法,目标MBean必须实现可用于生成本地代理的接口。

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. 这些方法中的任何一种都应该允许您在用户存储库bean上调用不带或不带参数的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM