我正在尝试编写一个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"});
答案 0 :(得分:5)
经过一番研究,我能够编写出符合我要求的代码:
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;
}
}
}