项目中用到job的主要是各种公告,比如活动开始公告这种,以此为例:

public class Domain {

    public Domain() {

        AnnounceManager.getIns().startAllAnnounceTimer();
}
public static void main(String[] args) {
new Domain();
}
}

#主要处理类AnnounceManager

public class AnnounceManager {
private static AnnounceManager ins = new AnnounceManager(); private AnnounceManager() {
} public static AnnounceManager getIns() {
return ins;
} /**
* 向客户端发送公告信息
*/
public void announce(AnnouncementInfo info) {
// 在这里获取到对应公告的实例对象,并进行不同的公告逻辑
System.out.println("AnnounceManager.announce(): " + "开始公告~");
} public void startAllAnnounceTimer() {
List<Z_announcement_info> list = new ArrayList<>();
try {
Scheduler scheduler = new StdSchedulerFactory().getScheduler(); if (!scheduler.isStarted()) {
scheduler.start();
} else {
scheduler.clear();
}
// ************ORCO************
Z_announcement_info a = new Z_announcement_info();
a.setAnnounceId(2);
a.setAnnounceName("活动开始");
a.setCronExpression("0 24 14 * * ?");// 14点24的时候触发,可以随意修改,来进行测试
a.setDefineId(2);
a.setDirection(0);
a.setHasExtraParam(1);
a.setSpeed(5);
a.setStatus(0);
// 定时器表
list.add(a);
// ************ORCO************ // 公告数据存于数据库,quartz.properties中没有配置使用插件
// ORCO中间的代码,目的只要是从数据库中取出公告配置数据,如cron表达式等
// 原本ORCO中间的代码为:list =
// DataManager.getIns.getZ_announcement_infoList();
// 为了清晰,如此修改 AnnouncementInfo info = null;
for (Z_announcement_info z_announcement_info : list) { // AnnouncementInfo与Z_announcement_info内容基本一致,可以用同一个类
info = new AnnouncementInfo(z_announcement_info); // 任务名,任务组,任务执行类
JobDetailImpl jobDetail = new JobDetailImpl();
jobDetail.setJobClass(AnnounceJob.class);
jobDetail.setKey(JobKey.jobKey(info.getAnnounceName())); Map<String, AnnouncementInfo> map = new HashMap<String, AnnouncementInfo>(); // jobDataMapKey可随意修改,在继承JOB的类中从map中通过此key获取map数据
map.put("jobDataMapKey", info);
jobDetail.setJobDataMap(new JobDataMap(map)); // cron 表达式
String cronExpression = info.getCronExpression().trim();
// 触发器
CronTriggerImpl trigger = new CronTriggerImpl();
// 触发器名
trigger.setKey(TriggerKey.triggerKey(info.getAnnounceName()));
try {
// 触发器时间设定
trigger.setCronExpression(cronExpression);
scheduler.scheduleJob(jobDetail, trigger);
} catch (ParseException e) {
}
}
} catch (SchedulerException e) {
} catch (Exception e1) {
}
}
}

#接下来,如果到触发时间,就会走实现JOB接口的类,进行你的逻辑

public class AnnounceJob implements Job {

    @Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
JobDataMap dm = arg0.getMergedJobDataMap();
AnnouncementInfo info = (AnnouncementInfo) dm.get("jobDataMapKey");
try {
AnnounceManager.getIns().announce(info);
} catch (Exception e) {
e.printStackTrace();
}
} }

#与数据库表相同结构的类

/**
* 公告详细信息
*/
public class Z_announcement_info
{
// 公告id
private int announceId; // 公告名
private String announceName; // 公告类型ID
private int defineId; // 公告的时间周期,使用cron表达式
private String cronExpression; // 公告的走向(0:从右向左,1:从左向右)
private int direction; // 公告的播放速度
private int speed; // 是否拥有额外参数(1 有,0 没有)
private int hasExtraParam; // 是否有效 0:失效,1:有效
private int status; public int getAnnounceId()
{
return announceId;
} public void setAnnounceId(int announceId)
{
this.announceId = announceId;
} public String getAnnounceName()
{
return announceName;
} public void setAnnounceName(String announceName)
{
this.announceName = announceName;
} public int getDefineId()
{
return defineId;
} public void setDefineId(int defineId)
{
this.defineId = defineId;
} public String getCronExpression()
{
return cronExpression;
} public void setCronExpression(String cronExpression)
{
this.cronExpression = cronExpression;
} public int getDirection()
{
return direction;
} public void setDirection(int direction)
{
this.direction = direction;
} public int getSpeed()
{
return speed;
} public void setSpeed(int speed)
{
this.speed = speed;
} public int getHasExtraParam()
{
return hasExtraParam;
} public void setHasExtraParam(int hasExtraParam)
{
this.hasExtraParam = hasExtraParam;
} public int getStatus()
{
return status;
} public void setStatus(int status)
{
this.status = status;
} @Override
public String toString()
{
return "Z_announcement_info [announceId=" + announceId + ", announceName=" + announceName + ", defineId="
+ defineId + ", cronExpression=" + cronExpression + ", direction=" + direction + ", speed=" + speed
+ ", hasExtraParam=" + hasExtraParam + ", status=" + status + "]";
}
}

