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

    }

}
相关推荐
CodeAmaz几秒前
Spring编程式事务详解
java·数据库·spring
没有bug.的程序员2 分钟前
微服务基础设施清单:必须、应该、可以、无需的四级分类指南
java·jvm·微服务·云原生·容器·架构
武子康5 分钟前
Java-204 RabbitMQ Connection/Channel 工作流程:AMQP 发布消费、抓包帧结构与常见坑
java·分布式·消息队列·rabbitmq·ruby·java-activemq
郑州光合科技余经理6 分钟前
海外国际版同城服务系统开发:PHP技术栈
java·大数据·开发语言·前端·人工智能·架构·php
Yorelee.8 分钟前
ms-swift在训练时遇到的部分问题及解决方案
开发语言·nlp·transformer·swift
行走的bug...11 分钟前
python项目管理
开发语言·python
appearappear17 分钟前
Mac 上重新安装了Cursor 2.2.30,重新配置 springboot 过程记录
java·spring boot·后端
一行注释19 分钟前
前端数据加密:保护用户数据的第一道防线
前端
纪伊路上盛名在24 分钟前
记1次BioPython Entrez模块Elink的debug
前端·数据库·python·debug·工具开发
CryptoRzz25 分钟前
日本股票 API 对接实战指南(实时行情与 IPO 专题)
java·开发语言·python·区块链·maven