This commit is contained in:
cdf
2024-07-02 17:44:12 +08:00
parent f5881e748b
commit 59f47a0c35
2 changed files with 17 additions and 26 deletions

View File

@@ -24,7 +24,7 @@ public class ClientHandler extends Thread {
private Consumer consumer;
public String topicMsg = "暂无数据";
@Override
@@ -32,7 +32,7 @@ public class ClientHandler extends Thread {
if (consumer == null) {
System.out.println("first join ++++++++++++++++++++++++++++++init");
consumer = new Consumer();
consumer.setUmpNamesrvUrls("25.36.190.3:19776"); // 设置统一消息
consumer.setUmpNamesrvUrls("25.36.190.3:19776;25.36.190.4:19776"); // 设置统一消息
//平台的服务器地址
consumer.setAppId("X_DNZLXT"); // 设置客户端账号
consumer.setAppSecret("IoKU7u47seGwzO4CqGmCaQ=="); // 设置客户端密钥
@@ -44,30 +44,12 @@ public class ClientHandler extends Thread {
public ConsumeStatus consume(Message message) {
// 消费消息的业务逻辑
try {
System.out.println("Receive: " + new String(message.getBody(), "UTF-8"));
// 指定要写入的文件路径
String filePath = "message.text";
String filePath2 = "body.text";
try (FileWriter file = new FileWriter(filePath)) {
// 将 JSON 字符串写入文件
file.write(JSON.toJSONString(message));
System.out.println("成功将及格 JSON 数据写入文件:" + filePath);
} catch (IOException e) {
e.printStackTrace();
}
try (FileWriter file2 = new FileWriter(filePath2)) {
// 将 JSON 字符串写入文件
file2.write(JSON.toJSONString(message.getBody()));
System.out.println("成功将及格 JSON 数据写入文件:" + filePath);
} catch (IOException e) {
e.printStackTrace();
}
String msg = new String(message.getBody(), "UTF-8");
topicMsg = msg;
System.out.println("Receive: " + msg);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
e.printStackTrace();
}
// 正常接收到消息后,请务必返回 CONSUME_SUCCESS只有在业务处理失败才返回RECONSUME_LATER
return ConsumeStatus.CONSUME_SUCCESS;
@@ -75,8 +57,10 @@ public class ClientHandler extends Thread {
});
try {
consumer.start();
} catch (UmpException e) {
throw new RuntimeException(e);
} catch (Exception e) {
System.out.println("start error >>>>>>>>>>>>>>>>>>>");
consumer = null;
e.printStackTrace();
}
// 启动消费者,建议在应用程序关闭时执行
//consumer.shutdown();

View File

@@ -87,4 +87,11 @@ public class TestController {
clientHandler.start();
}
@GetMapping("show")
public String show() {
return clientHandler.topicMsg;
}
}