This commit is contained in:
hzj
2024-07-01 16:26:20 +08:00
parent 1dd952f543
commit ee3f71089b

View File

@@ -1,5 +1,7 @@
package com.njcn.roma.client;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.huawei.it.eip.ump.client.consumer.ConsumeStatus;
import com.huawei.it.eip.ump.client.consumer.Consumer;
import com.huawei.it.eip.ump.client.listener.MessageListener;
@@ -7,6 +9,8 @@ import com.huawei.it.eip.ump.common.exception.UmpException;
import com.huawei.it.eip.ump.common.message.Message;
import org.springframework.stereotype.Component;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
/**
@@ -21,6 +25,8 @@ public class ClientHandler extends Thread {
private Consumer consumer;
@Override
public void run() {
if (consumer == null) {
@@ -39,6 +45,27 @@ public class ClientHandler extends Thread {
// 消费消息的业务逻辑
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();
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}