使用 Java 和 JMX/MBean 访问 Weblogic JMS

Posted

技术标签:

【中文标题】使用 Java 和 JMX/MBean 访问 Weblogic JMS【英文标题】:Accessing Weblogic JMS using Java and JMX/MBeans 【发布时间】:2014-04-06 01:55:19 【问题描述】:

我正在尝试编写一个 Java 程序,该程序可以浏览 Weblogic JMS 服务器中的所有 JMS 队列并读取给定队列中的消息(但不使用它们)。我正在尝试使用 Weblogic Mbeans 和 JMX,但对两者都很陌生。我有以下代码来获取所有队列及其深度:

private void countMessages1() throws Exception 
    JMXConnector connector = getMBeanServerConnector("/jndi/"+RuntimeServiceMBean.MBEANSERVER_JNDI_NAME);
    MBeanServerConnection mbeanServerConnection = connector.getMBeanServerConnection();
    ObjectName service = new ObjectName("com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean");
    ObjectName serverRuntime = (ObjectName) mbeanServerConnection.getAttribute(service, "ServerRuntime");
    ObjectName jmsRuntime = (ObjectName) mbeanServerConnection.getAttribute(serverRuntime, "JMSRuntime");
    ObjectName[] jmsServers = (ObjectName[]) mbeanServerConnection.getAttribute(jmsRuntime, "JMSServers");
    for (ObjectName jmsServer: jmsServers) 
        if ("JMSServer".equals(jmsServer.getKeyProperty("Name"))) 
            ObjectName[] destinations = (ObjectName[]) mbeanServerConnection.getAttribute(jmsServer, "Destinations");
            for (ObjectName destination: destinations) 
                String queueName = destination.getKeyProperty("Name");
                Long queueDepth = (Long) mbeanServerConnection.getAttribute(destination, "MessagesCurrentCount");
                System.out.println("Queue: " + queueName + " Depth: " + queueDepth);
            
            break;
        
    
    connector.close();

我还可以从队列中删除所有内容:

mbeanServerConnection.invoke(destination, "deleteMessages", new Object[] "", new String[] "java.lang.String");

我遇到的困难是如何读取目标/队列中的实际消息。我一直在玩 mbeanServerConnection.invoke,我看到了 getMessage 和 getMessages,但我不确定如何正确使用它们。有人可以举例说明如何使用它们来浏览目的地中的消息(但不使用它们)吗?我已经尝试了一些这样的变体,但我无法让它工作:

String message = (String) mbeanServerConnection.invoke(destination, "getMessage", new Object[]  "", 0, JMS_ALL_STATES, new String[] "java.lang.String");

【问题讨论】:

【参考方案1】:

经过一番研究,我能够编写出我想要的代码:

for (ObjectName destination: destinations) 
    if (destination.getKeyProperty("Name").equalsIgnoreCase(selectedQueue)) 
        try 
            String cursor = (String)mbeanServerConnection.invoke(destination, "getMessages", new Object[] "", 0, new String[] "java.lang.String", "java.lang.Integer");
            Long cursorSize = (Long)mbeanServerConnection.invoke(destination, "getCursorSize", new Object[] cursor, new String[] "java.lang.String");
            //System.out.println(cursor + ": " + cursorSize);
            CompositeData[] messages = (CompositeData[])mbeanServerConnection.invoke(destination, "getNext", new Object[] cursor, cursorSize.intValue(), new String[] "java.lang.String", "java.lang.Integer");
            if (null != messages) 
                for (CompositeData message: messages) 
                    JMSMessageInfo messageInfo = new JMSMessageInfo(message);
                    Long messageInfoHandle = messageInfo.getHandle();
                    CompositeData messageCursor = (CompositeData)mbeanServerConnection.invoke(destination, "getMessage", new Object[] cursor, messageInfoHandle, new String[] "java.lang.String", "java.lang.Long");
                    JMSMessageInfo mbi = new JMSMessageInfo(messageCursor);
                    WLMessage messageBody = mbi.getMessage();
                    body = messageBody.toString().substring(messageBody.toString().indexOf(", ") + 2, messageBody.toString().length()-1);
                    Object[] row = counter, body;
                    publish(row);
                    if (isCancelled()) 
                        modelMessages.setRowCount(0);
                        return model;
                    
                
            
         catch (Exception e) 
            e.printStackTrace();
            modelMessages.setRowCount(0);
            return model;
        
    

【讨论】:

以上是关于使用 Java 和 JMX/MBean 访问 Weblogic JMS的主要内容,如果未能解决你的问题,请参考以下文章

从 shell 脚本调用 JMX MBean 方法

部署后JMX MBean不可见

JMX MBean class xxx does not implement DynamicMBean, and neither follows the Standard MBean conventi

JMX MBean class xxx does not implement DynamicMBean, and neither follows the Standard MBean conventi

不能使用jconsole连接到JBoss eap7.1

java 替换文本中的所有指定的字符串,比如#*替换为sq, #**替换为we, 我使用了replaceAll进行的替换