1.pom.xml
<dependency> <groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId> <version>${quartz-version}</version>
</dependency> <dependency> <groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring-context-version}</version> </dependency>
<spring-context-version>4.0.6.RELEASE</spring-context-version>
<quartz-version>2.2.1</quartz-version>
2.  QuartzConfiguration
import com.ppdai.baihang.job.FirstJob; import com.ppdai.baihang.job.SecondJob;
import com.ppdai.baihang.utils.SpringUtil; import org.quartz.JobDetail; import
org.quartz.Trigger; import org.springframework.context.annotation.Bean; import
org.springframework.context.annotation.Configuration; import
org.springframework.scheduling.quartz.CronTriggerFactoryBean; import
org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean; import
org.springframework.scheduling.quartz.SimpleTriggerFactoryBean; @Configuration
public class QuartzConfiguration { // 配置触发器1 @Bean(name = "firstTrigger")
public SimpleTriggerFactoryBean firstTrigger(JobDetail firstJobDetail) {
SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
trigger.setJobDetail(firstJobDetail); // 设置任务启动延迟 trigger.setStartDelay(0); //
每5秒执行一次 trigger.setRepeatInterval(5000); return trigger; } // 配置定时任务1
@Bean(name = "firstJobDetail") public MethodInvokingJobDetailFactoryBean
firstJobDetail() { FirstJob firstJob = (FirstJob)
SpringUtil.getBean(FirstJob.class); MethodInvokingJobDetailFactoryBean
jobDetail = new MethodInvokingJobDetailFactoryBean(); // 是否并发执行
jobDetail.setConcurrent(false); // 为需要执行的实体类对应的对象
jobDetail.setTargetObject(firstJob); // 需要执行的方法
jobDetail.setTargetMethod("task"); return jobDetail; } // 配置触发器2 @Bean(name =
"secondTrigger") public CronTriggerFactoryBean secondTrigger(JobDetail
secondJobDetail) { CronTriggerFactoryBean trigger = new
CronTriggerFactoryBean(); trigger.setJobDetail(secondJobDetail); // cron表达式
trigger.setCronExpression("*/5 * * * * ?"); return trigger; } // 配置定时任务2
@Bean(name = "secondJobDetail") public MethodInvokingJobDetailFactoryBean
secondJobDetail() { SecondJob secondJob = (SecondJob)
SpringUtil.getBean(SecondJob.class); MethodInvokingJobDetailFactoryBean
jobDetail = new MethodInvokingJobDetailFactoryBean(); // 是否并发执行
jobDetail.setConcurrent(false); // 为需要执行的实体类对应的对象
jobDetail.setTargetObject(secondJob); // 需要执行的方法
jobDetail.setTargetMethod("task"); return jobDetail; } // 配置Scheduler
@Bean(name = "scheduler") public SchedulerFactoryBean schedulerFactory(Trigger
firstTrigger, Trigger secondTrigger) { SchedulerFactoryBean bean = new
SchedulerFactoryBean(); // 延时启动,应用启动1秒后 bean.setStartupDelay(1); // 注册触发器
bean.setTriggers(firstTrigger, secondTrigger); return bean; } }
3.FirstJob
import org.springframework.scheduling.annotation.EnableScheduling; import
org.springframework.stereotype.Component; @EnableScheduling @Component public
class FirstJob{ public void task(){ System.out.println("任务1执行...."); } }
4.SecondJob
import org.springframework.scheduling.annotation.EnableScheduling; import
org.springframework.stereotype.Component; @EnableScheduling @Component public
class SecondJob{ public void task(){ System.out.println("任务2执行...."); } }
5.由于QuartzConfiguration中使用@Configuratios注解  故不能使用注解的方式注入bean,
写个util从ApplicationContext中获取
import org.springframework.beans.BeansException; import
org.springframework.context.ApplicationContext; import
org.springframework.context.ApplicationContextAware; import
org.springframework.stereotype.Component; @Component public class SpringUtil
implements ApplicationContextAware { private static ApplicationContext
applicationContext; @Override public void
setApplicationContext(ApplicationContext applicationContext) throws
BeansException { if (SpringUtil.applicationContext == null) {
SpringUtil.applicationContext = applicationContext; } } //获取applicationContext
public static ApplicationContext getApplicationContext() { return
applicationContext; } //通过name获取 Bean. public static Object getBean(String
name) { return getApplicationContext().getBean(name); } //通过class获取Bean. public
static <T> T getBean(Class<T> clazz) { return
getApplicationContext().getBean(clazz); } //通过name,以及Clazz返回指定的Bean public
static <T> T getBean(String name, Class<T> clazz) { return
getApplicationContext().getBean(name, clazz); } }
 

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信