HOW-TO: Quartz Scheduler with Clustering in JEE application_MySQL

雨雪姑娘_6079

雨雪姑娘_6079

2016-06-01

3356人浏览

原创

quartz scheduler is one of the most popular scheduling library in java world. i had worked with quartz mostly in spring applications in the past. recently, i have been investigating scheduling in jee 6 application running on jboss 7.1.1 that is going to be deployed in the cloud. as one of the options i consider is quartz scheduler as it offers clustering with database. in this article i will show how easy is to configure quartz in jee application and run it either on jboss 7.1.1 or wildfly 8.0.0, use mysql as job store and utilize cdi to use dependency injection in jobs. all will be done in intellij. let’s get started.

Create Maven project

I usedorg.codehaus.mojo.archetypes:webapp-javaee6archetype to bootstrap the application and then I slightly modified thepom.xml. I also addedslf4Jdependency, so the resultingpom.xmllooks as following:

<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/xsd/maven-4.0.0.xsd"><modelversion>4.0.0</modelversion><groupid>pl.codeleak</groupid><artifactid>quartz-jee-demo</artifactid><version>1.0</version><packaging>war</packaging><name>quartz-jee-demo</name><properties><endorsed.dir>${project.build.directory}/endorsed</endorsed.dir><project.build.sourceencoding>UTF-8</project.build.sourceencoding></properties><dependencies><dependency><groupid>javax</groupid><artifactid>javaee-api</artifactid><version>6.0</version><scope>provided</scope></dependency><!-- Logging --><dependency><groupid>org.slf4j</groupid><artifactid>slf4j-api</artifactid><version>1.7.7</version></dependency><dependency><groupid>org.slf4j</groupid><artifactid>slf4j-jdk14</artifactid><version>1.7.7</version></dependency></dependencies><build><plugins><plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-compiler-plugin</artifactid><version>2.3.2</version><configuration><source>1.7</source><target>1.7</target><compilerarguments><endorseddirs>${endorsed.dir}</endorseddirs></compilerarguments></configuration></plugin><plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-war-plugin</artifactid><version>2.1.1</version><configuration><failonmissingwebxml>false</failonmissingwebxml></configuration></plugin><plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-dependency-plugin</artifactid><version>2.1</version><executions><execution><phase>validate</phase><goals><goal>copy</goal></goals><configuration><outputdirectory>${endorsed.dir}</outputdirectory><silent>true</silent><artifactitems><artifactitem><groupid>javax</groupid><artifactid>javaee-endorsed-api</artifactid><version>6.0</version><type>jar</type></artifactitem></artifactitems></configuration></execution></executions></plugin></plugins></build></project>

The next thing was to import the project to IDE. In my case this is IntelliJ and create a run configuration with JBoss 7.1.1.

One note, in the VM Options in run configuration I added two variables:

-Djboss.server.default.config=standalone-custom.xml-Djboss.socket.binding.port-offset=100

quartz-scheduler-with-clustering-img1

standalone-custom.xmlis a copy of the standardstandalone.xml, as the configuration will need to be modified (see below).

Configure JBoss server

In my demo application I wanted to use MySQL database with Quartz, so I needed to add MySQL data source to my configuration. This can be quickly done with two steps.

Add Driver Module

I created a folderJBOSS_HOME/modules/com/mysql/main. In this folder I added two files:module.xmlandmysql-connector-java-5.1.23.jar. The module file looks as follows:

<?xml version="1.0" encoding="UTF-8"?><module xmlns="urn:jboss:module:1.0" name="com.mysql"><resources><resource-root path="mysql-connector-java-5.1.23.jar"></resource-root></resources><dependencies><module name="javax.api"></module></dependencies></module>

Configure Data Source

In thestandalone-custom.xmlfile in thedatasourcessubsystem I added a new data source:

<datasource jta="false" jndi-name="java:jboss/datasources/MySqlDS" pool-name="MySqlDS" enabled="true" use-java-context="true"><connection-url>jdbc:mysql://localhost:3306/javaee</connection-url><driver>com.mysql</driver><security><user-name>jeeuser</user-name><password>pass</password></security></datasource>

And the driver:

