Integrating Quartz Scheduler In Web Application Java.
Integration of Quartz scheduler with Spring boot. Java Quartz scheduler cron expression example. Spring quartz scheduler postgresql database example.
Quartz Scheduler:
Spring boot:
Download complete source code of Quartz Scheduler with Spring Boot example using below link,
https://github.com/javabypatel/spring-boot-quartz-demo
Example uses,
Quartz work on concept of Jobs and Triggers.
There are 2 types of trigger,
For example, if you want the trigger to fire at exactly 11:23:54 AM on January 13, 2015, or if you want it to fire at that time, and then fire five more times, every ten seconds.
Cron Trigger: CronTrigger is often more useful than SimpleTrigger, It is used for scheduling jobs having complex time like if you need to execute job that recurs based on calendar-like notions, rather than on the exactly specified intervals.
With CronTrigger, you can specify firing-schedules such as "every Friday at noon", or "every weekday and 9:30 am", or even "every 5 minutes between 9:00 am and 10:00 am on every Monday, Wednesday and Friday during January".
Below are the few quartz methods that are used in example.
Demo application also explains Quartz listeners,
There are 2 types of listeners in quartz.
If you want to perform any task before job starts or when job completes or when trigger got misfired due to application shutdown, Quartz listeners will be helpful.
Download complete source code from here
After setting project in Eclipse,
Client application is listening on port 8080 and Server application is listening on port 7080.
If you don't want to run 2 application separately, then transpile the typescript files to javascript and put those files in your web application as shown below,
For converting typescript files to javascript files,
There are 3 configuration files,
application.properties :
It contains configuration for server and database as shown below,
data.sql :
Quartz require jobs and triggers to be stored in database.
data.sql contains SQL scripts that creates tables required by quartz to manage scheduler.
quartz.properties :
- Quartz is a richly featured, open source Job scheduling library.
- Quartz can be used to create simple or complex schedules for executing multiple jobs.
- Using quartz library, job can be schedule which can be executed instantly or to be executed later point of time.
- Quartz also accepts cron expression using which complex jobs can be scheduled like
"Run job after every 5 minutes" or "Run job every week on monday at 3 PM" etc.
Spring boot:
- Spring boot is (Spring + Configuration) bundle which helps you to develop application faster.
- Spring boot take care of many configurations and helps developer focus on business.
- It includes an embedded tomcat (or jetty) server.
Spring Boot and Quartz Scheduler in Action.
Download complete source code of Quartz Scheduler with Spring Boot example using below link,
https://github.com/javabypatel/spring-boot-quartz-demo
Example uses,
- Spring Boot 1.5.7
- Quartz 2.2.3
- PostgreSQL 9.4.1208
- Angular2
- Schedule Simple Quartz Job.
- Schedule Cron Quartz Job.
- Pause Quartz Job.
- Resume Quartz Job
- Edit Quartz Job.
- Delete Quartz Job.
- Unschedule Quartz Job.
- Stop Quartz Job.
- Check if Job is currently Running.
- Get the current state of Quartz Job.
- Get the list of all Scheduled Jobs.
- Job and Trigger Listeners for doing any operations before and after job starts, Listeners also hels in taking any action in case of job misfires due to application shutdown etc.
Quartz Scheduler Working.
Quartz work on concept of Jobs and Triggers.
- Job is anything that need to be executed when event occurs.
- Trigger is basically a event which contains time as when this event should occur.
There are 2 types of trigger,
- Simple Trigger
- Cron Trigger
For example, if you want the trigger to fire at exactly 11:23:54 AM on January 13, 2015, or if you want it to fire at that time, and then fire five more times, every ten seconds.
Cron Trigger: CronTrigger is often more useful than SimpleTrigger, It is used for scheduling jobs having complex time like if you need to execute job that recurs based on calendar-like notions, rather than on the exactly specified intervals.
With CronTrigger, you can specify firing-schedules such as "every Friday at noon", or "every weekday and 9:30 am", or even "every 5 minutes between 9:00 am and 10:00 am on every Monday, Wednesday and Friday during January".
Below are the few quartz methods that are used in example.
package com.javabypatel.demo.service; import java.util.Date; import java.util.List; import java.util.Map; import org.springframework.scheduling.quartz.QuartzJobBean; public interface JobService { boolean scheduleOneTimeJob(String jobName, Class<? extends QuartzJobBean> jobClass, Date date); boolean scheduleCronJob(String jobName, Class<? extends QuartzJobBean> jobClass, Date date, String cronExpression); boolean updateOneTimeJob(String jobName, Date date); boolean updateCronJob(String jobName, Date date, String cronExpression); boolean unScheduleJob(String jobName); boolean deleteJob(String jobName); boolean pauseJob(String jobName); boolean resumeJob(String jobName); boolean startJobNow(String jobName); boolean isJobRunning(String jobName); List<Map<String, Object>> getAllJobs(); boolean isJobWithNamePresent(String jobName); String getJobState(String jobName); boolean stopJob(String jobName); }
Demo application also explains Quartz listeners,
There are 2 types of listeners in quartz.
- Quartz Job Listeners.
- Quartz Trigger Listeners.
If you want to perform any task before job starts or when job completes or when trigger got misfired due to application shutdown, Quartz listeners will be helpful.
Download complete source code from here
Quartz Scheduler + Spring Boot + Angular2 Project Setup in Eclipse.
- Download the project from github.
- Import the project in Eclipse using "Import" > "Existing Maven Project".
- Once import is done properly and all dependencies are downloaded, project will look like below,
Running application in your Environment
After setting project in Eclipse,
- Install PostgreSQL database.(If you are planning to use other database then make respective changes in pom.xml for loading driver and in application.properties file for providing database path)
- Create database name "quartzdemoapp" (if you want to use any other name then give the same database name in application.properties file by replacing "quartzdemoapp" by that name)
- Open command prompt in admin mode.
- Go to "ui-app" folder and execute "npm install" command as shown below, (Make sure you have node and npm installed.)
- Go to "ui-app" folder and execute "npm run server" command as shown below,
This step will start server and ui-app will be deployed. - Open the browser and hit http://localhost:8080/ you will see the application UI as shown above which will be used to schedule both simple and cron quartz job.
- Start the Spring-boot application by clicking on "SpringBootQuartzAppApplication.java" > Right click, Run as, "Java Application"
Note: Server will be hosted on localhost and port as 7080. (http://localhost:7080/)
Client application is listening on port 8080 and Server application is listening on port 7080.
If you don't want to run 2 application separately, then transpile the typescript files to javascript and put those files in your web application as shown below,
For converting typescript files to javascript files,
- Open command prompt, navigate to ui-app folder and run "npm run build".
- After above step you will see "target" folder created inside "ui-app" folder, copy those files to your web application and run the server application.
Configuration files.
There are 3 configuration files,
- application.properties
- data.sql
- quartz.properties
application.properties :
It contains configuration for server and database as shown below,
server.port=7080 //Server will be listening on port 7080 spring.application.name=Scheduledemo ####### SPRING JPA ############ spring.jpa.database=POSTGRESQL spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=create-drop ####### SPRING JPA ############ ####### POSTGRES ############ spring.datasource.driver-class-name=org.postgresql.Driver #create the database by name "quartzdemoapp", here I am using postgresql running on port 5432. #if you want other database, change the url. spring.datasource.url=jdbc:postgresql://localhost:5432/quartzdemoapp #put your database username below spring.datasource.username=postgres #put your database password below spring.datasource.password=admin ####### POSTGRES ############
data.sql :
Quartz require jobs and triggers to be stored in database.
data.sql contains SQL scripts that creates tables required by quartz to manage scheduler.
quartz.properties :
org.quartz.scheduler.instanceName=springBootQuartzApp org.quartz.scheduler.instanceId=AUTO org.quartz.threadPool.threadCount=5 //number of concurrent jobs that can be run. org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PostgreSQLDelegate org.quartz.jobStore.useProperties=true org.quartz.jobStore.misfireThreshold=60000 //if job is delayed by 1 minute, don't consider it as misfire. org.quartz.jobStore.tablePrefix=qrtz_ //prefix used for all quartz related tables org.quartz.jobStore.isClustered=false org.quartz.plugin.shutdownHook.class=org.quartz.plugins.management.ShutdownHookPlugin org.quartz.plugin.shutdownHook.cleanShutdown=TRUE
You may also like to see
Compress a given string in-place and with constant extra space.
Check whether a given string is an interleaving of String 1 and String 2.
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord.
Serialize and Deserialize a Binary Tree
Advanced Multithreading Interview Questions In Java
Enjoy !!!!
If you find any issue in post or face any error while implementing, Please comment.
Post a Comment