2014-09-03 25 views
3

我配置了James服務器併爲其添加了一些用戶和域。如何獲取通過JMX在James Server中註冊的用戶列表

從Jconsole中我可以得到如下圖所示的用戶列表。

誰能請我提供的代碼片段通過JMX

詹姆斯文檔指定此To add user Programatically by JMX

不知何故,我設法得到的代碼片段工作,但無法找到如何調用獲得相同沒有任何參數的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(); 

    } 

此代碼打印屬性請在獲得此代碼的工作,以獲得用戶列表幫助。

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

回答

0

當通過JMX bean的調用操作時,調用都是通過MBeanServer的代理。您請求MBeanServer使用ObjectName調用託管bean上的某個方法。在您的代碼中,您可以通過MBeanServerConnection訪問MBeanServer。

要調用一個空白的方法,你會:

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) 

使用MBeanServer調用方法可能會很麻煩,所以它可能是更容易使用JMX代理對象。這只是本地連接構造了一個java.lang.reflect.Proxy對象,該對象在其InvocationHandler中使用MBeanServerConnection.invoke方法。然後,您可以像使用類的普通實例一樣使用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(); 

這兩種方式中的任何一種都應該允許您調用用戶存儲庫bean上沒有或沒有參數的方法。

相關問題