利用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);

    }

}
相关推荐
今天的砖头有点烫手啊4 分钟前
Spring Boot
java·人工智能
Zzj_tju1 小时前
Tool-Using LLM 论文精读路线:从 ReAct 到可验证工具调用
前端·react.js·前端框架
To_OC8 小时前
别再瞎写 React Router!7 个高频踩坑点一次性讲透
前端·javascript·react.js
BUG研究员_9 小时前
Runnable与LCEL
开发语言·人工智能·python
岭南灯火10 小时前
前端通用交互式几何编辑器的开发经验
前端·设计模式·架构
岭南灯火10 小时前
前端通用交互式几何编辑器的设计法则 7 - 设计模式与原则
前端·设计模式·架构
岭南灯火10 小时前
前端通用交互式几何编辑器的设计法则 4 - 绘制、悬停与编辑
前端·设计模式·架构
岭南灯火10 小时前
前端通用交互式几何编辑器的设计法则 3 - 数据对象的设计
前端·javascript·架构
Jackson__10 小时前
从 LLM 到 Agent:一篇文章搞懂 AI 圈热词!
前端·agent·ai编程
码农颜10 小时前
5.4.1 锁分类
java·数据库·mysql