|
@@ -6,6 +6,7 @@ import cn.reghao.autodop.common.mqtt.MqttProperties;
|
|
|
import cn.reghao.autodop.common.msg.MsgQueue;
|
|
import cn.reghao.autodop.common.msg.MsgQueue;
|
|
|
import cn.reghao.autodop.common.msg.pub.dto.node.constant.AppId;
|
|
import cn.reghao.autodop.common.msg.pub.dto.node.constant.AppId;
|
|
|
import cn.reghao.autodop.common.msg.rpc.clazz.IAppRpcClazz;
|
|
import cn.reghao.autodop.common.msg.rpc.clazz.IAppRpcClazz;
|
|
|
|
|
+import cn.reghao.autodop.common.util.ExceptionUtil;
|
|
|
import cn.reghao.autodop.dagent.machine.NodeEventClazzPubImpl;
|
|
import cn.reghao.autodop.dagent.machine.NodeEventClazzPubImpl;
|
|
|
import cn.reghao.autodop.dagent.mqttsub.DagentConnActionListener;
|
|
import cn.reghao.autodop.dagent.mqttsub.DagentConnActionListener;
|
|
|
import cn.reghao.autodop.dagent.mqttsub.DagentTopicListener;
|
|
import cn.reghao.autodop.dagent.mqttsub.DagentTopicListener;
|
|
@@ -14,17 +15,15 @@ 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.serializer.JsonConverter;
|
|
|
import cn.reghao.jdkutil.text.TextFile;
|
|
import cn.reghao.jdkutil.text.TextFile;
|
|
|
-import com.sun.net.httpserver.HttpServer;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.eclipse.paho.client.mqttv3.MqttException;
|
|
|
|
|
import oshi.SystemInfo;
|
|
import oshi.SystemInfo;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
-import java.net.InetSocketAddress;
|
|
|
|
|
-import java.util.Random;
|
|
|
|
|
|
|
+import java.net.ServerSocket;
|
|
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
public class DagentApp {
|
|
public class DagentApp {
|
|
|
|
|
+ static ServerSocket serverSocket;
|
|
|
static AsyncMqttClient mqttClient;
|
|
static AsyncMqttClient mqttClient;
|
|
|
static DagentTopicListener dagentTopicListener;
|
|
static DagentTopicListener dagentTopicListener;
|
|
|
static NodeEventClazzPubImpl nodeEventClazzPub;
|
|
static NodeEventClazzPubImpl nodeEventClazzPub;
|
|
@@ -47,23 +46,34 @@ public class DagentApp {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
static void stop() {
|
|
static void stop() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ serverSocket.close();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ String errmsg = ExceptionUtil.errorMsg(e);
|
|
|
|
|
+ log.error("关闭 54321 端口出错 {}", errmsg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
pubDagentShutdown();
|
|
pubDagentShutdown();
|
|
|
- log.info("资源清理完成,结束 DagentApp...");
|
|
|
|
|
|
|
+ log.info("资源清理完成,结束 autodop-dagent...");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- static void singleInstance() {
|
|
|
|
|
- int min = 30000;
|
|
|
|
|
- int max = 50000;
|
|
|
|
|
- int range = max-min+1;
|
|
|
|
|
- int port = new Random().nextInt(range) + min;
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 通过监听一个端口来保证一台机器上只运行一个 autodop-dagent 实例
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param
|
|
|
|
|
+ * @return
|
|
|
|
|
+ * @date 2021-11-02 下午3:01
|
|
|
|
|
+ */
|
|
|
|
|
+ static void singleInstance() throws Exception {
|
|
|
try {
|
|
try {
|
|
|
- HttpServer.create(new InetSocketAddress("127.0.0.1", port), 0).start();
|
|
|
|
|
|
|
+ serverSocket = new ServerSocket(54321);
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
|
|
|
|
+ throw new Exception("已有一个 autodop-dagent 实例在运行, 一台机器上只能运行一个 autodop-dagent 实例.");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static void main(String[] args) throws MqttException {
|
|
|
|
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
|
|
+ singleInstance();
|
|
|
if (args.length != 1) {
|
|
if (args.length != 1) {
|
|
|
log.error("必须指定配置文件...");
|
|
log.error("必须指定配置文件...");
|
|
|
return;
|
|
return;
|
|
@@ -84,8 +94,6 @@ public class DagentApp {
|
|
|
mqttClient.add(MsgQueue.dagentTopic(Machine.ID), dagentTopicListener);
|
|
mqttClient.add(MsgQueue.dagentTopic(Machine.ID), dagentTopicListener);
|
|
|
DagentConnActionListener connActionListener = new DagentConnActionListener(mqttClient, nodeEventClazzPub);
|
|
DagentConnActionListener connActionListener = new DagentConnActionListener(mqttClient, nodeEventClazzPub);
|
|
|
mqttClient.connect(connActionListener);
|
|
mqttClient.connect(connActionListener);
|
|
|
-
|
|
|
|
|
shutdownGracefully();
|
|
shutdownGracefully();
|
|
|
- singleInstance();
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|