From 8a7291fd51f983631319f62036f10706abef7407 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=AF=E5=BA=86?= Date: Tue, 7 Jul 2020 19:23:57 +0800 Subject: [PATCH 1/2] fix(manager): resolve NPE when deleting profile by improving notify service logic --- .../dc3/center/manager/api/ProfileApi.java | 7 +++-- .../center/manager/service/NotifyService.java | 14 +++++++-- .../service/impl/NotifyServiceImpl.java | 30 ++++++++----------- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/api/ProfileApi.java b/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/api/ProfileApi.java index dde1a6bb6..e3d5c09be 100644 --- a/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/api/ProfileApi.java +++ b/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/api/ProfileApi.java @@ -24,6 +24,7 @@ import com.dc3.common.bean.R; import com.dc3.common.constant.Common; import com.dc3.common.constant.Operation; import com.dc3.common.dto.ProfileDto; +import com.dc3.common.model.Driver; import com.dc3.common.model.Profile; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.RequestMapping; @@ -51,7 +52,8 @@ public class ProfileApi implements ProfileClient { try { Profile add = profileService.add(profile); if (null != add) { - notifyService.notifyDriverProfile(profile.getId(), Operation.Profile.ADD); + Driver driver = notifyService.getDriverByProfileId(profile.getId()); + notifyService.notifyDriverProfile(driver, Operation.Profile.ADD); return R.ok(add); } } catch (Exception e) { @@ -63,8 +65,9 @@ public class ProfileApi implements ProfileClient { @Override public R delete(Long id) { try { + Driver driver = notifyService.getDriverByProfileId(id); if (profileService.delete(id)) { - notifyService.notifyDriverProfile(id, Operation.Profile.DELETE); + notifyService.notifyDriverProfile(driver, Operation.Profile.DELETE); return R.ok(); } } catch (Exception e) { diff --git a/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/NotifyService.java b/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/NotifyService.java index 1efe0355f..c0d8c5088 100644 --- a/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/NotifyService.java +++ b/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/NotifyService.java @@ -16,6 +16,8 @@ package com.dc3.center.manager.service; +import com.dc3.common.model.Driver; + /** *

Notify Interface * @@ -26,10 +28,10 @@ public interface NotifyService { /** * 通知驱动 新增模板(ADD) / 删除模板(DELETE) * - * @param profileId Profile Id + * @param driver driver * @param operationType Operation Type */ - void notifyDriverProfile(Long profileId, String operationType); + void notifyDriverProfile(Driver driver, String operationType); /** * 通知驱动 新增设备(ADD) / 删除设备(DELETE) / 修改设备(UPDATE) @@ -69,4 +71,12 @@ public interface NotifyService { */ void notifyDriverPointInfo(Long pointInfoId, Long attributeId, Long deviceId, String operationType); + /** + * get driver by profile id + * + * @param profileId Profile Id + * @return Driver + */ + Driver getDriverByProfileId(Long profileId); + } diff --git a/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/impl/NotifyServiceImpl.java b/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/impl/NotifyServiceImpl.java index 87ef095a5..4503b22c5 100644 --- a/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/impl/NotifyServiceImpl.java +++ b/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/impl/NotifyServiceImpl.java @@ -50,9 +50,8 @@ public class NotifyServiceImpl implements NotifyService { private RabbitTemplate rabbitTemplate; @Override - public void notifyDriverProfile(Long profileId, String operationType) { - Driver driver = getDriverByProfileId(profileId); - DriverOperation operation = new DriverOperation().setCommand(operationType).setId(profileId); + public void notifyDriverProfile(Driver driver, String operationType) { + DriverOperation operation = new DriverOperation().setCommand(operationType).setId(driver.getId()); notifyDriver(driver, operation); } @@ -85,6 +84,16 @@ public class NotifyServiceImpl implements NotifyService { notifyDriver(driver, operation); } + @Override + public Driver getDriverByProfileId(Long profileId) { + Profile profile = profileService.selectById(profileId); + if (null != profile) { + return driverService.selectById(profile.getDriverId()); + } + return null; + } + + /** * notify driver * @@ -96,7 +105,6 @@ public class NotifyServiceImpl implements NotifyService { rabbitTemplate.convertAndSend(Common.Rabbit.TOPIC_EXCHANGE_NOTIFY, Common.Rabbit.ROUTING_KEY_PREFIX + driver.getServiceName(), operation); } - /** * get driver by device id * @@ -113,18 +121,4 @@ public class NotifyServiceImpl implements NotifyService { } return null; } - - /** - * get driver by profile id - * - * @param profileId Profile Id - * @return Driver - */ - private Driver getDriverByProfileId(Long profileId) { - Profile profile = profileService.selectById(profileId); - if (null != profile) { - return driverService.selectById(profile.getDriverId()); - } - return null; - } } From 6d0a6ff8da59fffc89dae42ff96ebf05daf1c8e7 Mon Sep 17 00:00:00 2001 From: pnoker Date: Tue, 7 Jul 2020 22:58:32 +0800 Subject: [PATCH 2/2] refactor(manager): add DriverService list method and simplify NotifyServiceImpl --- .../dc3/center/manager/api/ProfileApi.java | 11 +-- .../center/manager/service/DriverService.java | 16 +++++ .../center/manager/service/NotifyService.java | 15 ++-- .../service/impl/BatchServiceImpl.java | 3 +- .../service/impl/DriverServiceImpl.java | 27 +++++++ .../service/impl/NotifyServiceImpl.java | 72 +++++++------------ 6 files changed, 79 insertions(+), 65 deletions(-) diff --git a/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/api/ProfileApi.java b/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/api/ProfileApi.java index e3d5c09be..46fdecce5 100644 --- a/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/api/ProfileApi.java +++ b/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/api/ProfileApi.java @@ -18,6 +18,7 @@ package com.dc3.center.manager.api; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.dc3.api.center.manager.feign.ProfileClient; +import com.dc3.center.manager.service.DriverService; import com.dc3.center.manager.service.NotifyService; import com.dc3.center.manager.service.ProfileService; import com.dc3.common.bean.R; @@ -42,6 +43,8 @@ import javax.annotation.Resource; @RequestMapping(Common.Service.DC3_MANAGER_PROFILE_URL_PREFIX) public class ProfileApi implements ProfileClient { + @Resource + private DriverService driverService; @Resource private ProfileService profileService; @Resource @@ -52,8 +55,8 @@ public class ProfileApi implements ProfileClient { try { Profile add = profileService.add(profile); if (null != add) { - Driver driver = notifyService.getDriverByProfileId(profile.getId()); - notifyService.notifyDriverProfile(driver, Operation.Profile.ADD); + Driver driver = driverService.selectByProfileId(profile.getId()); + notifyService.notifyDriverProfile(driver, profile.getId(), Operation.Profile.ADD); return R.ok(add); } } catch (Exception e) { @@ -65,9 +68,9 @@ public class ProfileApi implements ProfileClient { @Override public R delete(Long id) { try { - Driver driver = notifyService.getDriverByProfileId(id); + Driver driver = driverService.selectByProfileId(id); if (profileService.delete(id)) { - notifyService.notifyDriverProfile(driver, Operation.Profile.DELETE); + notifyService.notifyDriverProfile(driver, id, Operation.Profile.DELETE); return R.ok(); } } catch (Exception e) { diff --git a/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/DriverService.java b/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/DriverService.java index badedf116..673e05531 100644 --- a/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/DriverService.java +++ b/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/DriverService.java @@ -45,6 +45,22 @@ public interface DriverService extends Service { */ Driver selectByHostPort(String host, Integer port); + /** + * 根据 DEVICE ID 查询 + * + * @param deviceId Device Id + * @return Driver + */ + Driver selectByDeviceId(Long deviceId); + + /** + * 根据 PROFILE ID 查询 + * + * @param profileId Profile Id + * @return Driver + */ + Driver selectByProfileId(Long profileId); + /** * 查询 Driver 服务状态 * diff --git a/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/NotifyService.java b/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/NotifyService.java index c0d8c5088..19edf75f9 100644 --- a/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/NotifyService.java +++ b/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/NotifyService.java @@ -28,10 +28,11 @@ public interface NotifyService { /** * 通知驱动 新增模板(ADD) / 删除模板(DELETE) * - * @param driver driver + * @param driver driver + * @param profileId Profile Id * @param operationType Operation Type */ - void notifyDriverProfile(Driver driver, String operationType); + void notifyDriverProfile(Driver driver, Long profileId, String operationType); /** * 通知驱动 新增设备(ADD) / 删除设备(DELETE) / 修改设备(UPDATE) @@ -45,7 +46,7 @@ public interface NotifyService { /** * 通知驱动 新增位号(ADD) / 删除位号(DELETE) / 修改位号(UPDATE) * - * @param pointId + * @param pointId Point Id * @param profileId Profile Id * @param operationType Operation Type */ @@ -71,12 +72,4 @@ public interface NotifyService { */ void notifyDriverPointInfo(Long pointInfoId, Long attributeId, Long deviceId, String operationType); - /** - * get driver by profile id - * - * @param profileId Profile Id - * @return Driver - */ - Driver getDriverByProfileId(Long profileId); - } diff --git a/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/impl/BatchServiceImpl.java b/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/impl/BatchServiceImpl.java index be69c75e7..16dbba681 100644 --- a/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/impl/BatchServiceImpl.java +++ b/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/impl/BatchServiceImpl.java @@ -33,7 +33,6 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; -import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; @@ -135,7 +134,7 @@ public class BatchServiceImpl implements BatchService { throw new ServiceException("Add profile failed: " + batchProfile.getName()); } - notifyService.notifyDriverProfile(profile.getId(), Operation.Profile.ADD); + notifyService.notifyDriverProfile(driver, profile.getId(), Operation.Profile.ADD); ThreadUtil.sleep(1, TimeUnit.SECONDS); } return profile; diff --git a/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/impl/DriverServiceImpl.java b/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/impl/DriverServiceImpl.java index ed7661ff6..244e32a4f 100644 --- a/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/impl/DriverServiceImpl.java +++ b/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/impl/DriverServiceImpl.java @@ -20,6 +20,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.dc3.center.manager.mapper.DriverMapper; +import com.dc3.center.manager.service.DeviceService; import com.dc3.center.manager.service.DriverService; import com.dc3.center.manager.service.ProfileService; import com.dc3.common.bean.Pages; @@ -27,6 +28,7 @@ import com.dc3.common.constant.Common; import com.dc3.common.dto.DriverDto; import com.dc3.common.dto.ProfileDto; import com.dc3.common.exception.ServiceException; +import com.dc3.common.model.Device; import com.dc3.common.model.Driver; import com.dc3.common.model.Profile; import com.netflix.discovery.EurekaClient; @@ -54,6 +56,10 @@ public class DriverServiceImpl implements DriverService { @Resource private EurekaClient eurekaClient; @Resource + private DriverService driverService; + @Resource + private DeviceService deviceService; + @Resource private ProfileService profileService; @Resource private DriverMapper driverMapper; @@ -155,6 +161,27 @@ public class DriverServiceImpl implements DriverService { return driverMapper.selectOne(queryWrapper); } + @Override + public Driver selectByDeviceId(Long deviceId) { + Device device = deviceService.selectById(deviceId); + if (null != device) { + Profile profile = profileService.selectById(device.getProfileId()); + if (null != profile) { + return driverService.selectById(profile.getDriverId()); + } + } + return null; + } + + @Override + public Driver selectByProfileId(Long profileId) { + Profile profile = profileService.selectById(profileId); + if (null != profile) { + return driverService.selectById(profile.getDriverId()); + } + return null; + } + @Override public Map driverStatus(DriverDto driverDto) { Map driverStatusMap = new HashMap<>(16); diff --git a/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/impl/NotifyServiceImpl.java b/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/impl/NotifyServiceImpl.java index 4503b22c5..7d8612fe0 100644 --- a/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/impl/NotifyServiceImpl.java +++ b/dc3-center/dc3-manager/src/main/java/com/dc3/center/manager/service/impl/NotifyServiceImpl.java @@ -16,15 +16,11 @@ package com.dc3.center.manager.service.impl; -import com.dc3.center.manager.service.DeviceService; import com.dc3.center.manager.service.DriverService; import com.dc3.center.manager.service.NotifyService; -import com.dc3.center.manager.service.ProfileService; import com.dc3.common.bean.driver.DriverOperation; import com.dc3.common.constant.Common; -import com.dc3.common.model.Device; import com.dc3.common.model.Driver; -import com.dc3.common.model.Profile; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.stereotype.Service; @@ -43,57 +39,53 @@ public class NotifyServiceImpl implements NotifyService { @Resource private DriverService driverService; @Resource - private DeviceService deviceService; - @Resource - private ProfileService profileService; - @Resource private RabbitTemplate rabbitTemplate; @Override - public void notifyDriverProfile(Driver driver, String operationType) { - DriverOperation operation = new DriverOperation().setCommand(operationType).setId(driver.getId()); - notifyDriver(driver, operation); + public void notifyDriverProfile(Driver driver, Long profileId, String operationType) { + if (null != driver) { + DriverOperation operation = new DriverOperation().setCommand(operationType).setId(profileId); + notifyDriver(driver, operation); + } } @Override public void notifyDriverDevice(Long deviceId, Long profileId, String operationType) { - Driver driver = getDriverByProfileId(profileId); - DriverOperation operation = new DriverOperation().setCommand(operationType).setId(deviceId); - notifyDriver(driver, operation); + Driver driver = driverService.selectByProfileId(profileId); + if (null != driver) { + DriverOperation operation = new DriverOperation().setCommand(operationType).setId(deviceId); + notifyDriver(driver, operation); + } } @Override public void notifyDriverPoint(Long pointId, Long profileId, String operationType) { - Driver driver = getDriverByProfileId(profileId); - DriverOperation operation = new DriverOperation().setCommand(operationType).setId(pointId).setParentId(profileId); - notifyDriver(driver, operation); + Driver driver = driverService.selectByProfileId(profileId); + if (null != driver) { + DriverOperation operation = new DriverOperation().setCommand(operationType).setId(pointId).setParentId(profileId); + notifyDriver(driver, operation); + } } @Override public void notifyDriverDriverInfo(Long driverInfoId, Long attributeId, Long profileId, String operationType) { - Driver driver = getDriverByProfileId(profileId); - DriverOperation operation = new DriverOperation().setCommand(operationType).setId(driverInfoId).setParentId(profileId).setAttributeId(attributeId); - notifyDriver(driver, operation); + Driver driver = driverService.selectByProfileId(profileId); + if (null != driver) { + DriverOperation operation = new DriverOperation().setCommand(operationType).setId(driverInfoId).setParentId(profileId).setAttributeId(attributeId); + notifyDriver(driver, operation); + } } @Override public void notifyDriverPointInfo(Long pointInfoId, Long attributeId, Long deviceId, String operationType) { - Driver driver = getDriverByDeviceId(deviceId); - DriverOperation operation = new DriverOperation().setCommand(operationType).setId(pointInfoId).setParentId(deviceId).setAttributeId(attributeId); - notifyDriver(driver, operation); - } - - @Override - public Driver getDriverByProfileId(Long profileId) { - Profile profile = profileService.selectById(profileId); - if (null != profile) { - return driverService.selectById(profile.getDriverId()); + Driver driver = driverService.selectByDeviceId(deviceId); + if (null != driver) { + DriverOperation operation = new DriverOperation().setCommand(operationType).setId(pointInfoId).setParentId(deviceId).setAttributeId(attributeId); + notifyDriver(driver, operation); } - return null; } - /** * notify driver * @@ -105,20 +97,4 @@ public class NotifyServiceImpl implements NotifyService { rabbitTemplate.convertAndSend(Common.Rabbit.TOPIC_EXCHANGE_NOTIFY, Common.Rabbit.ROUTING_KEY_PREFIX + driver.getServiceName(), operation); } - /** - * get driver by device id - * - * @param deviceId Device Id - * @return Driver - */ - private Driver getDriverByDeviceId(Long deviceId) { - Device device = deviceService.selectById(deviceId); - if (null != device) { - Profile profile = profileService.selectById(device.getProfileId()); - if (null != profile) { - return driverService.selectById(profile.getDriverId()); - } - } - return null; - } }