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


    一、编写事件

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

    1. public class PeriodicityRuleChangeEvent extends ApplicationEvent {
    2. private final JwpDeployWorkOrderRuleDTO jwpDeployWorkOrderRuleDTO;
    3. public PeriodicityRuleChangeEvent(Object source, JwpDeployWorkOrderRuleDTO jwpDeployWorkOrderRuleDTO) {
    4. super(source);
    5. this.jwpDeployWorkOrderRuleDTO = jwpDeployWorkOrderRuleDTO;
    6. }
    7. public JwpDeployWorkOrderRuleDTO getJwpDeployWorkOrderRuleDTO() {
    8. return jwpDeployWorkOrderRuleDTO;
    9. }
    10. }

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

    1. @Component
    2. public class PeriodicityRuleListener implements ApplicationListener {
    3. @Autowired
    4. private PeriodicityCreateProcessServiceImpl periodicityCreateProcessServiceImpl;
    5. @Override
    6. public void onApplicationEvent(PeriodicityRuleChangeEvent periodicityRuleChangeEvent) {
    7. periodicityCreateProcessServiceImpl.addTask(periodicityRuleChangeEvent.getJwpDeployWorkOrderRuleDTO());
    8. }
    9. }

    三、发布事件

    1. @compnent
    2. public class PeriodicityStartProcessService {
    3. @Autowired
    4. private ApplicationEventPublisher publisher;
    5. private void triggerEvent(JwpDeployWorkOrderRuleDTO jwpDeployWorkOrderRuleDTO) {
    6. PeriodicityRuleChangeEvent periodicityRuleChangeEvent = new PeriodicityRuleChangeEvent(this, jwpDeployWorkOrderRuleDTO);
    7. publisher.publishEvent(periodicityRuleChangeEvent);
    8. }
    9. }

  • 相关阅读:
    Thrift 实践1:编译安装篇
    判断文件为文本文件还是二进制文件
    如何知道Android SDK的安装路径
    React常见面试题
    nginx调优参数整理总结
    使用 Python为你的在线会议创建一个假的摄像头
    2018年美亚杯电子数据取证大赛-团体赛
    WebDAV之葫芦儿·派盘+书藏家
    导航【操作系统】
    网络安全合规-DSMM
  • 原文地址:https://blog.csdn.net/weixin_44705744/article/details/133925489