Jolokia相关

>>测试代码

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

}

}

}

////////////////////////////////////////////////////////////////
Jolokia 是一个用于在 Java 应用程序中进行 JMX(Java Management Extensions)操作的代理,可通过 HTTP 协议与 JMX MBean 进行交互,以获取各种监控信息,包括事务(transaction)信息。以下是使用 Jolokia 获取事务信息的一般步骤和示例。 ### 1. 确保 Jolokia 已集成到应用中 要使用 Jolokia,首先需要将其集成到 Java 应用程序中。如果使用的是 Spring Boot 应用,可以通过添加 Jolokia 的依赖来集成: ```xml <dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId> <version>1.7.1</version> </dependency> ``` ### 2. 确定事务 MBean 的名称 不同的应用服务器和事务管理器,其事务相关的 MBean 名称会有所不同。例如,在 Spring Boot 应用中使用的是 Spring 的事务管理器,可能需要查找与事务相关的 MBean。 ### 3. 使用 Jolokia API 获取事务信息 可以使用 Jolokia 的 HTTP API 来获取事务信息。以下是一个使用 Python 的 `requests` 库来调用 Jolokia API 的示例: ```python import requests # Jolokia 服务的 URL jolokia_url = "http://localhost:8080/jolokia" # 构建 Jolokia 请求 request = { "type": "read", "mbean": "com.example:type=TransactionManager,name=MyTransactionManager", # 替换为实际的 MBean 名称 "attribute": "TransactionCount" # 替换为实际的属性名称 } # 发送请求 response = requests.post(jolokia_url, json=request) # 处理响应 if response.status_code == 200: data = response.json() if data["status"] == 200: transaction_count = data["value"] print(f"Transaction Count: {transaction_count}") else: print(f"Jolokia request failed: {data['error']}") else: print(f"HTTP request failed: {response.status_code}") ``` ### 4. 处理不同的事务属性 除了事务计数,还可以获取其他事务相关的属性,如活动事务数、事务平均时间等。只需将 `attribute` 字段替换为相应的属性名称即可。 ### 5. 批量请求 如果需要获取多个事务相关的属性,可以使用 Jolokia 的批量请求功能: ```python import requests jolokia_url = "http://localhost:8080/jolokia" requests_list = [ { "type": "read", "mbean": "com.example:type=TransactionManager,name=MyTransactionManager", "attribute": "TransactionCount" }, { "type": "read", "mbean": "com.example:type=TransactionManager,name=MyTransactionManager", "attribute": "ActiveTransactionCount" } ] response = requests.post(jolokia_url, json=requests_list) if response.status_code == 200: data_list = response.json() for data in data_list: if data["status"] == 200: attribute_name = data["request"]["attribute"] value = data["value"] print(f"{attribute_name}: {value}") else: print(f"Jolokia request failed: {data['error']}") else: print(f"HTTP request failed: {response.status_code}") ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值