>>测试代码
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.jolokia.client.J4pClient;
import org.jolokia.client.request.J4pExecRequest;
import org.jolokia.client.request.J4pExecResponse;
import org.jolokia.client.request.J4pReadRequest;
import org.jolokia.client.request.J4pReadResponse;
public class TestClient {
public static void main(String[] args) throws Exception {
J4pClient j4pClient = new J4pClient("http://cnd:8161/jolokia");
// 执行execute操作==>通过jolokia获取MBean信息==>mBeanServersInfo
if (false) {
// 1.构造请求参数,指定MBean的对象名称和方法及参数
String[] params = {};// 注意,如果是无参数的方法,则需要传入一个空的数组,而不能是null的数组。
J4pExecRequest reqForExec = new J4pExecRequest(
"jolokia:type=ServerHandler", "mBeanServersInfo()",
(Object[]) params);
// 2.提交请求
J4pExecResponse respForExec = j4pClient.execute(reqForExec);
// 3.处理响应结果
System.out.println("=response is:=" + respForExec.getValue()
+ "=end=");
}
// 执行read操作,获取Broker属性
if (false) {
// 1.构造请求参数,指定MBean的名称
J4pReadRequest req = new J4pReadRequest(
"com.rf.emq:type=Broker,brokerName=broker-cnd");
// 2.提交请求
J4pReadResponse resp = j4pClient.execute(req);
// 3.将获取的值转化为Map
Map<String, Object> vals = resp.getValue();
for (Iterator<String> iter = vals.keySet().iterator(); iter
.hasNext();) {
String key = iter.next();
System.out.println("=" + key + " is:=" + vals.get(key)
+ "=end=");
}
}
// 执行read操作,获取队列属性
if (false) {
// 1.构造请求参数,指定MBean的名称
J4pReadRequest req = new J4pReadRequest(
"com.rf.emq:type=Broker,brokerName=broker-cnd,destinationType=Queue,destinationName=can_do");
// 2.提交请求
J4pReadResponse resp = j4pClient.execute(req);
// 3.将获取的值转化为Map
Map<String, Object> vals = resp.getValue();
for (Iterator<String> iter = vals.keySet().iterator(); iter
.hasNext();) {
String key = iter.next();
System.out.println("=" + key + " is:=" + vals.get(key)
+ "=end=");
}
}
// 执行read操作,获取JVM运行时属性
if (false) {
// 1.构造请求参数,指定MBean的名称
J4pReadRequest req = new J4pReadRequest("java.lang:type=Runtime");
// 2.提交请求
J4pReadResponse resp = j4pClient.execute(req);
// 3.将获取的值转化为Map
Map<String, Object> vals = resp.getValue();
for (Iterator<String> iter = vals.keySet().iterator(); iter
.hasNext();) {
String key = iter.next();
System.out.println("=" + key + " is:=" + vals.get(key)
+ "=end=");
}
}
// 执行read操作,获取OS属性
if (true) {
// 1.构造请求参数,指定MBean的名称
J4pReadRequest req = new J4pReadRequest(
"java.lang:type=OperatingSystem");
// 2.提交请求
J4pReadResponse resp = j4pClient.execute(req);
// 3.将获取的值转化为Map
Map<String, Object> vals = resp.getValue();
for (Iterator<String> iter = vals.keySet().iterator(); iter
.hasNext();) {
String key = iter.next();
System.out.println("=" + key + " is:=" + vals.get(key)
+ "=end=");
}
}
// 执行read操作,获取线程属性
if (false) {
// 1.构造请求参数,指定MBean的名称
J4pReadRequest req = new J4pReadRequest("java.lang:type=Threading");
// 2.提交请求
J4pReadResponse resp = j4pClient.execute(req);
// 3.将获取的值转化为Map
Map<String, Object> vals = resp.getValue();
for (Iterator<String> iter = vals.keySet().iterator(); iter
.hasNext();) {
String key = iter.next();
System.out.println("=" + key + " is:=" + vals.get(key)
+ "=end=");
}
}
// 执行read操作,获取JVM内存属性
if (false) {
// 1.构造请求参数,指定MBean的名称
J4pReadRequest req = new J4pReadRequest("java.lang:type=Memory");
// 2.提交请求
J4pReadResponse resp = j4pClient.execute(req);
// 3.将获取的值转化为Map
Map<String, Object> vals = resp.getValue();
for (Iterator<String> iter = vals.keySet().iterator(); iter
.hasNext();) {
String key = iter.next();
System.out.println("=" + key + " is:=" + vals.get(key)
+ "=end=");
}
}
// 执行read操作,获取装载类的信息属性
if (false) {
// 1.构造请求参数,指定MBean的名称
J4pReadRequest req = new J4pReadRequest(
"java.lang:type=ClassLoading");
// 2.提交请求
J4pReadResponse resp = j4pClient.execute(req);
// 3.将获取的值转化为Map
Map<String, Object> vals = resp.getValue();
for (Iterator<String> iter = vals.keySet().iterator(); iter
.hasNext();) {
String key = iter.next();
System.out.println("=" + key + " is:=" + vals.get(key)
+ "=end=");
}
}
// 执行execute操作==>添加队列==>addQueue
if (false) {
// 1.构造请求参数,指定MBean的对象名称和方法及参数
String[] params = { "can_do" };
J4pExecRequest reqForExec = new J4pExecRequest(
"com.rf.emq:type=Broker,brokerName=broker-cnd", "addQueue",
(Object[]) params);
// 2.提交请求
J4pExecResponse respForExec = j4pClient.execute(reqForExec);
// 3.处理响应结果
System.out.println("=response is:=" + respForExec.getValue());
}
// 执行execute操作==>删除队列==>removeQueue
if (false) {
// 1.构造请求参数,指定MBean的对象名称和方法及参数
String[] params = { "can_do" };
J4pExecRequest reqForExec = new J4pExecRequest(
"com.rf.emq:type=Broker,brokerName=broker-cnd",
"removeQueue", (Object[]) params);
// 2.提交请求
J4pExecResponse respForExec = j4pClient.execute(reqForExec);
// 3.处理响应结果
System.out.println("=response is:=" + respForExec.getValue());
}
// 执行execute操作==>发送消息==>sendTextMessage==>
// 有多个同名方法时,通过jolokia调用问题,==>重载方法的话,需要通过signature来指定具体用哪个方法
// sendTextMessageWithProperties is ok
if (false) {
// 1.构造请求参数,指定MBean的对象名称和方法及参数
String[] params = { "can_do" };
J4pExecRequest reqForExec = new J4pExecRequest(
"com.rf.emq:type=Broker,brokerName=broker-cnd,destinationType=Queue,destinationName=can_do",
"sendTextMessage(java.lang.String)", (Object[]) params);
// 2.提交请求
J4pExecResponse respForExec = j4pClient.execute(reqForExec);
// 3.处理响应结果
System.out.println("=response is:=" + respForExec.getValue());
}
// 执行execute操作==>发送消息==>sendTextMessage==>发送消息时指定消息的属性
// 有多个同名方法时,通过jolokia调用问题,==>重载方法的话,需要通过signature来指定具体用哪个方法
// sendTextMessageWithProperties is ok
// 没法发送持久化消息,因为持久化消息的发送,是通过Producer的属性设置进去的,而此处Producer是不可见的。
// 通过jolokia可以对系统进行监控,而不要试图去替代jms所能做到的方面
if (false) {
// 1.构造请求参数,指定MBean的对象名称和方法及参数
Map<String, String> msgHeader = new HashMap<String, String>();
//此处消息属性怎么设置进Map中?
msgHeader.put("JMSDeliveryMode", "true");//JMSDeliveryMode||priority
Object[] params = { msgHeader,"can_do" };
J4pExecRequest reqForExec = new J4pExecRequest(
"com.rf.emq:type=Broker,brokerName=broker-cnd,destinationType=Queue,destinationName=can_do",
"sendTextMessage(java.util.Map,java.lang.String)", params);
// 2.提交请求
J4pExecResponse respForExec = j4pClient.execute(reqForExec);
// 3.处理响应结果
System.out.println("=response is:=" + respForExec.getValue());
}
// 执行execute操作==>浏览队列==>browse
if (false) {
// 1.构造请求参数,指定MBean的对象名称和方法及参数
String[] params = {};// 注意,如果是无参数的方法,则需要传入一个空的数组,而不能是null的数组。
J4pExecRequest reqForExec = new J4pExecRequest(
"com.rf.emq:type=Broker,brokerName=broker-cnd,destinationType=Queue,destinationName=can_do",
"browse()", (Object[]) params);
// 2.提交请求
J4pExecResponse respForExec = j4pClient.execute(reqForExec);
// 3.处理响应结果
System.out.println("=response is:=" + respForExec.getValue());
}
// 执行execute操作==>清空队列消息==>purge
if (false) {
// 1.构造请求参数,指定MBean的对象名称和方法及参数
String[] params = {};// 注意,如果是无参数的方法,则需要传入一个空的数组,而不能是null的数组。
J4pExecRequest reqForExec = new J4pExecRequest(
"com.rf.emq:type=Broker,brokerName=broker-cnd,destinationType=Queue,destinationName=can_do",
"purge()", (Object[]) params);
// 2.提交请求
J4pExecResponse respForExec = j4pClient.execute(reqForExec);
// 3.处理响应结果
System.out.println("=response is:=" + respForExec.getValue());
}
}
}
////////////////////////////////////////////////////////////////