|
@@ -5,15 +5,12 @@ import com.persagy.iottransfer.communication.entity.Packet;
|
|
|
import com.persagy.iottransfer.communication.entity.PacketEntity;
|
|
|
import com.persagy.iottransfer.server.IotServer;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.kafka.clients.consumer.ConsumerRecord;
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
import org.springframework.kafka.annotation.KafkaListener;
|
|
|
import org.springframework.kafka.support.Acknowledgment;
|
|
|
-import org.springframework.kafka.support.KafkaHeaders;
|
|
|
-import org.springframework.messaging.handler.annotation.Header;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import java.util.Optional;
|
|
|
+import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* @description:
|
|
@@ -26,28 +23,32 @@ import java.util.Optional;
|
|
|
public class KafkaConsumerCloud2Edge {
|
|
|
|
|
|
@KafkaListener(topics = KafkaProducer.TOPIC_COLLECT2EDGE, groupId = "${persagy.group.iot:group_iot}")
|
|
|
- public void topicCollect2Edge(ConsumerRecord<String, String> record, Acknowledgment ack, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
|
|
|
- Optional<String> message = Optional.ofNullable(record.value());
|
|
|
- if (message.isPresent()) {
|
|
|
- PacketEntity packetEntity = JSONObject.parseObject(message.get(), PacketEntity.class);
|
|
|
- String projectId = packetEntity.getProjectId();
|
|
|
- Packet packet = packetEntity.getContent();
|
|
|
- IotServer.tcpCollectServerManager.AppendToSendByProject(projectId, packet);
|
|
|
- log.info("topicCollect2Edge 消费了: Topic:" + topic + ",Message:" + packetEntity + ",Offset:" + record.offset());
|
|
|
- ack.acknowledge();
|
|
|
+ public void topicCollect2Edge(List<String> records, Acknowledgment ack) {
|
|
|
+ log.info("persagy.iot.collect2edge收到数据[{}]条,数据为:{}", records.size(), records);
|
|
|
+ try {
|
|
|
+ for (String record : records) {
|
|
|
+ PacketEntity packetEntity = JSONObject.parseObject(record, PacketEntity.class);
|
|
|
+ String projectId = packetEntity.getProjectId();
|
|
|
+ Packet packet = packetEntity.getContent();
|
|
|
+ IotServer.tcpCollectServerManager.AppendToSendByProject(projectId, packet);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ ack.nack(2000);
|
|
|
+ return;
|
|
|
}
|
|
|
+ ack.acknowledge();
|
|
|
}
|
|
|
|
|
|
@KafkaListener(topics = KafkaProducer.TOPIC_CONTROL2EDGE, groupId = "${persagy.group.iot:group_iot}")
|
|
|
- public void topicControl2Edge(ConsumerRecord<String, String> record, Acknowledgment ack, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
|
|
|
- Optional<String> message = Optional.ofNullable(record.value());
|
|
|
- if (message.isPresent()) {
|
|
|
- PacketEntity packetEntity = JSONObject.parseObject(message.get(), PacketEntity.class);
|
|
|
+ public void topicControl2Edge(List<String> records, Acknowledgment ack) {
|
|
|
+ log.info("persagy.iot.control2edge收到数据[{}]条,数据为:{}", records.size(), records);
|
|
|
+ for (String record : records) {
|
|
|
+ PacketEntity packetEntity = JSONObject.parseObject(record, PacketEntity.class);
|
|
|
String projectId = packetEntity.getProjectId();
|
|
|
Packet packet = packetEntity.getContent();
|
|
|
IotServer.tcpControlServerManager.AppendToSendByProject(projectId, packet);
|
|
|
- log.info("topicControl2Edge 消费了: Topic:" + topic + ",Message:" + packetEntity + ",Offset:" + record.offset());
|
|
|
- ack.acknowledge();
|
|
|
}
|
|
|
+ ack.acknowledge();
|
|
|
}
|
|
|
}
|