简体   繁体   English

使用Java和JMX / MBean访问Weblogic JMS

[英]Accessing Weblogic JMS using Java and JMX/MBeans

I am trying to write a Java program that can browse all of the JMS queues in a Weblogic JMS server and read the messages in a given queue (but not consume them). 我正在尝试编写一个Java程序,它可以浏览Weblogic JMS服务器中的所有JMS队列,并读取给定队列中的消息(但不消耗它们)。 I'm trying to use Weblogic Mbeans and JMX but am new to both. 我正在尝试使用Weblogic Mbeans和JMX,但对两者都是新手。 I have the following code to get all of the queues and their depth: 我有以下代码来获取所有队列及其深度:

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();
}

I also have the ability to delete everything from a queue: 我还能够从队列中删除所有内容:

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

Where I'm stuck is how to read the actual messages in a destination/queue. 我遇到的问题是如何读取目标/队列中的实际消息。 I've been playing around with mbeanServerConnection.invoke and I see getMessage and getMessages but I am not sure how to use them properly. 我一直在玩mbeanServerConnection.invoke,我看到了getMessage和getMessages,但我不确定如何正确使用它们。 Can someone please show an example of how to use those to browse messages in a destination (but not consume them)? 有人可以展示一个如何使用它们来浏览目的地中的消息(但不消耗它们)的例子吗? I've tried a few variants like this but I can't get it to work: 我尝试了一些像这样的变种,但我无法让它工作:

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

After some study, I was able to write this code that does what I want: 经过一番研究,我能够编写出符合我想要的代码:

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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM