|
|
@@ -5,9 +5,7 @@ import cn.reghao.autodop.common.message.AsyncMsg;
|
|
|
import cn.reghao.autodop.common.message.MessageType;
|
|
|
import cn.reghao.autodop.common.dagent.machine.Machine;
|
|
|
import cn.reghao.autodop.common.message.ops.DagentOps;
|
|
|
-import cn.reghao.autodop.common.mqtt.MosquittoProperties;
|
|
|
-import cn.reghao.autodop.common.mqtt.MqttPub;
|
|
|
-import cn.reghao.autodop.common.mqtt.MqttSub;
|
|
|
+import cn.reghao.autodop.common.mqtt.DefaultMqttClient;
|
|
|
import cn.reghao.autodop.common.utils.MachineIdentity;
|
|
|
import cn.reghao.autodop.common.utils.serializer.JsonConverter;
|
|
|
import cn.reghao.autodop.dagent.mqttsub.DmasterMsgDispatcher;
|
|
|
@@ -20,7 +18,7 @@ import org.quartz.SchedulerException;
|
|
|
import org.springframework.beans.factory.DisposableBean;
|
|
|
import org.springframework.boot.ApplicationArguments;
|
|
|
import org.springframework.boot.ApplicationRunner;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
@@ -31,35 +29,30 @@ import java.io.IOException;
|
|
|
* @date 2021-04-01 11:36:51
|
|
|
*/
|
|
|
@Slf4j
|
|
|
-@Component
|
|
|
+@Configuration
|
|
|
public class DagentLifecycle implements ApplicationRunner, DisposableBean {
|
|
|
public static String MACHINE_ID;
|
|
|
public static String MACHINE_IPV4;
|
|
|
|
|
|
- private MosquittoProperties properties;
|
|
|
private DmasterMsgDispatcher dmasterMsgDispatcher;
|
|
|
- private MqttPub mqttPub;
|
|
|
- private MqttSub mqttSub;
|
|
|
+ private DefaultMqttClient mqttClient;
|
|
|
private MachineScheduler machineScheduler;
|
|
|
private Machine machine;
|
|
|
- private String topic = "dagent/" + DagentLifecycle.MACHINE_ID;
|
|
|
+ private String topic;
|
|
|
|
|
|
public DagentLifecycle(MachineIdentity machineIdentity,
|
|
|
- MosquittoProperties properties,
|
|
|
DmasterMsgDispatcher dmasterMsgDispatcher,
|
|
|
- MqttPub mqttPub,
|
|
|
+ DefaultMqttClient mqttClient,
|
|
|
MachineScheduler machineScheduler,
|
|
|
- Machine machine,
|
|
|
- MosquittoProperties mosquittoProperties) throws IOException, MqttException {
|
|
|
+ Machine machine) throws IOException {
|
|
|
MACHINE_ID = machineIdentity.id();
|
|
|
MACHINE_IPV4 = machineIdentity.ipv4();
|
|
|
- this.properties = properties;
|
|
|
this.dmasterMsgDispatcher = dmasterMsgDispatcher;
|
|
|
- this.mqttPub = mqttPub;
|
|
|
- this.mqttSub = new MqttSub("dagent" + MACHINE_ID, mosquittoProperties);
|
|
|
+ this.mqttClient = mqttClient;
|
|
|
this.machineScheduler = machineScheduler;
|
|
|
this.machine = machine;
|
|
|
- initLogger(machineIdentity);
|
|
|
+ this.topic = "dagent/" + DagentLifecycle.MACHINE_ID;
|
|
|
+ initLogger();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -72,14 +65,14 @@ public class DagentLifecycle implements ApplicationRunner, DisposableBean {
|
|
|
String payload = MACHINE_ID;
|
|
|
AsyncMsg asyncMsg = AsyncMsg.asyncMsg(MACHINE_ID, MessageType.dagentType.name(),
|
|
|
DagentOps.dagnetShutdown.name(), payload);
|
|
|
- mqttPub.pub("dmaster", JsonConverter.objectToJson(asyncMsg));
|
|
|
+ mqttClient.pub("dmaster", 1, JsonConverter.objectToJson(asyncMsg));
|
|
|
log.info("Dagent 停止...");
|
|
|
}
|
|
|
|
|
|
- private void initLogger(MachineIdentity machineIdentity) throws IOException {
|
|
|
+ private void initLogger() {
|
|
|
Appenders appenders = new Appenders();
|
|
|
LoggerConfig loggerConfig = new LoggerConfig();
|
|
|
- loggerConfig.addAppender(appenders.mqttAppender(machineIdentity, properties, "dagent"));
|
|
|
+ loggerConfig.addAppender(appenders.mqttAppender(MACHINE_ID, MACHINE_IPV4, "dagent", mqttClient));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -92,12 +85,12 @@ public class DagentLifecycle implements ApplicationRunner, DisposableBean {
|
|
|
}
|
|
|
|
|
|
private void subAndPub() throws MqttException {
|
|
|
- mqttSub.sub(topic, dmasterMsgDispatcher);
|
|
|
+ mqttClient.sub(topic, dmasterMsgDispatcher);
|
|
|
|
|
|
String payload = JsonConverter.objectToJson(machine.registry());
|
|
|
AsyncMsg asyncMsg = AsyncMsg.asyncMsg(MACHINE_ID, MessageType.dagentType.name(),
|
|
|
DagentOps.dagentStart.name(), payload);
|
|
|
- mqttPub.pub("dmaster", JsonConverter.objectToJson(asyncMsg));
|
|
|
+ mqttClient.pub("dmaster", 1, JsonConverter.objectToJson(asyncMsg));
|
|
|
}
|
|
|
|
|
|
private void scheduledJobs() throws SchedulerException {
|