spring的任务调度和事件监听

1:spring的任务调度配置

@Service 
public class timeTasks1{ 
    public void execute() { 
          Thread t = new Thread(new Runnable(){  
              public void run(){  
              System.out.println(“执行任务”);  
            }});  
        t.start(); 
    } 
} 
 <context:component-scan base-package="com.test"/>
<task:scheduled-tasks>
  <task:scheduled ref="timeTasks1" method="execute" initial-delay="5000" fixed-delay="3600000"/>
  <task:scheduled ref="timeTasks2" method="exchange" cron="0 59 23 * * ?" />
</task:scheduled-tasks>

第一个任务表示程序启动5s后调用timeTasks1类中的execute方法,然后每隔一个小时再调用execute一次
第三个任务表示每天的23点59分调用timeTasks2类中的exchange方法
更加全面的任务调度框架可以选择quartz

2.spring 的事件监听:

事件类 extends org.springframework.context.ApplicationEvent;
监听类 implements org.springframework.context.ApplicationListener<ContextRefreshedEvent>
@EventListener事件监听,定义事件监听的Bean,

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,087评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,989评论 6 342
  • Idea SpringMVC+Spring+MyBatis+Maven整合 创建项目 File-New Proje...
    mingli_jianshu1阅读 1,683评论 2 15
  • spring官方文档:http://docs.spring.io/spring/docs/current/spri...
    牛马风情阅读 1,754评论 0 3
  • 马上春节又要到来了,今年已经是我人生的第二十二个春节,每年的春节都会有不一样的地方但也有千篇一律的地方。 关于春节...
    鱼小坤阅读 347评论 0 1