#最后就是quartz.properties的配置了,这里没有使用插件,此properties能应用大多数情况

# Default Properties file for use by StdSchedulerFactory
# to create a Quartz Scheduler Instance, if a different
# properties file is not explicitly specified. org.quartz.scheduler.instanceName: DefaultQuartzScheduler
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 10
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true org.quartz.jobStore.misfireThreshold: 60000 org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore

注:需上传jar包 quartz-2.2.1.jar、quartz-jobs-2.2.1.jar、log4j-1.2.12.jar、slf4j-api-1.6.4.jar、slf4j-log4j12-1.6.4.jar

后三个jar包不导入,会报错。

至此,一个简单的示例已经完成,可以成功运行。

Quartz Job基本示例的更多相关文章

  1. quartz + spring 配置示例

    <!-- 配置job定时任务类 --> <bean id="triggerCalculateLecturerProfitJob" class="com. ...

  2. Quartz实例:quartz定时任务代码示例

    转自:http://www.blogchong.com/post/96.html quartz定时任务调度框架,使用实例. Job类://即实际调度任务实现 . package net.csdn.ed ...

  3. C#使用Quartz.NET详细讲解

    Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不牺牲 ...

  4. Quartz.NET作业调度框架详解(转)

    Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不牺牲 ...

  5. Jobs定时器 - Quartz

    Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为运行十个,百个, ...

  6. .net Quartz 服务 作业调度

    .net项目中使用Quartz   (1)在web.config中进行相关配置 <configSections> <section name="quartz" t ...

  7. Quartz学习——Quartz简单入门Demo(二)

    要学习Quartz框架,首先大概了解了Quartz的基本知识后,在通过简单的例子入门,一步一个脚印的走下去. 下面介绍Quartz入门的示例,由于Quartz的存储方式分为RAM和JDBC,分别对这两 ...

  8. Quartz.NET快速入门指南

    最近,在工作中遇到了 Quartz.net 这个组件,为了更好的理解项目代码的来龙去脉,于是决定好好的研究一下这个东西.确实是好东西,既然是好东西,我就拿出来分享一下.万丈高楼平地起,我们也从入门开始 ...

  9. Quartz教程:快速入门

    原文链接 | 译文链接 | 翻译:nkcoder | 校对:方腾飞 本系列教程由quartz-2.2.x官方文档翻译.整理而来,希望给同样对quartz感兴趣的朋友一些参考和帮助,有任何不当或错误之处 ...

随机推荐

  1. CentOS 新增swap交换空间

    在centos 6.4 64位系统中安装oracle 10g数据库软件,但由于交换空间过小导致检查不通过: 因此需要增加交换空间的大小. 第一步:在opt目录下新建swap交换文件,并设置其大小为2G ...

  2. win7 cmd 操作mysql数据库

    一 ,对MySql服务器的开启,重启,关闭等操作       当然,可以在win7的界面环境下,关闭或开启MySql服务.但是经常找不到win7的服务管理器,主要定位方法有二:命令行下输入servic ...

  3. 我常用的VS技巧

    声明:开发工具使用的是VS2013 1.开发 包括编辑,代码补全等 1.1快速选择一行 第一种方式:鼠标停留在要选择的行上,三击. 第二种方式:鼠标停留在要选择的行上,按home键将鼠标停留在行首,按 ...

  4. HashMap遍历的两种方式

    第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) {    ...

  5. linux perf - 性能测试和优化工具

    Perf简介 Perf是Linux kernel自带的系统性能优化工具.虽然它的版本还只是0.0.2,Perf已经显现出它强大的实力,足以与目前Linux流行的OProfile相媲美了. Perf 的 ...

  6. python基础-迭代器和生成器

    一.递归和迭代 1.递归:(问路示例) 递归算法是一种直接或者间接地调用自身算法的过程.在计算机编写程序中,递归算法对解决一大类问题是十分有效的,它往往使算法的描述简洁而且易于理解. 2.迭代:简单理 ...

  7. C#字符串操作 取文本左边 取文本右边 取文本中间 取文本中间到List集合 指定文本倒序

    /// <summary> /// 取文本左边内容 /// </summary> /// <param name="str">文本</pa ...

  8. IIS如何配置可以下载APK、IPA文件

    解决步骤: 1).打开IIS服务管理器,找到服务器,右键-属性,打开IIS服务属性: 2.单击MIME类型下的“MIME类型”按钮,打开MIME类型设置窗口: 3).单击“新建”,建立新的MIME类型 ...

  9. shell判断条件整理

    1.字符串判断 str1 = str2 当两个字符串串有相同内容.长度时为真 str1 != str2 当字符串str1和str2不等时为真 -n str1 当字符串的长度大于0时为真(串非空) -z ...

  10. 自然语言27_Converting words to Features with NLTK

    https://www.pythonprogramming.net/words-as-features-nltk-tutorial/ Converting words to Features with ...