You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java中读取WebLogic JMS Queue消息数及获取监控所需Jar包

读取WebLogic JMS Queue消息数及所需Jar包

获取JMS Queue消息数的实现方式

方式1:远程JMX连接获取

WebLogic通过JMX暴露了JMS相关的监控MBean,你可以通过远程JMX连接读取Queue的消息数(如当前消息数、累计接收消息数等)。核心代码示例如下:

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.util.HashMap;
import java.util.Map;

public class WebLogicJMSMonitor {
    public static void main(String[] args) throws Exception {
        // WebLogic JMX连接参数,替换为实际配置
        String jmxUrl = "service:jmx:t3://localhost:7001/jndi/weblogic.management.mbeanservers.runtime";
        String username = "weblogic";
        String password = "your_password";

        Map<String, Object> env = new HashMap<>();
        env.put(JMXConnector.CREDENTIALS, new String[]{username, password});
        JMXServiceURL serviceURL = new JMXServiceURL(jmxUrl);

        // 建立JMX连接并获取指标
        try (JMXConnector connector = JMXConnectorFactory.connect(serviceURL, env)) {
            MBeanServerConnection connection = connector.getMBeanServerConnection();
            // 构造Queue的MBean名称,替换为实际的服务器、模块、队列名称
            ObjectName queueMBean = new ObjectName("com.bea:ServerRuntime=AdminServer,Name=YourQueueName,Type=JMSRuntime,JMSModule=YourJMSModule,DestinationType=Queue");

            Long currentMsgCount = (Long) connection.getAttribute(queueMBean, "MessagesCurrentCount");
            Long totalReceivedCount = (Long) connection.getAttribute(queueMBean, "MessagesReceivedCount");

            System.out.println("当前队列消息数: " + currentMsgCount);
            System.out.println("累计接收消息数: " + totalReceivedCount);
        }
    }
}

方式2:WebLogic内部应用直接获取

如果监控代码部署在WebLogic服务器上的应用中,可以直接获取本地MBeanServer,无需远程连接:

import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;

public class LocalJMSMonitor {
    public static void main(String[] args) throws Exception {
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        // 替换为实际的Queue MBean名称
        ObjectName queueMBean = new ObjectName("com.bea:ServerRuntime=AdminServer,Name=YourQueueName,Type=JMSRuntime,JMSModule=YourJMSModule,DestinationType=Queue");
        
        Long currentMsgCount = (Long) mBeanServer.getAttribute(queueMBean, "MessagesCurrentCount");
        System.out.println("当前队列消息数: " + currentMsgCount);
    }
}

所需WebLogic Jar包

实现上述功能需要以下Jar包,均位于WebLogic安装目录的server/lib下:

  • wlclient.jar:基础WebLogic客户端Jar,包含JMX连接核心类
  • wljmxclient.jar:WebLogic JMX客户端专用Jar,处理JMX连接的细节逻辑
  • wlfullclient.jar(可选):全功能客户端Jar,包含所有WebLogic客户端API,若需扩展功能可使用此包

注意:wlfullclient.jar默认未生成,需在server/lib目录下执行命令生成:java -jar wljarbuilder.jar

内容的提问来源于stack exchange,提问作者1097733

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 23:25:24