DeviceAddressProducer.java
1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.glsx.smart.config;
import com.alibaba.fastjson.JSON;
import com.glsx.smart.pojo.DeviceMessage;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* @author think.wang
* @version 1.0.0
* @Title DeviceAddressProducer
* @Package com.glsx.smart.config
* @Description 设备围栏数据kafka推送
* @date 2021/4/11
*/
@Slf4j
@Component
public class DeviceAddressProducer {
@Resource
private Producer producer;
@Autowired
private KafkaConfig kafkaConfig;
/**
* 推送设备经纬度数据
* @param deviceId
* @param lngAndLat
* @param type 1新增 2更新 3删除
*/
public void pushUserDeviceData(String deviceId, String lngAndLat, Integer type) {
//kafka 发送消息数据封装
DeviceMessage data = new DeviceMessage();
data.setDeviceId(deviceId);
data.setLngAndLat(lngAndLat);
data.setType(type);
try {
String topic = kafkaConfig.getDeviceTopic();
String sendMessage = JSON.toJSONString(data);
log.info("推送kafka topic【{}】的数据:{}", topic, sendMessage);
KeyedMessage<byte[], byte[]> message = new KeyedMessage<>(topic, deviceId.getBytes(), sendMessage.getBytes());
producer.send(message);
}catch (Exception e){
log.error("消息推送kafka失败:{}", e);
}
}
}