Running environment:
Weblogic 92, jdk150
Through the JMX service of weblogic, we can directly obtain mbeans to monitor the JMS server, instead of opening the weblogic online console for manual monitoring every time, which can be completely automated.
The source code is as follows:
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 conne; <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/>/** 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/> connector. close (); <br/>}< br/>
Briefly introduce this code,
To capture mbeans of JMX, initialize the MBean object first,JmsService = new ObjectName (<br/> "com. bea: Name = RuntimeService, "+ <br/>" Type = weblogic. management. mbeanservers. runtime. runtimeServiceMBean "<br/> );
You can check the Name and Type in the weblogic document. Generally, weblogic provides several portals to access the desired MBean through the relationship between mbeans. for example, we want the queue information of JMS, But we first get a RuntimeServiceMBean during initialization.
Then establish a connection. Of course, the user name, password, and URL are required.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/>}
After the connection is established, MBean can be obtained,
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/>}
Find RuntimeServiceMBean, find ServerRuntime, JMSRuntime, and JMSServers. As for the relationship between them, weblogic help documentation has.
After obtaining JMSServers, it is not difficult to find Queue again.
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/>}
Finally, do not forget to close the connection.
Connector. close ();