|
|
@@ -1,14 +1,13 @@
|
|
|
package com.middle.platform.demo.service;
|
|
|
|
|
|
-import cn.hutool.core.date.LocalDateTimeUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.kafka.core.KafkaTemplate;
|
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import java.util.Random;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
@@ -18,6 +17,7 @@ import java.util.concurrent.TimeUnit;
|
|
|
@EnableScheduling
|
|
|
@Component
|
|
|
@RequiredArgsConstructor
|
|
|
+@Slf4j
|
|
|
public class KafkaService {
|
|
|
private final KafkaTemplate<String, Object> kafkaTemplate;
|
|
|
|
|
|
@@ -26,9 +26,14 @@ public class KafkaService {
|
|
|
*/
|
|
|
@Scheduled(initialDelay = 0, fixedRate = 10, timeUnit = TimeUnit.SECONDS)
|
|
|
public void job() {
|
|
|
+ log.info("task");
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
- jsonObject.put("123", LocalDateTimeUtil.formatNormal(LocalDateTimeUtil.now()));
|
|
|
- jsonObject.put("temp", new Random().nextInt());
|
|
|
- kafkaTemplate.send("iot.sn", jsonObject.toJSONString());
|
|
|
+ jsonObject.put("productKey", "PDT012024051300004");
|
|
|
+ jsonObject.put("deviceSn", "KAFKA");
|
|
|
+ JSONObject jsonObject1 = new JSONObject();
|
|
|
+ jsonObject1.put("temp", String.format("%.2f", Math.random() * 100));
|
|
|
+ jsonObject1.put("hum", String.format("%.2f", Math.random() * 100));
|
|
|
+ jsonObject.put("data", jsonObject1);
|
|
|
+ kafkaTemplate.send("iot.data", jsonObject.toJSONString());
|
|
|
}
|
|
|
}
|