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,