Integrated Quartz Job in Spring MVC

Wednesday, January 6th, 2016 (4 Comments)

If your applications have the task needs to do at a certain time, or if your system has the job of routine maintenance is necessary to install auto scheduled job run –> Quartz may be solution for your ideal.

What is Quartz ?

Quartz is an open source full featured, service planning can be integrated or used with any Java application – from the smallest independent application to the trading system the largest e-commerce. Quartz can be used to create simple schedule or complex to implement dozens, hundreds, or even tens of thousands of jobs, the jobs are identified as components of the Java standard is programmed to perform fully comply with the requirements of the application. Quartz Scheduler includes many advanced features, such as support for transactions and subheadings.

To begin to familiarize with Quartz framework, you should see the series of Spring MVC to understand and know about Spring MVC and how to configure a project.

The following example, I will guide you to use quartz job in Spring MVC, we’ll create a job that runs every 10 seconds, it will out a string line.

1. You need to create a project structure is as follows:

quartz job

quartz job

2. Config maven

You need to declare the quartz in maven libraries and add the following code in the file pom.xml

3. Config quartz

You need to create one XML file to configure the parameters for quartz job. Here I create files with the name servlet-quartz.xml and add the following in the file:

  • In this file, there are two ways to declare the job to run repeats after 10s, I had noted that detail.
  • Line 33: Your job is not set automatically when deploying start web that to press “run job” on the site. If you want to start automatically the “autoStartup” set to true.

servlet-context.xml file:

4. Add quartz into project

Next you need to load a configuration quartz file into the system, add the following code in the file web.xml

5. Create task in quartz

Create a Java class to execute the job you want, here I give the job to run every 10 seconds it will display the current time. Create the following MyTask.java class

6. Manage task of quartz

This step, you will modify the controller in file HelloWorldController.java to manage the page hello.jsp file

  • Line 27: Declaring schedulerFactory represent for org.springframework.scheduling.quartz.SchedulerFactoryBean class that it is configured in the servlet-quartz.xml file. Through an schedulerFactory to manage starting or stopping the job on screen page

7. Page screen for end user

Finally you edit the page hello.jsp to end users manipulate on screen

8. Page index to redirect to hello page

Build and run project: you check in Console screen  of eclipse or server.log file in log folder of jboss, you will see result after every 10 seconds then output is :

Current Time GGGGGGGGGGGGGG: <current time>

We can see How to create quartz spring dynamic in database

Tags: , , , ,

Related Posts

spring
How to create quartz spring dynamic in database
spring
Spring mvc mongodb crud users roles
spring
Java spring mvc tutorial on lvtutorial.com
spring
Angularjs spring mvc crud example

Related Posts

spring
How to create quartz spring dynamic in database
spring
Spring mvc mongodb crud users roles
spring
Java spring mvc tutorial on lvtutorial.com
spring
Angularjs spring mvc crud example

4 Comments

  1. Ian

    Thanks for this. Could you provide an example of what should be in the servlet-context.xml as well?

  2. James

    Hello. The image in step 1 for the quartz job project structure is missing. Can you please fix it so I can make sure my structure is correct? Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *