quartz简单应用
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>cn.byref</groupId>
<artifactId>web-quartz</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>web-quartz 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>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz-jobs -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.transaction/jta -->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency> </dependencies>
<build>
<finalName>web-quartz</finalName>
</build>
</project>
quartz.properties
#============================================================================
# Configure Main Scheduler Properties
#============================================================================ org.quartz.scheduler.instanceName = QuartzScheduler
org.quartz.scheduler.instanceId = AUTO #============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 3
org.quartz.threadPool.threadPriority = 5 #============================================================================
# Configure JobStore
#============================================================================
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore #============================================================================
# Configure Plugins
#============================================================================
org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = quartz_job.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval =10
org.quartz.plugin.jobInitializer.wrapInUserTransaction = false org.quartz.scheduler.skipUpdateCheck = true
quartz_job.xml
<?xml version="1.0" encoding="UTF-8"?>
<job-scheduling-data
xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_2_0.xsd"
version="2.0">
<schedule>
<job> <name>myjob</name>
<group>mygroup</group>
<description>my job</description>
<job-class>cn.yrm.web.job.MyJob</job-class>
</job>
<trigger>
<cron>
<name>my trigger</name>
<group>mygroup</group>
<job-name>myjob</job-name>
<job-group>mygroup</job-group>
<cron-expression>0/5 * * * * ?</cron-expression>
</cron>
</trigger> </schedule>
</job-scheduling-data>
Job实现类:
package cn.yrm.web.job; import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; @DisallowConcurrentExecution
public class MyJob implements Job { public void execute(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
System.out.println("my job execute");
} }
ServletContextListener实现类:
package cn.yrm.web.listener; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import cn.yrm.web.job.MyJob; public class MyQuatzListener implements ServletContextListener { Logger logger = LoggerFactory.getLogger(this.getClass());
private Scheduler scheduler; public void contextDestroyed(ServletContextEvent arg0) {
logger.info("shutdown scheduler"); if (scheduler != null) {
try {
scheduler.shutdown();
} catch (SchedulerException e) {
logger.error(e.getMessage());
e.printStackTrace();
}
}
} public void contextInitialized(ServletContextEvent arg0) {
logger.info("context initialized begin start quartz");
try {
scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start(); // JobDetail jobDetail = JobBuilder.newJob(MyJob.class).withIdentity("myJob", "myGroup").build();
// CronScheduleBuilder cronScheduleBuilder= CronScheduleBuilder.cronSchedule("*/5 * * * * ?");
// Trigger trigger = TriggerBuilder.newTrigger()
// .withIdentity("myTrigger","myGroup")
// .startNow().withSchedule(cronScheduleBuilder).build();
//
// scheduler.scheduleJob(jobDetail, trigger); } catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }
quartz简单应用的更多相关文章
- quartz简单实现
quartz介绍 在我们的项目中,经常会遇到某个任务需要在特定的时间点执行,这个时间点可能是每天的某分某秒,可能是一周的指定某个星期某个时间等:quartz就是实现这样一个复杂任务调度逻辑的框架. 官 ...
- Java调度框架Quartz简单示例
Quartz的大名如雷贯耳,这里就不赘述,而且本文也不作为深入探讨,只是看完Quartz的官方文档后,下个简单示例,至少证明曾经花了点时间学习过,以备不时之需. Quartz使用了SLF4J,所以至少 ...
- Quartz学习——Quartz简单入门Demo(二)
要学习Quartz框架,首先大概了解了Quartz的基本知识后,在通过简单的例子入门,一步一个脚印的走下去. 下面介绍Quartz入门的示例,由于Quartz的存储方式分为RAM和JDBC,分别对这两 ...
- springmvc+quartz简单实现定时调度
一.简介:Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为运行十 ...
- Quartz简单案例
需求需要开发一个每天定时推送消息给微信用户,第一次接触quartz,简单案例 1. 先编辑要执行的任务 测试类代码 package com.wqq.test.quartz; import org.sp ...
- Quartz简单实现定时任务管理(SSM+Quartz)
首先你得有一个用Maven搭好的SSM框架,数据库用的Mysql,这里只有关于Quartz的部分.其实有大神总结的很好了,但做完后总有些地方不一样,所以写这篇作为笔记.这里先把大神的写的分享给大家:h ...
- Spring-boot+Spring-batch+hibernate+Quartz简单批量读文件写数据用例
本文程序集成了Spring-boot.Spring-batch.Spring-data-jpa.hibernate.Quartz.H2等.完整代码在Github上共享,地址https://github ...
- quartz 简单定时器
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- Quartz简单使用
官方的Quartz会提供例子的,例子看个前三四个感觉就够用了,主要就是起Timer的作用,但是比timer稳定,而且功能更全. UpdateClientTimer.task(ClearJob.clas ...
- 定时执行程序-Quartz简单实例
1.加jar包:Quartz自己quartz-1.8.3.jar和依赖包commons-logging.jar .slf4j-log4j12-1.5.10.jar .slf4j-api-1.5.10 ...
随机推荐
- openwrt undefined reference to ‘getpwent_r’
由于须要安装 su sudo useradd groupadd chown login 等用户管理命令,会訪问passwd文件. 用到了 getpwant_r 函数.详细是哪个命令引起的,临时不知. ...
- R-CNN for Small Object Detection
R-CNN for Small Object Detection 文章方法概括 这篇文章主要讨论针对小目标的目标检测 文章为了证明:对传统R-CNN style的方法进行改进,可以用于小目标检测,并且 ...
- 004-React入门概述
一.概述 参考地址:https://reactjs.org/docs/try-react.html 1.1.本地快速体验 <!DOCTYPE html> <html> < ...
- NSIS卸载后无法删除开始菜单中的内容
我们在安装程序时通常会使用createShortCut命令来创建一个快捷方式,如下,是在开始–>启动项 里创建run.bat的快捷方式. CreateShortCut "$SMPROG ...
- AOP-面向方面编程
面向方面编程(aspect-oriented programming,AOP)是一种编程范式,旨在提高模块化允许横向关注点的分离.该范式以一种成为方面(Aspect)的语言构造为基础,切面是一种新的模 ...
- 9. Palindrome Number(判断整型数字是否是回文,直接暴力即可)
Determine whether an integer is a palindrome. Do this without extra space. class Solution: def isPal ...
- ajax 事件使用
error: function (XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.status); alert(XMLH ...
- c语言单元测试框架--CuTest
1.简介 CuTest是一款微小的C语言单元测试框,是我迄今为止见到的最简洁的测试框架之一,只有2个文件,CuTest.c和CuTest.h,全部代码加起来不到一千行.麻雀虽小,五脏俱全,测试的构建. ...
- Apache 静态缓存配置
静态文件缓存 静态缓存在客户端下进行缓存,可以设置缓存文件类型与缓存时间,提升客户端访问站点速度. 语法格式 ExpiresByType type/encoding “<base> [pl ...
- shell脚本小示例
求1-100自然数的和: 法一:for循环 #!/bin/bash # declare -i sum=0 for ((i=0;i<=100;i++));do let sum+=$i done e ...