利用Spring Boot框架做事件发布和监听

一、编写事件

1.编写事件类并集成spring boot 事件接口,提供访问事件参数属性

java 复制代码
public class PeriodicityRuleChangeEvent extends ApplicationEvent {

    private final JwpDeployWorkOrderRuleDTO jwpDeployWorkOrderRuleDTO;

    public PeriodicityRuleChangeEvent(Object source, JwpDeployWorkOrderRuleDTO jwpDeployWorkOrderRuleDTO) {
        super(source);
        this.jwpDeployWorkOrderRuleDTO = jwpDeployWorkOrderRuleDTO;
    }

    public JwpDeployWorkOrderRuleDTO getJwpDeployWorkOrderRuleDTO() {
        return jwpDeployWorkOrderRuleDTO;
    }

}

二、编写监听类(必须写明监听事件类型,重写监听到事件后,处理方法)

java 复制代码
@Component
public class PeriodicityRuleListener implements ApplicationListener<PeriodicityRuleChangeEvent> {

    @Autowired
    private PeriodicityCreateProcessServiceImpl periodicityCreateProcessServiceImpl;

    @Override
    public void onApplicationEvent(PeriodicityRuleChangeEvent periodicityRuleChangeEvent) {
        periodicityCreateProcessServiceImpl.addTask(periodicityRuleChangeEvent.getJwpDeployWorkOrderRuleDTO());
    }


}

三、发布事件

java 复制代码
@compnent
public class PeriodicityStartProcessService {

    @Autowired
    private ApplicationEventPublisher publisher;

    private void triggerEvent(JwpDeployWorkOrderRuleDTO jwpDeployWorkOrderRuleDTO) {
        PeriodicityRuleChangeEvent periodicityRuleChangeEvent = new PeriodicityRuleChangeEvent(this, jwpDeployWorkOrderRuleDTO);
        publisher.publishEvent(periodicityRuleChangeEvent);

    }

}
相关推荐
沐知全栈开发2 分钟前
HTML DOM 修改
开发语言
lcc1873 分钟前
Vue mixin混入
前端·vue.js
t***L2663 分钟前
终于搞定了!Vue项目打包后白屏问题
前端·javascript·vue.js
u***j3244 分钟前
前端组件通信方式,Vue与React对比
前端·vue.js·react.js
小贺要学前端6 分钟前
【无标题】
前端·javascript·vue·技术趋势
im_AMBER6 分钟前
React 18 用 State 响应输入
前端·react.js·前端框架
Tony_yitao7 分钟前
9.华为OD机试真题 - 最长的顺子 - 2024E卷 Java
java·华为od·algorithm
前端摸鱼匠8 分钟前
Vue 3 的全局组件注册:讲解如何全局注册组件
前端·javascript·vue.js·前端框架·node.js·ecmascript
2501_941236211 小时前
C++与Node.js集成
开发语言·c++·算法
毕设源码-赖学姐1 小时前
【开题答辩全过程】以 非凡物流公司电商物流管理系统的设计与实现为例,包含答辩的问题和答案
java·eclipse