一、大致介绍 1、本章节仅仅只是为了测试 Quartz 在微服务中的使用情况; 2、其实若只是简单的实现任务调用而言的话,SpringBoot 的 Schedule 这个注解即可满足需求,但是注意该注解不支持分布式;需要了解电子商务平台源码可加企鹅邱邱 一零三八七七四六二六 3、注意:配置文件中的 mysql 数据库链接配置大家就各自配置; 二、实现步骤 2.1 添加 maven 引用包 ``` 4.0.0 springms-simple-quartz 1.0-SNAPSHOT jar com.springms.cloud springms-spring-cloud 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-web mysql mysql-connector-java org.quartz-scheduler quartz 2.3.0 org.quartz-scheduler quartz-jobs 2.3.0 org.springframework spring-context-support ``` 2.2 添加应用配置文件 ``` # mysql com.springms.cloud-test spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://ip:port/hmilyylimh spring.datasource.username=username spring.datasource.password=password spring.datasource.jpa.hibernate.ddl-auto=update spring.datasource.jpa.show-sql=true # embedded servlet container server.port=8390 # sessionTimeout in seconds server.sessionTimeout=30000 ``` 2.3 添加Spring上下文配置文件 ``` classpath:application.properties run 0/10 * * * * ? ``` 2.4 添加任务调度对象类 ``` /** * 任务调度对象。 * * @author hmilyylimh * * @version 0.0.1 * * @date 17/9/18 * */ public class ScheduleJob { private String jobName; private String jobGroup; private String desc; private String jobStatus; private String cronExpression; public String getJobName() { return jobName; } public void setJobName(String jobName) { this.jobName = jobName; } public String getJobGroup() { return jobGroup; } public void setJobGroup(String jobGroup) { this.jobGroup = jobGroup; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public String getJobStatus() { return jobStatus; } public void setJobStatus(String jobStatus) { this.jobStatus = jobStatus; } public String getCronExpression() { return cronExpression; } public void setCronExpression(String cronExpression) { this.cronExpression = cronExpression; } } ``` 2.5 添加QuartzJobFactory ``` package com.springms.cloud.service; import com.springms.cloud.domain.ScheduleJob; import org.quartz.DisallowConcurrentExecution; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; @DisallowConcurrentExecution public class QuartzJobFactory implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("任务成功运行"); ScheduleJob scheduleJob = (ScheduleJob)context.getMergedJobDataMap().get("scheduleJob"); System.out.println("任务名称 = [" + scheduleJob.getJobName() + "]"); } } ``` 2.6 添加定时任务服务 ``` package com.springms.cloud.service; import java.util.ArrayList; import java.util.List; import java.util.Set; import com.springms.cloud.task.TestTask; import com.springms.cloud.util.SpringApplicationContextUtil; import com.springms.cloud.domain.ScheduleJob; import org.quartz.CronTrigger; import org.quartz.JobKey; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.Trigger; import org.quartz.impl.matchers.GroupMatcher; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.quartz.SchedulerFactoryBean; /** * 定时任务服务。 * * @author hmilyylimh * * @version 0.0.1 * * @date 17/9/18 * */ public class ScheduleJobService { private static final Logger Logger = LoggerFactory.getLogger(TestTask.class); public void getScheduleJob(){ try { SchedulerFactoryBean schedulerFactoryBean = SpringApplicationContextUtil.getBean("scheduler"); Scheduler scheduler = schedulerFactoryBean.getScheduler(); GroupMatcher matcher = GroupMatcher.anyJobGroup(); Set jobKeys = scheduler.getJobKeys(matcher); List jobList = new ArrayList (); for (JobKey jobKey : jobKeys) { List triggers = scheduler.getTriggersOfJob(jobKey); for (Trigger trigger : triggers) { ScheduleJob job = new ScheduleJob(); job.setJobName(jobKey.getName()); job.setJobGroup(jobKey.getGroup()); job.setDesc("触发器:" + trigger.getKey()); Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey()); job.setJobStatus(triggerState.name()); if (trigger instanceof CronTrigger) { CronTrigger cronTrigger = (CronTrigger) trigger; String cronExpression = cronTrigger.getCronExpression(); job.setCronExpression(cronExpression); } jobList.add(job); } } for (ScheduleJob job : jobList) { Logger.info("计划列表,name:{},group:{},desc:{},status:{}",job.getJobName(),job.getJobGroup(),job.getDesc(),job.getJobStatus()); } } catch (SchedulerException e) { Logger.error("SchedulerException", e); } } } ``` 2.7 添加测试任务类 ``` package com.springms.cloud.task; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 测试任务类(被任务调度后执行该任务类)。 * * @author hmilyylimh * * @version 0.0.1 * * @date 17/9/18 * */ public class TestTask { /** 日志对象 */ private static final Logger LOG = LoggerFactory.getLogger(TestTask.class); public void run() { if (LOG.isInfoEnabled()) { LOG.info("测试任务线程开始执行"); //new ScheduleJobService().getScheduleJob(); } } } ``` 2.8 添加 Spring 上下文工具类 ``` package com.springms.cloud.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; /** * Spring 上下文工具类。 * * @author hmilyylimh * * @version 0.0.1 * * @date 17/9/18 * */ @Component public class SpringApplicationContextUtil implements ApplicationContextAware{ // 声明一个静态变量保存 private static ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringApplicationContextUtil.applicationContext=applicationContext; } public static ApplicationContext getContext(){ return applicationContext; } @SuppressWarnings("unchecked") public static T getBean(String name) throws BeansException { return (T) applicationContext.getBean(name); } } ``` 2.9 添加 Quartz 启动类 ``` package com.springms.cloud; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; import java.util.concurrent.TimeUnit; /** * 简单 Quartz 微服务,不支持分布式。 * * 其实若只是简单的实现任务调用而言的话,SpringBoot 的 Schedule 这个注解即可满足需求,但是注意该注解不支持分布式; * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/18 * */ @ComponentScan @Configuration @ImportResource("applicationContext.xml") public class SimpleQuartzApplication { private static final Logger Logger = LoggerFactory.getLogger(SimpleQuartzApplication.class); @Value("${server.port}") private int port; @Value("${server.sessionTimeout}") private int sessionTimeout; public static void main(String[] args) { Logger.info("简单Quartz微服务入口函数编码-" +System.getProperty("file.encoding")); SpringApplication.run(SimpleQuartzApplication.class, args); System.out.println(" 简单Quartz微服务 已启动."); } @Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); factory.setPort(port); factory.setSessionTimeout(sessionTimeout, TimeUnit.SECONDS); return factory; } } ``` 三、测试 1、添加 Quartz 相关配置文件; 2、启动 springms-simple-quartz 模块服务,启动1个端口; 3、然后查看日志, TestTask 类的日志不断被定时打印出来; [java B2B2C 多租户电子商城系统](https://2147775633.iteye.com/blog/2434341)