概述
Quartz也常用在Web应用中,常见的是交由Spring托管的形式,但这里并非介绍这个。如果你的很老的一个项目没有使用Spring呢? 这里我们介绍Quartz在Web应用中单独使用的场景。
实现
对于定时任务来讲,一般来说,Web应用启动时,应注册已经确定的定时任务;一些动态的、未确定触发时间的定时任务,后续可通过静态的Scheduler注册。
这里使用监听器在应用启动时注册,需要在web.xml注册这个监听器,在关闭Web应用时,也要相应的注销定时任务。
示例
maven工程
步骤一 构建Maven项目
pom.xml中添加依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.artisan</groupId> <artifactId>quartzInWeb</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>quartzInWeb Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </dependency> <!-- 日志组件slf4j xml的方式需增加 logback --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.21</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.21</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.7</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.7</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <finalName>quartzInWeb</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project>
步骤二 日志组件的配置logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="true"> <!-- 应用名称 --> <property name="APP_NAME" value="logtest" /> <!--日志文件的保存路径,首先查找系统属性-Dlog.dir,如果存在就使用其;否则,在当前目录下创建名为logs目录做日志存放的目录 --> <property name="LOG_HOME" value="${log.dir:-logs}/${APP_NAME}" /> <!-- 日志输出格式 --> <property name="ENCODER_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n" /> <contextName>${APP_NAME}</contextName> <!-- 控制台日志:输出全部日志到控制台 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <Pattern>${ENCODER_PATTERN}</Pattern> </encoder> </appender> <!-- 文件日志:输出全部日志到文件 --> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_HOME}/output.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>7</maxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>${ENCODER_PATTERN}</pattern> </encoder> </appender> <!-- 错误日志:用于将错误日志输出到独立文件 --> <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_HOME}/error.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>7</maxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>${ENCODER_PATTERN}</pattern> </encoder> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>WARN</level> </filter> </appender> <!-- 独立输出的同步日志 --> <appender name="SYNC_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_HOME}/sync.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>7</maxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>${ENCODER_PATTERN}</pattern> </encoder> </appender> <logger name="log.sync" level="DEBUG" addtivity="true"> <appender-ref ref="SYNC_FILE" /> </logger> <root> <level value="DEBUG" /> <appender-ref ref="STDOUT" /> <appender-ref ref="FILE" /> <appender-ref ref="ERROR_FILE" /> </root> </configuration>
步骤三 自定义监听器的编写
package com.artisan.quartz; import static org.quartz.JobBuilder.newJob; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SimpleScheduleBuilder; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ApplicationContextListener implements ServletContextListener { private Logger logger = LoggerFactory .getLogger(ApplicationContextListener.class); public static Scheduler scheduler = null; public void contextInitialized(ServletContextEvent servletContextEvent) { logger.info("Web应用开始..."); /* 注册定时任务 */ try { // 获取Scheduler实例 scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.start(); // 具体任务 JobDetail job = newJob(HelloJob.class) .withIdentity("job1", "group1").build(); // 触发时间点 SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder .simpleSchedule().withIntervalInSeconds(5).repeatForever(); Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("trigger1", "group1").startNow() .withSchedule(simpleScheduleBuilder).build(); // 交由Scheduler安排触发 scheduler.scheduleJob(job, trigger); logger.info("调度器开始注册:The scheduler register..."); } catch (SchedulerException se) { logger.error(se.getMessage(), se); } } public void contextDestroyed(ServletContextEvent sce) { logger.info("Web应用停止..."); /* 注销定时任务 */ try { // 关闭Scheduler scheduler.shutdown(); logger.info("调度器已关闭:The scheduler shutdown..."); } catch (SchedulerException se) { logger.error(se.getMessage(), se); } } }
假设我们有一个明确的任务,在初始化监听器的时候就启动执行,如下
package com.artisan.quartz; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class HelloJob implements Job { private Logger logger = LoggerFactory.getLogger(HelloJob.class); @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("Hello Job"); // 此任务仅打印日志便于调试、观察 System.out.println(this.getClass().getSimpleName() + " trigger..."); logger.debug(this.getClass().getSimpleName() + " trigger..."); } }
步骤四 web.xml中注册监听器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- 加入自定义监听器 --> <listener> <listener-class>com.artisan.quartz.ApplicationContextListener</listener-class> </listener> </web-app>
步骤五 启动
由于我们使用的JDK1.7 ,我们用的tomcat,这里tomcat的版本需要为8.0
关键日志如下:
如果我们Eclipse或者Spring tool suit中调试,无法看到contextDestroyed方法的执行。
SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever();
repeatForever()方法,这个方法的意思表示永远执行,当然我们也可以自定义重复执行的次数,使用withRepeatCount(10)方法,10表示执行了10次