From 898be955bd5de3cb9b2c578cfb1a27ca1535f6d2 Mon Sep 17 00:00:00 2001 From: wanggeng <450292408@qq.com> Date: Thu, 23 Dec 2021 22:40:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84kafka=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../consts/ISmartCityUserCenterConsts.java | 7 +++ .../kafka/KafKaPublishConsumer.java | 5 +- .../grid/impl/GridDeleteAfterHandlerImpl.java | 12 ++--- .../impl/GridPointDeleteAfterHandlerImpl.java | 43 +++++++++++++++ .../impl/GridPointSaveAfterHandlerImpl.java | 54 +++++++++++++++++++ .../grid/impl/GridSaveAfterHandlerImpl.java | 7 +-- .../grid/impl/GridUpdateAfterHandlerImpl.java | 7 +-- 7 files changed, 119 insertions(+), 16 deletions(-) create mode 100644 src/main/java/cn/com/tenlion/usercenter/service/grid/impl/GridPointDeleteAfterHandlerImpl.java create mode 100644 src/main/java/cn/com/tenlion/usercenter/service/grid/impl/GridPointSaveAfterHandlerImpl.java diff --git a/src/main/java/cn/com/tenlion/usercenter/consts/ISmartCityUserCenterConsts.java b/src/main/java/cn/com/tenlion/usercenter/consts/ISmartCityUserCenterConsts.java index 399d908..ff86138 100644 --- a/src/main/java/cn/com/tenlion/usercenter/consts/ISmartCityUserCenterConsts.java +++ b/src/main/java/cn/com/tenlion/usercenter/consts/ISmartCityUserCenterConsts.java @@ -14,4 +14,11 @@ public interface ISmartCityUserCenterConsts { */ String APP_CLIENT_NAME = "city-governance"; + String KAFKA_TOPIC_GRID = "C0021"; + String KAFKA_TOPIC_GRID_POINT = "C0022"; + + String KAFKA_DATA_SAVE = "save"; + String KAFKA_DATA_UPDATE = "update"; + String KAFKA_DATA_DELETE = "delete"; + } diff --git a/src/main/java/cn/com/tenlion/usercenter/kafka/KafKaPublishConsumer.java b/src/main/java/cn/com/tenlion/usercenter/kafka/KafKaPublishConsumer.java index c4da093..0d92de0 100644 --- a/src/main/java/cn/com/tenlion/usercenter/kafka/KafKaPublishConsumer.java +++ b/src/main/java/cn/com/tenlion/usercenter/kafka/KafKaPublishConsumer.java @@ -7,7 +7,6 @@ import org.apache.kafka.clients.consumer.ConsumerRecord; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; -import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Component; @@ -25,10 +24,8 @@ public class KafKaPublishConsumer implements ApplicationEventPublisherAware { @Autowired private ITeamMemberGridService teamMemberGridService; - private MongoTemplate mongoTemplate; - public KafKaPublishConsumer(MongoTemplate mongoTemplate) { - this.mongoTemplate = mongoTemplate; + public KafKaPublishConsumer() { } @KafkaListener(topics = {"C0021"}) diff --git a/src/main/java/cn/com/tenlion/usercenter/service/grid/impl/GridDeleteAfterHandlerImpl.java b/src/main/java/cn/com/tenlion/usercenter/service/grid/impl/GridDeleteAfterHandlerImpl.java index 781600f..9ff2f63 100644 --- a/src/main/java/cn/com/tenlion/usercenter/service/grid/impl/GridDeleteAfterHandlerImpl.java +++ b/src/main/java/cn/com/tenlion/usercenter/service/grid/impl/GridDeleteAfterHandlerImpl.java @@ -1,9 +1,10 @@ package cn.com.tenlion.usercenter.service.grid.impl; +import cn.com.tenlion.usercenter.consts.ISmartCityUserCenterConsts; import cn.com.tenlion.usercenter.pojo.dtos.kafka.SyncDataDTO; import com.alibaba.fastjson.JSONObject; import ink.wgink.interfaces.map.IGridDeleteAfterHandler; -import org.apache.commons.lang3.StringUtils; +import ink.wgink.util.string.WStringUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.stereotype.Service; @@ -33,13 +34,12 @@ public class GridDeleteAfterHandlerImpl implements IGridDeleteAfterHandler { if (gridIds.isEmpty()) { return; } - String gridIdString = StringUtils.join(gridIds, "_"); SyncDataDTO syncDataDTO = new SyncDataDTO<>(); - syncDataDTO.setUid(gridIdString); - syncDataDTO.setAction("delete"); + syncDataDTO.setUid(WStringUtil.listToStr(gridIds, "_")); + syncDataDTO.setAction(ISmartCityUserCenterConsts.KAFKA_DATA_DELETE); syncDataDTO.setData(new HashMap<>()); - syncDataDTO.setTableNumber(""); - kafkaTemplate.send("C0021", JSONObject.toJSONString(syncDataDTO)); + syncDataDTO.setTableNumber(ISmartCityUserCenterConsts.KAFKA_TOPIC_GRID); + kafkaTemplate.send(ISmartCityUserCenterConsts.KAFKA_TOPIC_GRID, JSONObject.toJSONString(syncDataDTO)); } } diff --git a/src/main/java/cn/com/tenlion/usercenter/service/grid/impl/GridPointDeleteAfterHandlerImpl.java b/src/main/java/cn/com/tenlion/usercenter/service/grid/impl/GridPointDeleteAfterHandlerImpl.java new file mode 100644 index 0000000..5071944 --- /dev/null +++ b/src/main/java/cn/com/tenlion/usercenter/service/grid/impl/GridPointDeleteAfterHandlerImpl.java @@ -0,0 +1,43 @@ +package cn.com.tenlion.usercenter.service.grid.impl; + +import cn.com.tenlion.usercenter.consts.ISmartCityUserCenterConsts; +import cn.com.tenlion.usercenter.pojo.dtos.kafka.SyncDataDTO; +import ink.wgink.interfaces.map.IGridPointDeleteAfterHandler; +import ink.wgink.module.map.service.grid.IGridPointService; +import ink.wgink.util.string.WStringUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.kafka.core.KafkaTemplate; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @ClassName: GridPointDeleteAfterHandlerImpl + * @Description: 网格点删除 + * @Author: wanggeng + * @Date: 2021/12/23 10:15 PM + * @Version: 1.0 + */ +@Service +public class GridPointDeleteAfterHandlerImpl implements IGridPointDeleteAfterHandler { + @Autowired + private KafkaTemplate kafkaTemplate; + @Autowired + private IGridPointService gridPointService; + + @Override + public void handle(List gridIds) { + if (gridIds.isEmpty()) { + return; + } + SyncDataDTO syncDataDTO = new SyncDataDTO<>(); + syncDataDTO.setKeyId("grid_id"); + syncDataDTO.setKeyValue(WStringUtil.listToStr(gridIds, "_")); + syncDataDTO.setAction(ISmartCityUserCenterConsts.KAFKA_DATA_DELETE); + syncDataDTO.setData(new HashMap()); + syncDataDTO.setTableNumber(ISmartCityUserCenterConsts.KAFKA_TOPIC_GRID_POINT); + + } +} diff --git a/src/main/java/cn/com/tenlion/usercenter/service/grid/impl/GridPointSaveAfterHandlerImpl.java b/src/main/java/cn/com/tenlion/usercenter/service/grid/impl/GridPointSaveAfterHandlerImpl.java new file mode 100644 index 0000000..b602889 --- /dev/null +++ b/src/main/java/cn/com/tenlion/usercenter/service/grid/impl/GridPointSaveAfterHandlerImpl.java @@ -0,0 +1,54 @@ +package cn.com.tenlion.usercenter.service.grid.impl; + +import cn.com.tenlion.usercenter.consts.ISmartCityUserCenterConsts; +import cn.com.tenlion.usercenter.pojo.dtos.kafka.SyncDataDTO; +import com.alibaba.fastjson.JSONObject; +import ink.wgink.interfaces.map.IGridPointSaveAfterHandler; +import ink.wgink.module.map.pojo.dtos.grid.GridPointDTO; +import ink.wgink.module.map.service.grid.IGridPointService; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.kafka.core.KafkaTemplate; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @ClassName: GridPointSaveAfterHandlerImpl + * @Description: 网格点 + * @Author: wanggeng + * @Date: 2021/12/23 10:03 PM + * @Version: 1.0 + */ +@Service +public class GridPointSaveAfterHandlerImpl implements IGridPointSaveAfterHandler { + + @Autowired + private KafkaTemplate kafkaTemplate; + @Autowired + private IGridPointService gridPointService; + + @Override + public void handle(String gridId) { + if (StringUtils.isBlank(gridId)) { + return; + } + List gridPointDTOs = gridPointService.list(gridId); + gridPointDTOs.forEach(gridPointDTO -> { + Map data = new HashMap<>(6); + data.put("lng", gridPointDTO.getLng()); + data.put("lat", gridPointDTO.getLat()); + SyncDataDTO syncDataDTO = new SyncDataDTO<>(); + syncDataDTO.setKeyId("grid_id"); + syncDataDTO.setKeyValue(gridId); + syncDataDTO.setAction(ISmartCityUserCenterConsts.KAFKA_DATA_SAVE); + syncDataDTO.setData(data); + syncDataDTO.setTableNumber(ISmartCityUserCenterConsts.KAFKA_TOPIC_GRID_POINT); + kafkaTemplate.send(ISmartCityUserCenterConsts.KAFKA_TOPIC_GRID_POINT, JSONObject.toJSONString(syncDataDTO)); + }); + } + + +} diff --git a/src/main/java/cn/com/tenlion/usercenter/service/grid/impl/GridSaveAfterHandlerImpl.java b/src/main/java/cn/com/tenlion/usercenter/service/grid/impl/GridSaveAfterHandlerImpl.java index b68d538..629a5f9 100644 --- a/src/main/java/cn/com/tenlion/usercenter/service/grid/impl/GridSaveAfterHandlerImpl.java +++ b/src/main/java/cn/com/tenlion/usercenter/service/grid/impl/GridSaveAfterHandlerImpl.java @@ -1,5 +1,6 @@ package cn.com.tenlion.usercenter.service.grid.impl; +import cn.com.tenlion.usercenter.consts.ISmartCityUserCenterConsts; import cn.com.tenlion.usercenter.pojo.dtos.kafka.SyncDataDTO; import com.alibaba.fastjson.JSONObject; import ink.wgink.interfaces.map.IGridSaveAfterHandler; @@ -37,10 +38,10 @@ public class GridSaveAfterHandlerImpl implements IGridSaveAfterHandler { gridMap.put("areaCode", areaCode); gridMap.put("areaName", areaName); syncDataDTO.setUid(gridId); - syncDataDTO.setAction("save"); + syncDataDTO.setAction(ISmartCityUserCenterConsts.KAFKA_DATA_SAVE); syncDataDTO.setData(gridMap); - syncDataDTO.setTableNumber(""); - kafkaTemplate.send("C0021", JSONObject.toJSONString(syncDataDTO)); + syncDataDTO.setTableNumber(ISmartCityUserCenterConsts.KAFKA_TOPIC_GRID); + kafkaTemplate.send(ISmartCityUserCenterConsts.KAFKA_TOPIC_GRID, JSONObject.toJSONString(syncDataDTO)); } } diff --git a/src/main/java/cn/com/tenlion/usercenter/service/grid/impl/GridUpdateAfterHandlerImpl.java b/src/main/java/cn/com/tenlion/usercenter/service/grid/impl/GridUpdateAfterHandlerImpl.java index 014a945..67bfb52 100644 --- a/src/main/java/cn/com/tenlion/usercenter/service/grid/impl/GridUpdateAfterHandlerImpl.java +++ b/src/main/java/cn/com/tenlion/usercenter/service/grid/impl/GridUpdateAfterHandlerImpl.java @@ -1,5 +1,6 @@ package cn.com.tenlion.usercenter.service.grid.impl; +import cn.com.tenlion.usercenter.consts.ISmartCityUserCenterConsts; import cn.com.tenlion.usercenter.pojo.dtos.kafka.SyncDataDTO; import com.alibaba.fastjson.JSONObject; import ink.wgink.interfaces.map.IGridUpdateAfterHandler; @@ -37,10 +38,10 @@ public class GridUpdateAfterHandlerImpl implements IGridUpdateAfterHandler { gridMap.put("gridName", gridName); gridMap.put("fillColor", fillColor); syncDataDTO.setUid(gridId); - syncDataDTO.setAction("update"); + syncDataDTO.setAction(ISmartCityUserCenterConsts.KAFKA_DATA_UPDATE); syncDataDTO.setData(gridMap); - syncDataDTO.setTableNumber(""); - kafkaTemplate.send("C0021", JSONObject.toJSONString(syncDataDTO)); + syncDataDTO.setTableNumber(ISmartCityUserCenterConsts.KAFKA_TOPIC_GRID); + kafkaTemplate.send(ISmartCityUserCenterConsts.KAFKA_TOPIC_GRID, JSONObject.toJSONString(syncDataDTO)); } }