近日項目開發中須要在天天日定時分析一次前一天的日誌信息,藉此機會整理了一下在spring環境下幾種定時任務的實現方式:java
- java.util.Timer
用於管理在後臺執行的延遲任務或週期性任務,能夠按照固定頻率或固定延遲時間執行。
- Quartz
一個輕量級的Java庫,幾乎能夠集成到任何應用系統中的框架。- task
spring3.0自帶的Timer,使用更加簡便。
如下案例schedule()方法內的第一個參數爲TimerTask task
,爲須要執行的任務,第二個參數爲long delay
爲延時啓動時間,第三個參數long period
爲執行週期。程序效果爲2s後開始執行,5s執行一次。spring
new Timer().schedule(new TimerTask() {
@Override
public void run() {
System.out.println("Timer is running");
}
}, 2000,5000);
複製代碼
因爲自帶Timer有如下缺陷,因此使用場景較少:框架
- 能夠讓程序以某個頻率執行,但沒法實如今指定時間運行;
- 執行任務的線程只有一個,當某一任務執行時間過長影響其餘定時任務的準確性;
- 沒有持久化機制;
- Timers沒有真正的管理計劃;
@Component
public class MyJob1 {
public void sayHello() {
System.out.println("hello MyJob1...");
}
}
public class MyWork {
public void sayHello() {
System.out.println("hello MyWork....");
}
}
複製代碼
public class Myjob2 extends QuartzJobBean {
HelloService helloService;
public HelloService getHelloService() {
return helloService;
}
public void setHelloService(HelloService helloService) {
this.helloService = helloService;
}
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
helloService.helloSe();
}
}
複製代碼
@Configuration
public class QuartzConfig {
@Bean
JobDetail methodInvokingJobDetailFactoryBean() {
MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean();
bean.setTargetBeanName("myJob1");//設置目標Bean(就是Job Bean的名字)
bean.setTargetMethod("sayHello");//設置目標方法名
return bean;
}
}
複製代碼
@Configuration
public class QuartzConfig {
@Bean
JobDetail jobDetailFactoryBean() {
JobDetailFactoryBean bean = new JobDetailFactoryBean();
bean.setJobClass(MyJob2.class);
JobDataMap map = new JobDataMap();
map.put("myWork", new MyWork());
bean.setJobDataMap(map);
return bean;
}
}
複製代碼
@Bean
SimpleTriggerFactoryBean simpleTriggerFactoryBean() {
SimpleTriggerFactoryBean bean = new SimpleTriggerFactoryBean();
bean.setStartTime(new Date());
bean.setRepeatCount(3);
bean.setRepeatInterval(2000);
bean.setJobDetail(methodInvokingJobDetailFactoryBean().getObject());
return bean;
}
複製代碼
@Bean
CronTrigger cronTriggerFactoryBean() {
CronTriggerFactoryBean bean = new CronTriggerFactoryBean();
bean.setCronExpression("0/5 * * * * ?");
bean.setJobDetail(jobDetailFactoryBean().getObject());
return bean;
}
複製代碼
@Bean
SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean bean = new SchedulerFactoryBean();
bean.setTriggers(simpleTriggerFactoryBean().getObject(),cronTriggerFactoryBean().getObject());
return bean;
}
複製代碼
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
複製代碼
@Service
public class HelloService {
public String helloSe() {
return "hello service";
}
}
複製代碼
@Component
public class Myschedule {
@Scheduled(fixedDelay = 2000)
public void sch1() {
System.out.println("fixedDelay執行"+new Date());
}
@Scheduled(fixedRate = 2000)
public void sch2() {
System.out.println("fixedRate執行"+new Date());
}
@Scheduled(initialDelay = 2000,fixedDelay = 2000)
public void sch3() {
System.out.println("initialDelay"+new Date());
}
@Scheduled(cron ="" )
public void sch4() {
System.out.println("initialDelay"+new Date());
}
}
複製代碼
參數解釋ide
- cron:指定cron表達式
- fixedDelay:An interval-based trigger where the interval is measured from the completion time of the previous task. The time unit value is measured in milliseconds.即表示從上一個任務完成開始到下一個任務開始的間隔,單位是毫秒。
- fixedRate:An interval-based trigger where the interval is measured from the start time of the previous task. The time unit value is measured in milliseconds.即從上一個任務開始到下一個任務開始的間隔,單位是毫秒。