ApplicationEvent实现业务解耦

这个还是2017年索道项目里面应用到技术,hession通信,因为就我自己一个人开发,不想搞个rabbitmq之类的消息队列,来实现发布-订阅,解耦业务。这里记录一下。
1 定义事件
这里定义了一个日终处理事件

package com.dzmsoft.monitor.api.event;

import org.springframework.context.ApplicationEvent;

/**
 * 日终处理事件
 * @author dzm
 */
public class EndProcessEvent extends ApplicationEvent{
    
    private static final long serialVersionUID = 5594301256105426255L;
    
    private  Boolean flag;
    
    public EndProcessEvent(Object source) {
        super(source);
    }

    public EndProcessEvent(Object source, Boolean flag) {
        super(source);
        this.flag = flag;
    }

    public Boolean getFlag() {
        return flag;
    }

}

2 触发事件

import org.springframework.context.ApplicationEventPublisher;

@Transactional(readOnly = false)
    @Override
    public FontalEndProcess deal() {
        Date optDate = fontalConfigService.getOptDate();
        FontalEndProcess fontalEndProcess = selectByOptDate(optDate);
        boolean flag = true;
        if (fontalEndProcess == null){
            fontalEndProcess = new FontalEndProcess();
            fontalEndProcess.setOptDate(optDate);
           flag = checkSign(fontalEndProcess, optDate);
           if (flag){
               fontalEndProcess.setCurrentStep(DealStepEnum.FINISH.value());
               // 开始向云端上传日志
               EndProcessEvent event = new EndProcessEvent(this, flag);
               applicationEventPublisher.publishEvent(event);
           }
           insertSelective(fontalEndProcess);
        } else {
            switch(fontalEndProcess.getCurrentStep()){
            case 0: case 1:
                flag = checkSign(fontalEndProcess, optDate);
                break;
            case 2:
                flag = checkMaintainlog(fontalEndProcess, optDate);
                break;
            case 3:
                flag = nextOptDate(fontalEndProcess, optDate);
                break;
            case 4:
                flag = genNextMaintain(fontalEndProcess, optDate);
                break;
            case 5:
                break;
            }
            if (flag){
                fontalEndProcess.setCurrentStep(DealStepEnum.FINISH.value());
                // 开始向云端上传日志
                EndProcessEvent event = new EndProcessEvent(this, flag);
                applicationEventPublisher.publishEvent(event);
            }
            updateByPrimaryKeySelective(fontalEndProcess);
        }
        return fontalEndProcess;
    }

3 事件监听
现在springboot的模式应该写法会有变化,第二十七章:SpringBoot使用ApplicationEvent&Listener完成业务解耦,已经4年过了,再回忆之前的技术,发现有些地方已经过时了,这是不是搞技术人员的悲哀呢?你需要像乾卦一样自强不息,努力学习保持进步,但是如果只是应用的话,太容易被淘汰了。

import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Async
@Component
public class EndProcessListener implements ApplicationListener<EndProcessEvent>{

    @Override
    public void onApplicationEvent(EndProcessEvent event) {
        if (event.getFlag()){
            // 上传运行日志
            SyncFontalRunLogJobServiceImpl syncFontalRunLogJobServiceImpl= (SyncFontalRunLogJobServiceImpl)SpringContextHolder.getBean("syncFontalRunLogJobServiceImpl");
            syncFontalRunLogJobServiceImpl.sync();
            // 上传检查项目
            SyncFontalRunLogItemsJobServiceImpl syncFontalRunLogItemsJobServiceImpl= (SyncFontalRunLogItemsJobServiceImpl)SpringContextHolder.getBean("syncFontalRunLogItemsJobServiceImpl");
            syncFontalRunLogItemsJobServiceImpl.sync();
            // 上传抱索力
            SyncFontalGribForceJobServiceImpl syncFontalGribForceJobServiceImpl= (SyncFontalGribForceJobServiceImpl)SpringContextHolder.getBean("syncFontalGribForceJobServiceImpl");
            syncFontalGribForceJobServiceImpl.sync();
            // 上传维修日志
            SyncFontalMaintainLogJobServiceImpl syncFontalMaintainLogJobServiceImpl= (SyncFontalMaintainLogJobServiceImpl)SpringContextHolder.getBean("syncFontalMaintainLogJobServiceImpl");
            syncFontalMaintainLogJobServiceImpl.sync();
            // 上传停车记录
            SyncFontalStopCarJobServiceImpl syncFontalStopCarJobServiceImpl= (SyncFontalStopCarJobServiceImpl)SpringContextHolder.getBean("syncFontalStopCarJobServiceImpl");
            syncFontalStopCarJobServiceImpl.sync();
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

warrah

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值