<drivers><driver name="com.mysql" module="com.mysql"></driver></drivers>

Note: For the purpose of this demo, the data source is not JTA managed to simplify the configuration.

Configure Quartz with Clustering

I used official tutorial to configure Quarts with Clustering:http://quartz-scheduler.org/documentation/quartz-2.2.x/configuration/ConfigJDBCJobStoreClustering

Add Quartz dependencies topom.xml

<dependency><groupid>org.quartz-scheduler</groupid><artifactid>quartz</artifactid><version>2.2.1</version></dependency><dependency><groupid>org.quartz-scheduler</groupid><artifactid>quartz-jobs</artifactid><version>2.2.1</version></dependency>

Addquartz.propertiestosrc/main/resources

#============================================================================# Configure Main Scheduler Properties#============================================================================org.quartz.scheduler.instanceName = MySchedulerorg.quartz.scheduler.instanceId = AUTO#============================================================================# Configure ThreadPool#============================================================================org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPoolorg.quartz.threadPool.threadCount = 1#============================================================================# Configure JobStore#============================================================================org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTXorg.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegateorg.quartz.jobStore.useProperties = falseorg.quartz.jobStore.dataSource=MySqlDSorg.quartz.jobStore.isClustered = trueorg.quartz.jobStore.clusterCheckinInterval = 5000org.quartz.dataSource.MySqlDS.jndiURL=java:jboss/datasources/MySqlDS

Create MySQL tables to be used by Quartz

The schema file can be found in the Quartz distribution:quartz-2.2.1/docs/dbTables.

Demo code

Having the configuration in place, I wanted to check if Quartz works, so I created a scheduler, with no jobs and triggers.

package pl.codeleak.quartzdemo;import org.quartz.JobKey;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.quartz.TriggerKey;import org.quartz.impl.StdSchedulerFactory;import org.quartz.impl.matchers.GroupMatcher;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;import javax.ejb.Singleton;import javax.ejb.Startup;@Startup@Singletonpublic class SchedulerBean {	private Logger LOG = LoggerFactory.getLogger(SchedulerBean.class);	private Scheduler scheduler;	@PostConstruct	public void scheduleJobs() {		try {			scheduler = new StdSchedulerFactory().getScheduler();						scheduler.start();			printJobsAndTriggers(scheduler);		} catch (SchedulerException e) {		 LOG.error("Error while creating scheduler", e);		}	}	private void printJobsAndTriggers(Scheduler scheduler) throws SchedulerException {		LOG.info("Quartz Scheduler: {}", scheduler.getSchedulerName());		for(String group: scheduler.getJobGroupNames()) {			for(JobKey jobKey : scheduler.getJobKeys(GroupMatcher.<jobkey>groupEquals(group))) {				LOG.info("Found job identified by {}", jobKey);			}		}		for(String group: scheduler.getTriggerGroupNames()) {			for(TriggerKey triggerKey : scheduler.getTriggerKeys(GroupMatcher.<triggerkey>groupEquals(group))) {				LOG.info("Found trigger identified by {}", triggerKey);			}		}	}	@PreDestroy	public void stopJobs() {		if (scheduler != null) {			try {				scheduler.shutdown(false);			} catch (SchedulerException e) {				LOG.error("Error while closing scheduler", e);			}		}	}}</triggerkey></jobkey>

When you run the application you should be able to see some debugging information from Quartz:

Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.NOT STARTED.Currently in standby mode.Number of jobs executed: 0Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 1 threads.Using job-store 'org.quartz.impl.jdbcjobstore.JobStoreTX' - which supports persistence. and is clustered.

Let Quartz utilize CDI

In Quartz, jobs must implementorg.quartz.Jobinterface.

