運行環境:
weblogic 92, jdk150
通過weblogic的JMX服務,我們可以直接取得MBean來監控JMS伺服器,而不必每次都開啟weblogic的online console進行人工監控,完全可以實現自動化.
原始碼如下:
import java.io.IOException;<br />import java.net.MalformedURLException;<br />import java.util.Hashtable;<br />import javax.management.MBeanServerConnection;<br />import javax.management.MalformedObjectNameException;<br />import javax.management.ObjectName;<br />import javax.management.remote.JMXConnector;<br />import javax.management.remote.JMXConnectorFactory;<br />import javax.management.remote.JMXServiceURL;<br />import javax.naming.Context;</p><p>public class PrintJMSQueueInfo {<br /> private static MBeanServerConnection connection;<br /> private static JMXConnector connector;<br /> private static final ObjectName jmsService; </p><p> // Initializing the object name for DomainRuntimeServiceMBean<br /> // so it can be used throughout the class.<br /> static {<br /> try {<br /> jmsService = new ObjectName(<br /> "com.bea:Name=RuntimeService," +<br /> "Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean"<br /> );<br /> }catch (MalformedObjectNameException e) {<br /> throw new AssertionError(e.getMessage());<br /> }<br /> }<br /> /* * Initialize connection to the Domain Runtime MBean Server */<br /> public static void initConnection(String hostname, String portString,<br /> String username, String password) throws IOException,<br /> MalformedURLException {<br /> String protocol = "t3";<br /> Integer portInteger = Integer.valueOf(portString);<br /> int port = portInteger.intValue();<br /> String jndiroot = "/jndi/";<br /> String mserver = "weblogic.management.mbeanservers.runtime";<br /> JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, jndiroot + mserver);<br /> Hashtable h = new Hashtable();<br /> h.put(Context.SECURITY_PRINCIPAL, username);<br /> h.put(Context.SECURITY_CREDENTIALS, password);<br /> h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,"weblogic.management.remote");<br /> connector = JMXConnectorFactory.connect(serviceURL, h);<br /> connection = connector.getMBeanServerConnection();<br /> }</p><p> public static ObjectName[] getJMSServers() throws Exception{<br /> ObjectName serverRuntime = (ObjectName)connection.getAttribute(jmsService,"ServerRuntime");<br /> ObjectName jmsRuntime = (ObjectName)connection.getAttribute(serverRuntime,"JMSRuntime");<br /> ObjectName[] jmsServers = (ObjectName[])connection.getAttribute(jmsRuntime,"JMSServers");<br /> return jmsServers;<br /> }</p><p> public static void main(String[] args) throws Exception {<br /> String hostname = "localhost";<br /> String portString = "7001";<br /> String username = "weblogic";<br /> String password = "weblogic";<br /> initConnection(hostname, portString, username, password);<br /> ObjectName[] serverRT = getJMSServers();<br /> int length = (int) serverRT.length;<br /> for (int i = 0; i < length; i++) {<br /> ObjectName[] queues = (ObjectName[])connection.getAttribute(serverRT[i],"Destinations");<br /> int queueCount = (int)queues.length;<br /> for(int k=0;k<queueCount;k++){<br /> String queueName = (String) connection.getAttribute(queues[k],<br /> "Name");<br /> Long messagesCurrentCount = (Long) connection.getAttribute(queues[k],<br /> "MessagesCurrentCount");<br /> System.out.println("Queue name: " + queueName + ", current message is " + messagesCurrentCount);<br /> }<br /> }<br /> connector.close();<br /> }<br />}<br />
簡單介紹一下這段代碼,
抓取JMX的MBean首先要初始化MBean對象,jmsService = new ObjectName(<br /> "com.bea:Name=RuntimeService," +<br /> "Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean"<br /> );
關於Name 和 Type 可以到weblogic 的文檔裡面去查,一般weblogic提供幾個入口,通過MBean之間的關係可以訪問到你需要的MBean. 比如這裡我們要的是JMS的queue資訊,但是初始化的時候先得到一個RuntimeServiceMBean。
然後是建立串連,當然使用者名稱密碼和URL是肯定需要的。public static void initConnection(String hostname, String portString,<br /> String username, String password) throws IOException,<br /> MalformedURLException {<br /> String protocol = "t3";<br /> Integer portInteger = Integer.valueOf(portString);<br /> int port = portInteger.intValue();<br /> String jndiroot = "/jndi/";<br /> String mserver = "weblogic.management.mbeanservers.runtime";<br /> JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, jndiroot + mserver);<br /> Hashtable h = new Hashtable();<br /> h.put(Context.SECURITY_PRINCIPAL, username);<br /> h.put(Context.SECURITY_CREDENTIALS, password);<br /> h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,"weblogic.management.remote");<br /> connector = JMXConnectorFactory.connect(serviceURL, h);<br /> connection = connector.getMBeanServerConnection();<br /> }
建立好串連之後就可以擷取MBean了,
public static ObjectName[] getJMSServers() throws Exception{<br /> ObjectName serverRuntime = (ObjectName)connection.getAttribute(jmsService,"ServerRuntime");<br /> ObjectName jmsRuntime = (ObjectName)connection.getAttribute(serverRuntime,"JMSRuntime");<br /> ObjectName[] jmsServers = (ObjectName[])connection.getAttribute(jmsRuntime,"JMSServers");<br /> return jmsServers;<br /> }
先找到RuntimeServiceMBean,通過它找到ServerRuntime, 再是JMSRuntime, 最後是JMSServers。 至於他們之間的關係,weblogic協助文檔中都有。
獲得了JMSServers之後再找Queue就不難了,逐個遍曆
ObjectName[] serverRT = getJMSServers();<br /> int length = (int) serverRT.length;<br /> for (int i = 0; i < length; i++) {<br /> ObjectName[] queues = (ObjectName[])connection.getAttribute(serverRT[i],"Destinations");<br /> int queueCount = (int)queues.length;<br /> for(int k=0;k<queueCount;k++){<br /> String queueName = (String) connection.getAttribute(queues[k],<br /> "Name");<br /> Long messagesCurrentCount = (Long) connection.getAttribute(queues[k],<br /> "MessagesCurrentCount");<br /> System.out.println("Queue name: " + queueName + ", current message is " + messagesCurrentCount);<br /> }
最後,別忘記關閉串連。
connector.close();