|
|
@@ -5,11 +5,15 @@ import cn.reghao.autodop.common.amqp.MqMessage;
|
|
|
import cn.reghao.autodop.common.amqp.RabbitProducer;
|
|
|
import cn.reghao.autodop.common.dagent.machine.Machine;
|
|
|
import cn.reghao.autodop.common.dagent.machine.api.MachineOps;
|
|
|
+import cn.reghao.autodop.common.mqtt.MqttPub;
|
|
|
+import cn.reghao.autodop.common.mqtt.MqttSub;
|
|
|
import cn.reghao.autodop.common.utils.serializer.JsonConverter;
|
|
|
+import cn.reghao.autodop.dagent.dispatcher.DmasterMessageDispatcher;
|
|
|
import cn.reghao.autodop.dagent.machine.timer.HeartbeatJob;
|
|
|
import cn.reghao.autodop.dagent.machine.timer.MachineScheduler;
|
|
|
import cn.reghao.autodop.dagent.utils.log.LoggerConfig;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.eclipse.paho.client.mqttv3.MqttException;
|
|
|
import org.quartz.SchedulerException;
|
|
|
import org.springframework.beans.factory.DisposableBean;
|
|
|
import org.springframework.boot.ApplicationArguments;
|
|
|
@@ -26,15 +30,22 @@ import org.springframework.stereotype.Component;
|
|
|
@Slf4j
|
|
|
@Component
|
|
|
public class DagentLifecycle implements ApplicationRunner, DisposableBean {
|
|
|
+ private MqttPub mqttPub;
|
|
|
+ private MqttSub mqttSub;
|
|
|
private RabbitProperties rabbitProperties;
|
|
|
private MachineScheduler machineScheduler;
|
|
|
private RabbitProducer rabbitProducer;
|
|
|
private Machine machine;
|
|
|
+ private String topic = "dagent@" + Machine.machineId();
|
|
|
|
|
|
- public DagentLifecycle(RabbitProperties rabbitProperties,
|
|
|
- MachineScheduler machineScheduler,
|
|
|
- RabbitProducer rabbitProducer,
|
|
|
- Machine machine) {
|
|
|
+ public DagentLifecycle(MqttPub mqttPub,
|
|
|
+ MqttSub mqttSub,
|
|
|
+ RabbitProperties rabbitProperties,
|
|
|
+ MachineScheduler machineScheduler,
|
|
|
+ RabbitProducer rabbitProducer,
|
|
|
+ Machine machine) {
|
|
|
+ this.mqttPub = mqttPub;
|
|
|
+ this.mqttSub = mqttSub;
|
|
|
this.rabbitProperties = rabbitProperties;
|
|
|
this.machineScheduler = machineScheduler;
|
|
|
this.rabbitProducer = rabbitProducer;
|
|
|
@@ -48,7 +59,8 @@ public class DagentLifecycle implements ApplicationRunner, DisposableBean {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void destroy() {
|
|
|
+ public void destroy() throws MqttException {
|
|
|
+ mqttPub.pub("dmaster", "Dagent 停止...");
|
|
|
log.info("Dagent 停止...");
|
|
|
}
|
|
|
|
|
|
@@ -56,8 +68,9 @@ public class DagentLifecycle implements ApplicationRunner, DisposableBean {
|
|
|
* @date 2019-09-26 下午5:23
|
|
|
*/
|
|
|
private void activate() throws Exception {
|
|
|
- registry();
|
|
|
- scheduledJobs();
|
|
|
+ subAndPub();
|
|
|
+ //registry();
|
|
|
+ //scheduledJobs();
|
|
|
initLogger();
|
|
|
log.info("dagent 应用已启动...");
|
|
|
}
|
|
|
@@ -70,6 +83,15 @@ public class DagentLifecycle implements ApplicationRunner, DisposableBean {
|
|
|
log.info("发送机器注册信息...");
|
|
|
}
|
|
|
|
|
|
+ private void subAndPub() throws MqttException {
|
|
|
+ mqttSub.sub(topic, new DmasterMessageDispatcher(mqttPub));
|
|
|
+ String payload = JsonConverter.objectToJson(machine.registry());
|
|
|
+ MqMessage mqMessage = MqMessage.mqMessage(MessageType.machineType.name(),
|
|
|
+ MachineOps.machineRegistryOps.name(), false, payload);
|
|
|
+ mqttPub.pub("dmaster", JsonConverter.objectToJson(mqMessage));
|
|
|
+ log.info("发送机器注册信息...");
|
|
|
+ }
|
|
|
+
|
|
|
private void scheduledJobs() throws SchedulerException {
|
|
|
machineScheduler.add(HeartbeatJob.class, "machine-heartbeat", "0/10 * * * * ?");
|
|
|
machineScheduler.start();
|