当前位置:首页 > 编程笔记 > 正文
已解决

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

来自网友在路上 159859提问 提问时间:2023-10-19 16:31:51阅读次数: 59

最佳答案 问答题库598位专家为你答疑解惑

一、编写事件

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

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;}}

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

@Component
public class PeriodicityRuleListener implements ApplicationListener<PeriodicityRuleChangeEvent> {@Autowiredprivate PeriodicityCreateProcessServiceImpl periodicityCreateProcessServiceImpl;@Overridepublic void onApplicationEvent(PeriodicityRuleChangeEvent periodicityRuleChangeEvent) {periodicityCreateProcessServiceImpl.addTask(periodicityRuleChangeEvent.getJwpDeployWorkOrderRuleDTO());}}

三、发布事件

@compnent
public class PeriodicityStartProcessService {@Autowiredprivate ApplicationEventPublisher publisher;private void triggerEvent(JwpDeployWorkOrderRuleDTO jwpDeployWorkOrderRuleDTO) {PeriodicityRuleChangeEvent periodicityRuleChangeEvent = new PeriodicityRuleChangeEvent(this, jwpDeployWorkOrderRuleDTO);publisher.publishEvent(periodicityRuleChangeEvent);}}

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"利用Spring Boot框架做事件发布和监听":http://eshow365.cn/6-19741-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!