package pl.codeleak.quartzdemo;import org.quartz.Job;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;public class SimpleJob implements Job {@Overridepublic void execute(JobExecutionContext context) throws JobExecutionException {// do something}}

Then to create a Job we use JobBuilder:

JobKey job1Key = JobKey.jobKey("job1", "my-jobs");JobDetail job1 = JobBuilder.newJob(SimpleJob.class).withIdentity(job1Key).build();

In my example, I needed to inject EJBs to my jobs in order to re-use existing application logic. So in fact, I needed to inject a EJB reference. How this can be done with Quartz? Easy. Quartz Scheduler has a method to provide JobFactory to that will be responsible for creating Job instances.

package pl.codeleak.quartzdemo;import org.quartz.Job;import org.quartz.JobDetail;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.quartz.spi.JobFactory;import org.quartz.spi.TriggerFiredBundle;import javax.enterprise.inject.Any;import javax.enterprise.inject.Instance;import javax.inject.Inject;import javax.inject.Named;public class CdiJobFactory implements JobFactory {	@Inject	@Any	private Instance<job> jobs;	@Override	public Job newJob(TriggerFiredBundle triggerFiredBundle, Scheduler scheduler) throws SchedulerException {		final JobDetail jobDetail = triggerFiredBundle.getJobDetail();		final Class extends Job> jobClass = jobDetail.getJobClass();		for (Job job : jobs) {			if (job.getClass().isAssignableFrom(jobClass)) {				return job;			}		}		throw new RuntimeException("Cannot create a Job of type " + jobClass);	}}</job>

As of now, all jobs can use dependency injection and inject other dependencies, including EJBs.

package pl.codeleak.quartzdemo.ejb;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import javax.ejb.Stateless;@Statelesspublic class SimpleEjb {		private static final Logger LOG = LoggerFactory.getLogger(SimpleEjb.class);		public void doSomething() {		LOG.info("Inside an EJB");	}}package pl.codeleak.quartzdemo;import org.quartz.Job;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import pl.codeleak.quartzdemo.ejb.SimpleEjb;import javax.ejb.EJB;import javax.inject.Named;public class SimpleJob implements Job {	@EJB // @Inject will work too	private SimpleEjb simpleEjb;	@Override	public void execute(JobExecutionContext context) throws JobExecutionException {		simpleEjb.doSomething();	}}

The last step is to modify SchedulerBean:

package pl.codeleak.quartzdemo;import org.quartz.*;import org.quartz.impl.StdSchedulerFactory;import org.quartz.impl.matchers.GroupMatcher;import org.quartz.spi.JobFactory;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;import javax.ejb.Singleton;import javax.ejb.Startup;import javax.inject.Inject;@Startup@Singletonpublic class SchedulerBean {	private Logger LOG = LoggerFactory.getLogger(SchedulerBean.class);	private Scheduler scheduler;	@Inject	private JobFactory cdiJobFactory;	@PostConstruct	public void scheduleJobs() {		try {			scheduler = new StdSchedulerFactory().getScheduler();			scheduler.setJobFactory(cdiJobFactory);			JobKey job1Key = JobKey.jobKey("job1", "my-jobs");			JobDetail job1 = JobBuilder					.newJob(SimpleJob.class)					.withIdentity(job1Key)					.build();			TriggerKey tk1 = TriggerKey.triggerKey("trigger1", "my-jobs");			Trigger trigger1 = TriggerBuilder					.newTrigger()					.withIdentity(tk1)					.startNow()					.withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(10))					.build();			scheduler.scheduleJob(job1, trigger1);			scheduler.start();			printJobsAndTriggers(scheduler);		} catch (SchedulerException e) {			LOG.error("Error while creating scheduler", e);		}	}	private void printJobsAndTriggers(Scheduler scheduler) throws SchedulerException {		// not changed	}	@PreDestroy	public void stopJobs() {		// not changed	}}

Note: Before running the application add beans.xml file to WEB-INF directory.

<?xml version="1.0" encoding="UTF-8"?><beans 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/beans_1_1.xsd" bean-discovery-mode="all"></beans>

You can now start the server and observe the results. Firstly, job and trigger was created:

12:08:19,592 INFO (MSC service thread 1-3) Quartz Scheduler: MyScheduler12:08:19,612 INFO (MSC service thread 1-3) Found job identified by my-jobs.job112:08:19,616 INFO (MSC service thread 1-3) Found trigger identified by m

Our job is running (at about every 10 seconds):

12:08:29,148 INFO (MyScheduler_Worker-1) Inside an EJB12:08:39,165 INFO (MyScheduler_Worker-1) Inside an EJB

Look also inside the Quartz tables, and you will see it is filled in with the data.

Test the application

The last thing I wanted to check was how the jobs are triggered in multiple instances. For my test, I just cloned the server configuration twice in IntelliJ and assigned different port offset to each new copy.

quartz-scheduler-with-clustering-img2
Additional change I needed to do is to modify the creation of jobs and triggers. Since all Quartz objects are stored in the database, creating the same job and trigger (with the same keys) will cause an exception to be raised:

Error while creating scheduler: org.quartz.ObjectAlreadyExistsException: Unable to store Job : 'my-jobs.job1', because one already exists with this identification.

I needed to change the code, to make sure that if the job/trigger exists I update it. The final code of the scheduleJobs method for this test registers three triggers for the same job.

@PostConstructpublic void scheduleJobs() {try {	scheduler = new StdSchedulerFactory().getScheduler();	scheduler.setJobFactory(cdiJobFactory);	JobKey job1Key = JobKey.jobKey("job1", "my-jobs");	JobDetail job1 = JobBuilder		.newJob(SimpleJob.class)		.withIdentity(job1Key)		.build();	TriggerKey tk1 = TriggerKey.triggerKey("trigger1", "my-jobs");	Trigger trigger1 = TriggerBuilder		.newTrigger()		.withIdentity(tk1)		.startNow()		.withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(10))		.build();	TriggerKey tk2 = TriggerKey.triggerKey("trigger2", "my-jobs");	Trigger trigger2 = TriggerBuilder		.newTrigger()		.withIdentity(tk2)		.startNow()		.withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(10))		.build();	TriggerKey tk3 = TriggerKey.triggerKey("trigger3", "my-jobs");	Trigger trigger3 = TriggerBuilder		.newTrigger()		.withIdentity(tk3)		.startNow()		.withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(10))		.build();	scheduler.scheduleJob(job1, newHashSet(trigger1, trigger2, trigger3), true);	scheduler.start();	printJobsAndTriggers(scheduler);} catch (SchedulerException e) {	LOG.error("Error while creating scheduler", e);}}

In addition to the above, I added logging the JobExecutionContext in SimpleJob, so I could better analyze the outcome.

@Overridepublic void execute(JobExecutionContext context) throws JobExecutionException {try {	LOG.info("Instance: {}, Trigger: {}, Fired at: {}",		context.getScheduler().getSchedulerInstanceId(),		context.getTrigger().getKey(),		sdf.format(context.getFireTime()));} catch (SchedulerException e) {}simpleEjb.doSomething();}

After running all three server instances I observed the results.

quartz-scheduler-with-clustering-img3

Job execution

I observed trigger2 execution on all three nodes, and it was executed on three of them like this:

Instance: kolorobot1399805959393 (instance1), Trigger: my-jobs.trigger2, Fired at: 13:00:09Instance: kolorobot1399805989333 (instance3), Trigger: my-jobs.trigger2, Fired at: 13:00:19Instance: kolorobot1399805963359 (instance2), Trigger: my-jobs.trigger2, Fired at: 13:00:29Instance: kolorobot1399805959393 (instance1), Trigger: my-jobs.trigger2, Fired at: 13:00:39Instance: kolorobot1399805959393 (instance1), Trigger: my-jobs.trigger2, Fired at: 13:00:59

Similarly for other triggers.

Recovery

After I disconnected kolorobot1399805989333 (instance3), after some time I saw the following in the logs:

ClusterManager: detected 1 failed or restarted instances.ClusterManager: Scanning for instance "kolorobot1399805989333"'s failed in-progress jobs.

Then I disconnected kolorobot1399805963359 (instance2) and again this is what I saw in the logs:

ClusterManager: detected 1 failed or restarted instances.ClusterManager: Scanning for instance "kolorobot1399805963359"'s failed in-progress jobs.ClusterManager: ......Freed 1 acquired trigger(s).

As of now all triggers where executed by kolorobot1399805959393 (instance1)

Running on Wildfly 8

Without any change I could deploy the same application on WildFly 8.0.0. Similarly to JBoss 7.1.1 I added MySQL module (the location of modules folder is different on WildFly 8 –modules/system/layers/base/com/mysql/main. The datasource and the driver was defined exactly the same as shown above. I created a run configuration for WildFly 8:

quartz-scheduler-with-clustering-img4
And I ran the application getting the same results as with JBoss 7.

I found out the WildFly seem to offer adatabase based store for persistent EJB timers, but I did not investigate it yet. Maybe something for another blog post.

Source code

  • Please find the source code for this blog post on GitHub:https://github.com/kolorobot/quartz-jee-demo
Reference: HOW-TO: Quartz Scheduler with Clustering in JEE application with MySQLfrom ourJCG partnerRafal Borowiec at theCodeleak.plblog.

You might also like:
  • Getting started with Quartz Scheduler on MySQL database
  • Quartz 2 Scheduler example
  • Quartz scheduler plugins – hidden treasure
Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now! 

PHP速学视频免费教程(入门到精通)
PHP速学视频免费教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
GoFrame框架Web开发与路由中间件
GoFrame框架Web开发与路由中间件

本专题聚焦GoFrame框架的Web开发,拆解路由注册与匹配规则、请求参数获取、参数校验与中间件执行顺序,说明统一响应、JWT鉴权、文件上传与接口文档生成的落地方式,并补充优雅重启与测试实践。

2026.11.17

0

15

GoFrame框架安装与gf命令行工具
GoFrame框架安装与gf命令行工具

本专题围绕GoFrame框架的安装与工具链配置展开,讲清Go版本要求、gf命令行的安装方式、代理镜像与依赖准备,说明项目初始化、热编译、交叉编译打包的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.17

0

15

GoFrame框架零基础入门
GoFrame框架零基础入门

本专题整理GoFrame框架的入门知识,从模块化设计理念、环境准备到跑通第一个Web服务,讲清目录分层、配置加载、日志与中间件的用法,并对比Gin、Go-Zero的差异,帮助Go开发者建立整体认知。

2026.11.17

0

15

SQLAlchemy 一对多关系怎么建立
SQLAlchemy 一对多关系怎么建立

SQLAlchemy 一对多关系怎么建立?本专题整理父子表外键、relationship 双向映射、关联查询、级联删除和加载优化等相关文章,帮助你理解并实现常见的 ORM 关系建模。

2026.11.17

0

10

SQLAlchemy 如何增删改查
SQLAlchemy 如何增删改查

SQLAlchemy 如何增删改查?本专题整理 ORM 新增记录、条件查询、更新和删除、Session 事务提交与批量处理等实用文章,帮助你掌握 Python 数据库操作流程。

2026.11.17

0

11

SQLAlchemy ORM 模型怎么写
SQLAlchemy ORM 模型怎么写

SQLAlchemy ORM 模型怎么写?本专题整理模型类定义、Mapped 字段映射、主键与约束、表结构创建和常见报错的相关文章,帮助你从基础配置到实际建模逐步掌握 SQLAlchemy。

2026.11.17

0

16

Go-Zero API开发使用教程
Go-Zero API开发使用教程

本专题聚焦Go-Zero的API开发,拆解.api描述文件的语法结构、路由与分组定义、参数绑定与校验规则,说明统一响应体、JWT鉴权、跨域与接口文档生成的落地方式,并给出网关聚合与接口测试的实践建议。

2026.11.12

160

15

Go-Zero安装与goctl环境配置教程
Go-Zero安装与goctl环境配置教程

本专题围绕Go-Zero的安装与工具链配置展开,讲清Go版本要求、goctl安装方式、代理镜像、protoc依赖与环境变量设置,说明多版本共存、升级回退与CI流水线安装的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.12

120

15

Go-Zero零基础入门教程
Go-Zero零基础入门教程

本专题整理Go-Zero的入门知识,从框架定位、环境准备到跑通第一个API服务,讲清目录分层、配置加载、日志与中间件的基本用法,并对比Gin、Kratos的差异,帮助Go开发者建立整体认知,判断它是否适合当前项目。

2026.11.12

100

15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
热门推荐
/
最新课程
phpStudy极速入门视频教程
phpStudy极速入门视频教程

共6课时 | 54.7万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时 | 134.3万人学习