Tuesday, February 10, 2015

Oracle SOA : Get Number of messages in JMS using JMX api's

Below is the java code to get Number of messages in JMS using JMX api's


package com.raylabs.jmx;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
public class Class1 {
public Class1() {
super();
}
public static void main(String[] args) {
String environment = "dev";
String userName = "xxx";
String password = "xxx";
List jmsDestinations=new ArrayList();
jmsDestinations.add("topic1");
jmsDestinations.add("queu1");
jmsDestinations.add("topic2");
JMXConnector connector = null;
String node1BPMHostname =
environment + "-node1BPM.com";
String node1BPMPort = "7002";
String node2BPMHostname =
environment + "-node2BPM.com";
String node2BPMPort = "7002";
String node1SOAHostname =
environment + "-node1SOA";
String node1SOAPort = "7002";
String node2SOAHostname =
environment + "-node2SOA";
String node2SOAPort = "7002";
List server = new ArrayList();
server.add(node1BPMHostname + ":" + node1BPMPort);
server.add(node2BPMHostname + ":" + node2BPMPort);
server.add(node1SOAHostname + ":" + node1SOAPort);
server.add(node2SOAHostname + ":" + node2SOAPort);
System.out.println("Environment : "+environment);
for (int i = 0; i < server.size(); i++) {
try {
ObjectName SERVICE =
new ObjectName("com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean");
Map<String, String> map = new HashMap<String, String>();
map.put(Context.SECURITY_PRINCIPAL, userName);
map.put(Context.SECURITY_CREDENTIALS, password);
map.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
"weblogic.management.remote");
map.put("jmx.remote.x.request.waiting.timeout", "20000");
JMXServiceURL url =
new JMXServiceURL("service:jmx:t3://" + (String)server.get(i) +
"/jndi/weblogic.management.mbeanservers.runtime");
connector = JMXConnectorFactory.connect(url, map);
MBeanServerConnection connection =
connector.getMBeanServerConnection();
ObjectName serverRuntimeMBean =
(ObjectName)connection.getAttribute(SERVICE,
"ServerRuntime");
String serverName =
(String)connection.getAttribute(serverRuntimeMBean,
"Name");
System.out.println("Server name :" + serverName);
String listenAddress =
(String)connection.getAttribute(serverRuntimeMBean,
"ListenAddress");
System.out.println("Listen Address : " + listenAddress);
ObjectName jmsRuntimeMbean =
(ObjectName)connection.getAttribute(serverRuntimeMBean,
"JMSRuntime");
ObjectName[] jmsServerRuntimeMbeans =
(ObjectName[])connection.getAttribute(jmsRuntimeMbean,
"JMSServers");
for (ObjectName jmsServerRuntime : jmsServerRuntimeMbeans) {
ObjectName[] jmsDestinationRuntimeMbeans =
(ObjectName[])connection.getAttribute(jmsServerRuntime,
"Destinations");
for (ObjectName jmsDestinationRuntime :
jmsDestinationRuntimeMbeans) {
String destinationName =
(String)connection.getAttribute(jmsDestinationRuntime,
"Name");
if (destinationName.split("@").length == 2) {
destinationName =
destinationName.split("@")[1];
}
if (destinationName.split("!").length == 2) {
destinationName =
destinationName.split("!")[1];
}
// Specified topics and QUeues
if (jmsDestinations.contains(destinationName)) {
// All topics and QUeues
// if (1==1) {
System.out.println(destinationName);
//The current number of messages in the destination. This does not include the pending messages.
long messagesCurrentCount =
(Long)connection.getAttribute(jmsDestinationRuntime,
"MessagesCurrentCount");
System.out.println("messagesCurrentCount " +
messagesCurrentCount);
//Pending messages are over and above the current number of messages.
//A pending message is one that has either been sent in a transaction and not committed, or that has been received and not committed or acknowledged.
long messagesPendingCount =
(Long)connection.getAttribute(jmsDestinationRuntime,
"MessagesPendingCount");
System.out.println("messagesPendingCount " +
messagesPendingCount);
//The current number of consumers accessing this destination
long consumersCurrentCount =
(Long)connection.getAttribute(jmsDestinationRuntime,
"ConsumersCurrentCount");
System.out.println("consumersCurrentCount " +
consumersCurrentCount);
System.out.println("--------------------------------------------------");
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
connector.close();
System.out.println("Connection closed");
System.out.println("---------------------------------------------------------------");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}




Libraries


No comments:

Post a Comment