美文网首页
spring的任务调度和事件监听

spring的任务调度和事件监听

作者: 青苹果和梨 | 来源:发表于2017-03-20 21:46 被阅读0次

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的任务调度和事件监听

      本文链接:https://www.haomeiwen.com/subject/tbwinttx.html