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<List<BroadCastMessageGroup>> 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<List<BroadCastMessageConfirmed>> 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<BroadCastMessageDTO> {
@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<BroadCastMessageDTO> handlerChainContext, BroadCastMessageDTO dto) {
Integer msgId = dto.getId();
String applicationCode = requestContext.getCurrentUser().getApplication().name();
Map<String, List<String>> roleActionMap = dto.getRoleActionMap();
List<String> addDisplayRoleList = roleActionMap.get(RoleActions.ADD.name());
List<String> deleteDisplayRoleList = roleActionMap.get(RoleActions.DELETE.name());
List<BroadCastMessageGroup> changeList = new ArrayList<>();
if (CollectionUtils.isNotEmpty(addDisplayRoleList)) {
List<String> addRoleIds = addDisplayRoleList.stream().map(DisplayUserType::getRoleIdByRoleName).collect(Collectors.toList());
List<BroadCastMessageGroup> 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<String> deleteRoleIds = deleteDisplayRoleList.stream().map(DisplayUserType::getRoleIdByRoleName).collect(Collectors.toList());
List<BroadCastMessageGroup> 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;
}
}