spring的任务调度和事件监听

2017-03-20  本文已影响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,

上一篇 下一篇

猜你喜欢

热点阅读