Listener:
- @Component
- public class BroadCastEventListener implements ApplicationContextAware {
- private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
-
- private ApplicationContext applicationContext;
-
- @EventListener
- public void onBroadcastEventListener(BroadcastMessageGroupEvent
> event)
{ - LOGGER.info("received broadcastMessageGroupEvent...");
- try{
- applicationContext.getBean(BroadCastMessageGroupRepository.class).saveAll(event.getEvent());
- }catch (Exception e){
- LOGGER.error("fail to complete broadcastMessageGroupEvent");
- }
- LOGGER.info("broadcastMessageGroupEvent is completed");
- }
-
- @EventListener
- public void onBroadcastEventListenerToChangeStatus(BroadcastConfirmChangeEvent
> event)
{ - LOGGER.info("received broadcastConfirmChangeEvent...");
- try{
- applicationContext.getBean(BroadCastMessageConfirmedRepository.class).saveAll(event.getEvent());
- }catch (Exception e){
- LOGGER.error("fail to complete broadcastMessageGroupEvent");
- }
- LOGGER.info("broadcastConfirmChangeEvent is completed");
- }
-
- @Override
- public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
- this.applicationContext = applicationContext;
- }
- }
Publisher:
- @Component
- public class BroadcastEventPublisher implements ApplicationEventPublisherAware {
-
-
- private ApplicationEventPublisher applicationEventPublisher;
-
-
- @Override
- public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
- this.applicationEventPublisher = applicationEventPublisher;
- }
-
- /**
- * use specified threadPool
- *
- * @param event
- */
- @Async("taskExecutor")
- public void asyncPublish(ApplicationEvent event) {
- applicationEventPublisher.publishEvent(event);
- }
-
- public void syncPublish(ApplicationEvent event){
- applicationEventPublisher.publishEvent(event);
- }
- }
Service publish event:
- @Component
- public class MessageGroupSaveHandler implements BroadcastHandler
{ -
-
- @Autowired
- private ApplicationContext applicationContext;
-
-
- @Autowired
- private BroadCastMessageGroupRepository broadCastMessageGroupRepository;
-
- @Autowired
- private BroadCastMessageRepository broadCastMessageRepository;
-
- @Autowired
- private CurrentRequestContext requestContext;
-
- @Autowired
- private BroadcastEventPublisher eventPublisher;
-
-
- @Override
- public BroadCastMessageDTO doHandle(HandlerChain
handlerChainContext, BroadCastMessageDTO dto) { - Integer msgId = dto.getId();
- String applicationCode = requestContext.getCurrentUser().getApplication().name();
- Map
> roleActionMap = dto.getRoleActionMap(); - List
addDisplayRoleList = roleActionMap.get(RoleActions.ADD.name()); - List
deleteDisplayRoleList = roleActionMap.get(RoleActions.DELETE.name()); - List
changeList = new ArrayList<>(); - if (CollectionUtils.isNotEmpty(addDisplayRoleList)) {
- List
addRoleIds = addDisplayRoleList.stream().map(DisplayUserType::getRoleIdByRoleName).collect(Collectors.toList()); - List
addMessageGroups - = broadCastMessageGroupRepository.queryAllGroupByMessageIdAndRoles(applicationCode, msgId, addRoleIds);
- if (CollectionUtils.isNotEmpty(addMessageGroups)) {
- throw new BroadcastException(GlobalConstants.MESSAGE_ROLE_ALREADY_ADD);
- }
- for (String roleId : addRoleIds) {
- BroadCastMessageGroup messageGroup = BroadCastMessageGroup
- .builder()
- .applicationCode(applicationCode)
- .message(dto.getBroadCastMessage())
- .activeInd(GlobalConstants.ACTIVE)
- .groupRole(roleId)
- .build();
- changeList.add(messageGroup);
- }
- }
-
- if (CollectionUtils.isNotEmpty(deleteDisplayRoleList)) {
- List
deleteRoleIds = deleteDisplayRoleList.stream().map(DisplayUserType::getRoleIdByRoleName).collect(Collectors.toList()); - List
deleteMessageGroups - = broadCastMessageGroupRepository.queryAllGroupByMessageIdAndRoles(applicationCode, msgId, deleteRoleIds);
- if (CollectionUtils.isEmpty(deleteMessageGroups)) {
- throw new BroadcastException(GlobalConstants.MESSAGE_ROLE_ALREADY_DELETE);
- }
- deleteMessageGroups.forEach(deleteMessageGroup -> deleteMessageGroup.setActiveInd(GlobalConstants.IN_ACTIVE));
- changeList.addAll(deleteMessageGroups);
- }
-
- // publish event
- if (CollectionUtils.isNotEmpty(changeList)) {
- eventPublisher.syncPublish(new BroadcastMessageGroupEvent<>(changeList));
- }
- return dto;
- }
- }