|
@@ -12,6 +12,8 @@ import cn.reghao.autodop.dagent.mqttsub.DagentTopicListener;
|
|
|
import cn.reghao.autodop.dagent.mqttsub.impl.AppRpcClazzDispatcher;
|
|
import cn.reghao.autodop.dagent.mqttsub.impl.AppRpcClazzDispatcher;
|
|
|
import cn.reghao.autodop.dagent.mqttsub.impl.AppRpcClazzImpl;
|
|
import cn.reghao.autodop.dagent.mqttsub.impl.AppRpcClazzImpl;
|
|
|
import cn.reghao.jdkutil.http.WebClient;
|
|
import cn.reghao.jdkutil.http.WebClient;
|
|
|
|
|
+import cn.reghao.jdkutil.serializer.JsonConverter;
|
|
|
|
|
+import cn.reghao.jdkutil.text.TextFile;
|
|
|
import com.sun.net.httpserver.HttpServer;
|
|
import com.sun.net.httpserver.HttpServer;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttException;
|
|
import org.eclipse.paho.client.mqttv3.MqttException;
|
|
@@ -19,6 +21,7 @@ import oshi.SystemInfo;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.net.InetSocketAddress;
|
|
import java.net.InetSocketAddress;
|
|
|
|
|
+import java.util.Random;
|
|
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
public class DagentApp {
|
|
public class DagentApp {
|
|
@@ -26,32 +29,15 @@ public class DagentApp {
|
|
|
static DagentTopicListener dagentTopicListener;
|
|
static DagentTopicListener dagentTopicListener;
|
|
|
static NodeEventClazzPubImpl nodeEventClazzPub;
|
|
static NodeEventClazzPubImpl nodeEventClazzPub;
|
|
|
|
|
|
|
|
- static void startMqtt() throws MqttException {
|
|
|
|
|
- mqttClient.add(MsgQueue.dagentTopic(Machine.ID), dagentTopicListener);
|
|
|
|
|
- DagentConnActionListener connActionListener = new DagentConnActionListener(mqttClient, nodeEventClazzPub);
|
|
|
|
|
- mqttClient.connect(connActionListener);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
static void pubDagentShutdown() {
|
|
static void pubDagentShutdown() {
|
|
|
nodeEventClazzPub.nodeShutdown();
|
|
nodeEventClazzPub.nodeShutdown();
|
|
|
log.info("Dagent 停止");
|
|
log.info("Dagent 停止");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 优雅关闭爬虫(进程级别的关闭)
|
|
|
|
|
- *
|
|
|
|
|
- * @date 2019-08-16 下午3:45
|
|
|
|
|
- */
|
|
|
|
|
static void shutdownGracefully() {
|
|
static void shutdownGracefully() {
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownHook(), "main-shutdown-hook"));
|
|
Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownHook(), "main-shutdown-hook"));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 1.可以处理 kill -2 或 kill -15
|
|
|
|
|
- * 2.无法处理 kill -9 和机器断电的情况
|
|
|
|
|
- *
|
|
|
|
|
- * @date 2019-08-16 下午4:07
|
|
|
|
|
- */
|
|
|
|
|
static class ShutdownHook implements Runnable {
|
|
static class ShutdownHook implements Runnable {
|
|
|
@Override
|
|
@Override
|
|
|
public void run() {
|
|
public void run() {
|
|
@@ -60,28 +46,32 @@ public class DagentApp {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 1.停止从 UrlScheduler 中获取数据
|
|
|
|
|
- * 2.停止向 DataProducer 提交数据
|
|
|
|
|
- * 3.线程池停止接受新任务,并等待当前执行的任务完成
|
|
|
|
|
- *
|
|
|
|
|
- * @param
|
|
|
|
|
- * @return
|
|
|
|
|
- * @date 2021-04-02 下午11:26
|
|
|
|
|
- */
|
|
|
|
|
static void stop() {
|
|
static void stop() {
|
|
|
|
|
+ pubDagentShutdown();
|
|
|
log.info("资源清理完成,结束 DagentApp...");
|
|
log.info("资源清理完成,结束 DagentApp...");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static void main(String[] args) throws MqttException, IOException {
|
|
|
|
|
- shutdownGracefully();
|
|
|
|
|
|
|
+ static void singleInstance() {
|
|
|
|
|
+ int min = 30000;
|
|
|
|
|
+ int max = 50000;
|
|
|
|
|
+ int range = max-min+1;
|
|
|
|
|
+ int port = new Random().nextInt(range) + min;
|
|
|
|
|
+ try {
|
|
|
|
|
+ HttpServer.create(new InetSocketAddress("127.0.0.1", port), 0).start();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- MqttProperties mqttProperties = new MqttProperties();
|
|
|
|
|
- mqttProperties.setBroker("tcp://localhost:1883");
|
|
|
|
|
|
|
+ public static void main(String[] args) throws MqttException {
|
|
|
|
|
+ if (args.length != 1) {
|
|
|
|
|
+ log.error("必须指定配置文件...");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ String configFilePath = ConfigFile.configFilePath(args[0], DagentApp.class);
|
|
|
|
|
+ String json = new TextFile().readFile(configFilePath);
|
|
|
|
|
+ MqttProperties mqttProperties = JsonConverter.jsonToObject(json, MqttProperties.class);
|
|
|
mqttProperties.setClientId(AppId.dagent.name());
|
|
mqttProperties.setClientId(AppId.dagent.name());
|
|
|
- mqttProperties.setUsername("dev");
|
|
|
|
|
- mqttProperties.setPassword("Dev@123456");
|
|
|
|
|
-
|
|
|
|
|
mqttClient = new AsyncMqttClient(mqttProperties);
|
|
mqttClient = new AsyncMqttClient(mqttProperties);
|
|
|
|
|
|
|
|
IAppRpcClazz appRpcClazz = new AppRpcClazzImpl();
|
|
IAppRpcClazz appRpcClazz = new AppRpcClazzImpl();
|
|
@@ -95,7 +85,7 @@ public class DagentApp {
|
|
|
DagentConnActionListener connActionListener = new DagentConnActionListener(mqttClient, nodeEventClazzPub);
|
|
DagentConnActionListener connActionListener = new DagentConnActionListener(mqttClient, nodeEventClazzPub);
|
|
|
mqttClient.connect(connActionListener);
|
|
mqttClient.connect(connActionListener);
|
|
|
|
|
|
|
|
- HttpServer.create(new InetSocketAddress("127.0.0.1", 9790), 0).start();
|
|
|
|
|
- //pubDagentShutdown();
|
|
|
|
|
|
|
+ shutdownGracefully();
|
|
|
|
|
+ singleInstance();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|