1. 程式人生 > >java spring-mvc spring-boot quartz定時器

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引入依賴

<!-- springBoot的啟動器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
				   <!-- 新增Scheduled座標 -->
	    <dependency>
	        <groupId>org.springframework</groupId>
	        <artifactId>spring-context-support</artifactId>
	    </dependency>

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引入依賴

<!-- springBoot的啟動器 -->
<!-- springBoot的啟動器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		

		<!-- Quartz座標 -->
		<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz</artifactId>
			<version>2.2.1</version>
			<exclusions>
				<exclusion>
					<artifactId>slf4j-api</artifactId>
					<groupId>org.slf4j</groupId>
				</exclusion>
			</exclusions>
		</dependency>

		<!-- 新增Scheduled座標 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
		</dependency>

		<!-- Sprng tx 座標 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
		</dependency>

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);
	}

}