quartz定时任务-job
1,首先添加对quartz组建的引用 quartz-2.2.3.jar,slf4j-api-1.7.7.jar
https://files.cnblogs.com/files/renjing/quartz.zip
2,新增类实现job接口
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; public class TestJob implements Job { private static final Logger logger = LogManager.getLogger(TestJob.class); @Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
logger.debug("TestJob执行啦!");
}
}
3.新增一个Servlet,并且设置为在网站启动的时候自动初始化
import java.util.TimeZone; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory; //loadOnStartup表示:网站启动的时候自动初始化QuartzInitServlet(即init被调用)
@WebServlet(value = "/QuartzInitServlet", loadOnStartup = 1)
public class QuartzInitServlet extends HttpServlet { private static final Logger logger = LogManager.getLogger(QuartzInitServlet.class);
@Override
public void init() throws ServletException {
super.init(); try
{
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobDetail job = JobBuilder.newJob(TestJob.class).build();
CronScheduleBuilder cronScheduleBuilder =
CronScheduleBuilder.cronSchedule("0 0 0/1 * * ? ").inTimeZone(TimeZone.getTimeZone("GMT+08:00"));//每小时执行一次
//CronScheduleBuilder.cronSchedule("10/5 * * * * ? ").inTimeZone(TimeZone.getTimeZone("GMT+08:00"));//从10秒开始,每隔5秒执行一次
//CronScheduleBuilder.cronSchedule("45 53 23 * * ? ").inTimeZone(TimeZone.getTimeZone("GMT+08:00"));//每天23:53:45
//CronScheduleBuilder.dailyAtHourAndMinute(23, 48).inTimeZone(TimeZone.getTimeZone("GMT+08:00"));
CronTrigger trigger = TriggerBuilder.newTrigger().withSchedule(cronScheduleBuilder).build();
scheduler.scheduleJob(job, trigger);
scheduler.start();//不要忘了启动
}
catch(Throwable ex)
{
logger.error("定时任务安排出错",ex);
}
}
}
quartz定时任务-job的更多相关文章
- Quartz定时任务学习(二)web应用/Quartz定时任务学习(三)属性文件和jar
web中使用Quartz 1.首先在web.xml文件中加入 如下内容(根据自己情况设定) 在web.xml中添加QuartzInitializerServlet,Quartz为能够在web应用中使用 ...
- quartz定时任务框架的使用
quartz定时任务时间设置 这些星号由左到右按顺序代表 : * * * * * * * ...
- Quartz定时任务学习(二)web应用
web中使用Quartz 1.首先在web.xml文件中加入 如下内容(根据自己情况设定) 在web.xml中添加QuartzInitializerServlet,Quartz为能够在web应用中使用 ...
- Quartz定时任务使用小记(11月22日)
骤然接触quartz,先从小处着手,why,what,how quartz定时任务: 为什么使用quartz定时任务,以及定时任务在实际应用场景下的特定需求. 1.用户方面的需要,为了提供更好的使用体 ...
- quartz定时任务时间配置
quartz定时任务时间设置描述(2011-03-03 16:23:50)转载▼标签: quartz时间it 分类: 凌乱小记 这些星号由左到右按顺序代表 : * * * ...
- 对quartz定时任务的初步认识
已经好久没有写技术博文了,今天就谈一谈我前两天自学的quartz定时任务吧,我对quartz定时任务的理解,就是可以设定一个时间,然后呢,在这个时间到的时候,去执行业务逻辑,这是我的简单理解,接下来看 ...
- Spring整合Quartz定时任务执行2次,Spring定时任务执行2次
Spring整合Quartz定时任务执行2次,Spring定时任务执行2次 >>>>>>>>>>>>>>>&g ...
- Quartz 定时任务时间设置
转自https://blog.csdn.net/zdx1515888659/article/details/79158169 quartz定时任务时间设置: 这些星号由左到右按顺序代表 : * * * ...
- quartz定时任务及时间设置
quartz 定时任务时间设置1.这些星号由左到右按顺序代表 : * * * * * * * ...
- Spring整合Quartz定时任务 在集群、分布式系统中的应用(Mysql数据库环境)
Spring整合Quartz定时任务 在集群.分布式系统中的应用(Mysql数据库环境) 转载:http://www.cnblogs.com/jiafuwei/p/6145280.html 单个Q ...
随机推荐
- python主成分分析
#-*- coding: utf-8 -*- #主成分分析 降维 import pandas as pd #参数初始化 inputfile = '../data/principal_component ...
- the project already contains a form or module named pcm001怎麼解決
the project already contains a form or module named pcm001怎麼解決 菜单Project -> Remove from project.. ...
- html5應用緩存
HTML5使用了應用緩存,就是web應用緩存,使得在離線狀態下可以訪問web'應用. 應用緩存的優點: 離線訪問-可以在無網的狀態下訪問應用 速度-有緩存的應用加載更快 瀏覽器負載-瀏覽器只從服務器加 ...
- html DOM簡介
DOM:文檔對象模型 dom分為3類,核心DOM.xml DOM.HTML DOM: 核心DOM:針對任何結構化文檔的標準模型: xml DOM:針對xml的標準模型,定義了所有的元素的對象和屬性,以 ...
- 21 Zabbix系统性能优化建议
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 21 Zabbix系统性能优化建议 1. Zabbix性能变慢的可能表现: zabbix队列有太多 ...
- 自学Aruba6.2-控制器基本维护操作(web页面配置)
点击返回:自学Aruba之路 自学Aruba6.2-控制器基本维护操作(web页面配置) 1 显示当前控制器版本 Dashboard---Controller中 2 升级Aruab os版本 Main ...
- 自学Zabbix2.2-服务器端环境配置
点击返回:自学Zabbix之路
- Android canvast View 代码实例
package com.app.canvastest; import android.content.Context; import android.graphics.Bitmap; import a ...
- [luogu1373]小a和uim之大逃离【动态规划】
传送门:https://www.luogu.org/problemnew/show/P1373 定义状态是:\(f[i][j][h][0..1]\)表示在\([i,j]\)两个人相差为h,让某一个人走 ...
- bzoj1494 生成树计数 (dp+矩阵快速幂)
题面欺诈系列... 因为一个点最多只能连到前k个点,所以只有当前的连续k个点的连通情况是对接下来的求解有用的 那么就可以计算k个点的所有连通情况,dfs以下发现k=5的时候有52种. 我们把它们用类似 ...