java spring-mvc spring-boot quartz定时器

1.java中定时任务类。java.util.Timer  java.util.TimerTask

package com.jiayun.demo;

import java.util.Timer;
import java.util.TimerTask;

public class TimerDemo {
	
	public static void main(String[] args) {
		new Timer().schedule(new TimerTask() {

			@Override
			public void run() {
				System.out.println("我执行一次了...");
			}
		}, 2000, 2000);
	}

}

Timer对象还有很多其他定时的方法和销毁time对象的方法。

2.spring-mvc中使用定时器。待完善。。。

 

3.spring-boot中使用scheduled定时器

3.1引入依赖


		
			org.springframework.boot
			spring-boot-starter-web
		
		
				   
	    
	        org.springframework
	        spring-context-support
	    

3.2写一个job类  

package com.jiayun.component;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduledJob {
	
	@Scheduled(cron="0/2 * * * * ?")
	public void job(){
		System.out.println("job run...");
	}

}

 

4.quartz的简单实例

 

 

5.spring-boot整合quartz

5.1引入依赖



		
			org.springframework.boot
			spring-boot-starter-web
		
		
		

		
		
			org.quartz-scheduler
			quartz
			2.2.1
			
				
					slf4j-api
					org.slf4j
				
			
		

		
		
			org.springframework
			spring-context-support
		

		
		
			org.springframework
			spring-tx
		

5.2创建一个Job类implement quartz的Job类

package com.jiayun;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class QuartzJob implements Job {

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		System.out.println("任务类干活了");

	}

}

5.3写一个quartz定时任务配置类,类名用@Configuration注解,类中有三个方法就是quartz实例的三步

package com.jiayun.conf;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

import com.jiayun.QuartzJob;

@Configuration
public class QuartzConf {
	
	/**
	 * 1.创建Job对象
	 */
	@Bean
	public JobDetailFactoryBean getJobDetailFactoryBean(){
		JobDetailFactoryBean jdfb = new JobDetailFactoryBean();
		jdfb.setJobClass(QuartzJob.class);
		return jdfb;
	}
	
	/**
	 * 2.创建Trigger对象
	 */
	@Bean
	public SimpleTriggerFactoryBean getSimpleTriggerFactoryBean(JobDetailFactoryBean jdfb){
		SimpleTriggerFactoryBean stfb = new SimpleTriggerFactoryBean();
		stfb.setJobDetail(jdfb.getObject());
		stfb.setRepeatInterval(2000);
		stfb.setRepeatCount(10);
		return stfb;
		
	}
	
	@Bean
	public CronTriggerFactoryBean getCronTriggerFactoryBean(JobDetailFactoryBean jdfb){
		CronTriggerFactoryBean ctfbBean = new CronTriggerFactoryBean();
		ctfbBean.setJobDetail(jdfb.getObject());
		ctfbBean.setCronExpression("0/3 * * * * ?");
		return ctfbBean;
	}
	
	/**
	 * 3.创建Schedule对象
	 */
	@Bean
	public SchedulerFactoryBean getSchedulerFactoryBean(CronTriggerFactoryBean stfb){
		SchedulerFactoryBean sfb = new SchedulerFactoryBean();
		sfb.setTriggers(stfb.getObject());
		return sfb;
		
	}

}

5.4启动类App.class中开启定时任务

package com.jiayun;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class App {
	
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}

}

 

 

 

你可能感兴趣的:(微架构spring-boot,Java)