first
This commit is contained in:
@@ -24,7 +24,7 @@ public class ClientHandler extends Thread {
|
|||||||
|
|
||||||
private Consumer consumer;
|
private Consumer consumer;
|
||||||
|
|
||||||
|
public String topicMsg = "暂无数据";
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -32,7 +32,7 @@ public class ClientHandler extends Thread {
|
|||||||
if (consumer == null) {
|
if (consumer == null) {
|
||||||
System.out.println("first join ++++++++++++++++++++++++++++++init");
|
System.out.println("first join ++++++++++++++++++++++++++++++init");
|
||||||
consumer = new Consumer();
|
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.setAppId("X_DNZLXT"); // 设置客户端账号
|
||||||
consumer.setAppSecret("IoKU7u47seGwzO4CqGmCaQ=="); // 设置客户端密钥
|
consumer.setAppSecret("IoKU7u47seGwzO4CqGmCaQ=="); // 设置客户端密钥
|
||||||
@@ -44,30 +44,12 @@ public class ClientHandler extends Thread {
|
|||||||
public ConsumeStatus consume(Message message) {
|
public ConsumeStatus consume(Message message) {
|
||||||
// 消费消息的业务逻辑
|
// 消费消息的业务逻辑
|
||||||
try {
|
try {
|
||||||
System.out.println("Receive: " + new String(message.getBody(), "UTF-8"));
|
String msg = new String(message.getBody(), "UTF-8");
|
||||||
// 指定要写入的文件路径
|
topicMsg = msg;
|
||||||
String filePath = "message.text";
|
System.out.println("Receive: " + msg);
|
||||||
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) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
throw new RuntimeException(e);
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
// 正常接收到消息后,请务必返回 CONSUME_SUCCESS,只有在业务处理失败才返回RECONSUME_LATER
|
// 正常接收到消息后,请务必返回 CONSUME_SUCCESS,只有在业务处理失败才返回RECONSUME_LATER
|
||||||
return ConsumeStatus.CONSUME_SUCCESS;
|
return ConsumeStatus.CONSUME_SUCCESS;
|
||||||
@@ -75,8 +57,10 @@ public class ClientHandler extends Thread {
|
|||||||
});
|
});
|
||||||
try {
|
try {
|
||||||
consumer.start();
|
consumer.start();
|
||||||
} catch (UmpException e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
System.out.println("start error >>>>>>>>>>>>>>>>>>>");
|
||||||
|
consumer = null;
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
// 启动消费者,建议在应用程序关闭时执行
|
// 启动消费者,建议在应用程序关闭时执行
|
||||||
//consumer.shutdown();
|
//consumer.shutdown();
|
||||||
|
|||||||
@@ -87,4 +87,11 @@ public class TestController {
|
|||||||
clientHandler.start();
|
clientHandler.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("show")
|
||||||
|
public String show() {
|
||||||
|
return clientHandler.topicMsg;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user