|
|
@@ -0,0 +1,101 @@
|
|
|
+package cn.reghao.autodop.dagent;
|
|
|
+
|
|
|
+import cn.reghao.autodop.common.machine.Machine;
|
|
|
+import cn.reghao.autodop.common.mqtt.AsyncMqttClient;
|
|
|
+import cn.reghao.autodop.common.mqtt.MqttProperties;
|
|
|
+import cn.reghao.autodop.common.msg.MsgQueue;
|
|
|
+import cn.reghao.autodop.common.msg.pub.dto.node.constant.AppId;
|
|
|
+import cn.reghao.autodop.common.msg.rpc.clazz.IAppRpcClazz;
|
|
|
+import cn.reghao.autodop.dagent.machine.NodeEventClazzPubImpl;
|
|
|
+import cn.reghao.autodop.dagent.mqttsub.DagentConnActionListener;
|
|
|
+import cn.reghao.autodop.dagent.mqttsub.DagentTopicListener;
|
|
|
+import cn.reghao.autodop.dagent.mqttsub.impl.AppRpcClazzDispatcher;
|
|
|
+import cn.reghao.autodop.dagent.mqttsub.impl.AppRpcClazzImpl;
|
|
|
+import cn.reghao.jdkutil.http.WebClient;
|
|
|
+import com.sun.net.httpserver.HttpServer;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.eclipse.paho.client.mqttv3.MqttException;
|
|
|
+import oshi.SystemInfo;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.net.InetSocketAddress;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class DagentApp {
|
|
|
+ static AsyncMqttClient mqttClient;
|
|
|
+ static DagentTopicListener dagentTopicListener;
|
|
|
+ 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() {
|
|
|
+ nodeEventClazzPub.nodeShutdown();
|
|
|
+ log.info("Dagent 停止");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 优雅关闭爬虫(进程级别的关闭)
|
|
|
+ *
|
|
|
+ * @date 2019-08-16 下午3:45
|
|
|
+ */
|
|
|
+ static void shutdownGracefully() {
|
|
|
+ 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 {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ log.info("清理资源后关闭应用...");
|
|
|
+ stop();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 1.停止从 UrlScheduler 中获取数据
|
|
|
+ * 2.停止向 DataProducer 提交数据
|
|
|
+ * 3.线程池停止接受新任务,并等待当前执行的任务完成
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ * @date 2021-04-02 下午11:26
|
|
|
+ */
|
|
|
+ static void stop() {
|
|
|
+ log.info("资源清理完成,结束 DagentApp...");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws MqttException, IOException {
|
|
|
+ shutdownGracefully();
|
|
|
+
|
|
|
+ MqttProperties mqttProperties = new MqttProperties();
|
|
|
+ mqttProperties.setBroker("tcp://localhost:1883");
|
|
|
+ mqttProperties.setClientId(AppId.dagent.name());
|
|
|
+ mqttProperties.setUsername("dev");
|
|
|
+ mqttProperties.setPassword("Dev@123456");
|
|
|
+
|
|
|
+ mqttClient = new AsyncMqttClient(mqttProperties);
|
|
|
+
|
|
|
+ IAppRpcClazz appRpcClazz = new AppRpcClazzImpl();
|
|
|
+ AppRpcClazzDispatcher appRpcClazzDispatcher = new AppRpcClazzDispatcher(appRpcClazz);
|
|
|
+ dagentTopicListener = new DagentTopicListener(mqttClient, appRpcClazzDispatcher);
|
|
|
+
|
|
|
+ Machine machine = new Machine(new WebClient(), new SystemInfo());
|
|
|
+ nodeEventClazzPub = new NodeEventClazzPubImpl(mqttClient, machine);
|
|
|
+
|
|
|
+ mqttClient.add(MsgQueue.dagentTopic(Machine.ID), dagentTopicListener);
|
|
|
+ DagentConnActionListener connActionListener = new DagentConnActionListener(mqttClient, nodeEventClazzPub);
|
|
|
+ mqttClient.connect(connActionListener);
|
|
|
+
|
|
|
+ HttpServer.create(new InetSocketAddress("127.0.0.1", 9790), 0).start();
|
|
|
+ //pubDagentShutdown();
|
|
|
+ }
|
|
|
+}
|