• ApplicationEvent


     Listener:

    1. @Component
    2. public class BroadCastEventListener implements ApplicationContextAware {
    3. private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
    4. private ApplicationContext applicationContext;
    5. @EventListener
    6. public void onBroadcastEventListener(BroadcastMessageGroupEvent> event) {
    7. LOGGER.info("received broadcastMessageGroupEvent...");
    8. try{
    9. applicationContext.getBean(BroadCastMessageGroupRepository.class).saveAll(event.getEvent());
    10. }catch (Exception e){
    11. LOGGER.error("fail to complete broadcastMessageGroupEvent");
    12. }
    13. LOGGER.info("broadcastMessageGroupEvent is completed");
    14. }
    15. @EventListener
    16. public void onBroadcastEventListenerToChangeStatus(BroadcastConfirmChangeEvent> event) {
    17. LOGGER.info("received broadcastConfirmChangeEvent...");
    18. try{
    19. applicationContext.getBean(BroadCastMessageConfirmedRepository.class).saveAll(event.getEvent());
    20. }catch (Exception e){
    21. LOGGER.error("fail to complete broadcastMessageGroupEvent");
    22. }
    23. LOGGER.info("broadcastConfirmChangeEvent is completed");
    24. }
    25. @Override
    26. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    27. this.applicationContext = applicationContext;
    28. }
    29. }

    Publisher:

    1. @Component
    2. public class BroadcastEventPublisher implements ApplicationEventPublisherAware {
    3. private ApplicationEventPublisher applicationEventPublisher;
    4. @Override
    5. public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
    6. this.applicationEventPublisher = applicationEventPublisher;
    7. }
    8. /**
    9. * use specified threadPool
    10. *
    11. * @param event
    12. */
    13. @Async("taskExecutor")
    14. public void asyncPublish(ApplicationEvent event) {
    15. applicationEventPublisher.publishEvent(event);
    16. }
    17. public void syncPublish(ApplicationEvent event){
    18. applicationEventPublisher.publishEvent(event);
    19. }
    20. }

    Service publish event: 

    1. @Component
    2. public class MessageGroupSaveHandler implements BroadcastHandler {
    3. @Autowired
    4. private ApplicationContext applicationContext;
    5. @Autowired
    6. private BroadCastMessageGroupRepository broadCastMessageGroupRepository;
    7. @Autowired
    8. private BroadCastMessageRepository broadCastMessageRepository;
    9. @Autowired
    10. private CurrentRequestContext requestContext;
    11. @Autowired
    12. private BroadcastEventPublisher eventPublisher;
    13. @Override
    14. public BroadCastMessageDTO doHandle(HandlerChain handlerChainContext, BroadCastMessageDTO dto) {
    15. Integer msgId = dto.getId();
    16. String applicationCode = requestContext.getCurrentUser().getApplication().name();
    17. Map> roleActionMap = dto.getRoleActionMap();
    18. List addDisplayRoleList = roleActionMap.get(RoleActions.ADD.name());
    19. List deleteDisplayRoleList = roleActionMap.get(RoleActions.DELETE.name());
    20. List changeList = new ArrayList<>();
    21. if (CollectionUtils.isNotEmpty(addDisplayRoleList)) {
    22. List addRoleIds = addDisplayRoleList.stream().map(DisplayUserType::getRoleIdByRoleName).collect(Collectors.toList());
    23. List addMessageGroups
    24. = broadCastMessageGroupRepository.queryAllGroupByMessageIdAndRoles(applicationCode, msgId, addRoleIds);
    25. if (CollectionUtils.isNotEmpty(addMessageGroups)) {
    26. throw new BroadcastException(GlobalConstants.MESSAGE_ROLE_ALREADY_ADD);
    27. }
    28. for (String roleId : addRoleIds) {
    29. BroadCastMessageGroup messageGroup = BroadCastMessageGroup
    30. .builder()
    31. .applicationCode(applicationCode)
    32. .message(dto.getBroadCastMessage())
    33. .activeInd(GlobalConstants.ACTIVE)
    34. .groupRole(roleId)
    35. .build();
    36. changeList.add(messageGroup);
    37. }
    38. }
    39. if (CollectionUtils.isNotEmpty(deleteDisplayRoleList)) {
    40. List deleteRoleIds = deleteDisplayRoleList.stream().map(DisplayUserType::getRoleIdByRoleName).collect(Collectors.toList());
    41. List deleteMessageGroups
    42. = broadCastMessageGroupRepository.queryAllGroupByMessageIdAndRoles(applicationCode, msgId, deleteRoleIds);
    43. if (CollectionUtils.isEmpty(deleteMessageGroups)) {
    44. throw new BroadcastException(GlobalConstants.MESSAGE_ROLE_ALREADY_DELETE);
    45. }
    46. deleteMessageGroups.forEach(deleteMessageGroup -> deleteMessageGroup.setActiveInd(GlobalConstants.IN_ACTIVE));
    47. changeList.addAll(deleteMessageGroups);
    48. }
    49. // publish event
    50. if (CollectionUtils.isNotEmpty(changeList)) {
    51. eventPublisher.syncPublish(new BroadcastMessageGroupEvent<>(changeList));
    52. }
    53. return dto;
    54. }
    55. }

  • 相关阅读:
    原三高搜索条件-多选问题
    Java9-17新特性一览,了解少于3个你可能脱节了
    错误:npm ERR! code CERT_HAS_EXPIRED 解决
    H110主板搭配魔改QNCW升级小记
    如何设置任务管理器的任务开机自启
    CAN 协议常见面试题总结
    UnityShader(六)透明效果
    学会提问,ChatGPT可以帮你写出高质量论文
    洛谷P3521 ROT-Tree Rotations
    Java模拟电影院购票系统
  • 原文地址:https://blog.csdn.net/a15512138486/article/details/126869141