| |
Struts Tutorials:
Struts2 Tag CheckBoxList , Checkbox, Iterator, IF
Struts2 Tag Library Example
Struts2 Tiles Example
Struts2 Tiles I18N Example
Struts2 Questions
Struts Tiles I18N Example
Struts Eclipse MVC
Struts2 Tags
Struts2 Example and Tutorial
Struts MVC
Struts2 Validation
Hibernate Tutorials:
Hibernate Case Study
Class Hierarchy Persist Example
Using Hibernate Interceptor
Hibernate Questions with Answer
Hibernate Many-to-Many Mapping Example
Hibernate one-to-many Mapping Example
Hibernate and ORM tools
Spring Hibernate Example
Hibernate SessionFactory Example
Hibernate Mapping Class Hierarchy
Hibernate Questions
Hibernate SessionFactory Questions
Spring Hibernate Example:
Spring Hibernate Case Study
Written By : Amit
Title :
Quartz Scheduler Example
Description :
Load on startup More...
|
Written By : Amit
Title :
Web Load Test
Description :
Grinder WEB Load Testing Framework More...
|
| Tags/Keywords : Quartz Scheduler, Quartz Java,Java Scheduler,Quartz cron,Quartz Job,Quartz Example, Scheduler Example, Quartz Scheduler Example, Java Scheduler Author : Amit Date (Year/Month/Date): 2010-05-07
Using Quartz as Scheduler for some dummy Job at some
Trigger timmers set in a predefined XML file | |
Please be informed that NONE of the design/code/matter from this page is claiming to be some
sort of best practice and we DO NOT expect any of our visitor/reader of this page to assume
this as some sort of best practice for any context and should not be using this as it is
without appropriate evaluation.
This page intends only to provide bits and pieces of known ways for doing some sort of example
and may not be fit for any other purpose.
In spite of all precautions taken to provide accurate and avoid any typo in these pages,
there might be some issues like grammatical mistakes and typo being observed in these pages,
We extend our sincerest apologies for the same.
A very simple example using Quartz as a scheduler in a
web application deployed onto Tomcat web server.
Quartz has a Servlet for initialization of scheduler
and it can be loaded on web server startup time by
setting load-on-startup tag in web.xml file of the
web application.
In this example I shall be adding various pieces of code
to come up with a working code for running a JOB at some
pre-defined time or on some reoccurring triggers.
So we may have to start with a Job and a Trigger, both having
same group, as defined in the related job definition XML file
such as shown below:
example_jobs.xml
....
....
<quartz>
<job>
<job-detail>
<name>example_job</name>
<group>example_group</group>
<job-class>example.jobs.ExampleJob</job-class>
</job-detail>
<trigger>
<simple>
<name>example_trigger</name>
<group>example_group</group>
<job-name>example_job</job-name>
<job-group>example_group</job-group>
<start-time>2010-05-07 9:35:00 pm</start-time>
</simple>
</trigger>
</job>
</quartz>
....
....
|
As you might have noticed quartz tag having job tag and trigger
tag within it. Job and Trigger name and group are to be defined.
I have given same name for the group for Job and Trigger.
Job class is having a custom user defined class with public
no-argument constructor, and this implements Job interface
from the Quartz framework.
In this example trigger is a simple type with the start time
as some predefined time for testing this example.
The Job class file is a dummy job with no task other than saying
some sys out println, as shown below:
ExampleJob.java
package example.jobs;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class ExampleJob implements Job {
public ExampleJob() {
System.out.println("This job constructor called...");
}
@Override
public void execute(JobExecutionContext context)
throws JobExecutionException {
System.out.println("inside execute method of example job");
}
}
|
I have defined the Quartz specific properties file as quartz.properties
...
...
org.quartz.scheduler.instanceName = ExampleScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 2
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.fileName = jobs/example_jobs.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
...
...
|
So atleast we should have a scheduler instance name with instance id
can be AUTO generated, a simple thread pool with some thread count.
There is a Job Initialization Plugin used as job intializer, that can
take a file name, in this case it is example_jobs.xml file.
and a provision for fail if this file not found.
This web application will not be having any UI interface , so the
Quartz specific servlet will not require any servlet mapping tag,
as shown below the WEB.XML file:
<web-app>
<servlet>
<servlet-name>quartz-loader-servlet</servlet-name>
<servlet-class>
org.quartz.ee.servlet.QuartzInitializerServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
|
After placing all the desired JAR file from the Quartz version
1.3.2, in the lib folder of this example, when web server started,
I observed two lines printed out on console as mentioned in the
Example JOB file in the constructor and the execute methods.
One interesting issue I encountered and how I resolve that is
discussed below:
When I used the standard DOCTYPE definition from Quartz Framework
with the DTD file as the following URL:
http://www.quartzscheduler.org/dtd/job_scheduling_data_1_0.dtd
in the JOB XML file, I encounted an exception while web server
starting up, so I changed it to only "job_scheduling_data_1_0.dtd"
and placed this dtd file in the TOMCAT bin folder. This resolved
the exception at server startup and the ExampleScheduler started
working as expected..
Any person having any other solution to this issue, please share
with me by using following comment section....
|
|
|
Commented By -> Suvna | Can you show an example of Quartz Job with Cron type
trigger?
Like the job should run every monday at 10:30 PM ... for example.
Thanks in advance |
| | |
Commented By -> Amit | In the above example, I can use CRON trigger to run this dummy
job by placing modified example_jobs.xml file as follows:
<quartz>
<job>
<job-detail>
<name>example_job</name>
<group>example_group</group>
<job-class>example.jobs.ExampleJob</job-class>
</job-detail>
<trigger>
<cron>
<name>example_trigger1</name>
<group>example_group</group>
<job-name>example_job</job-name>
<job-group>example_group</job-group>
<cron-expression>
0 30 22 ? * MON
</cron-expression>
</cron>
</trigger>
</job>
</quartz>
I have just replaced the simple trigger from this example
with a cron tag and related cron-expression.
This cron expression "0 30 22 ? * MON" triggers this dummy
job to run every Monday at 10:30 pm.
Only pain area is to test this functionality, as I had to
maually change my computer's system time, and run this sample
to be executed at 10:30 pm on any monday.
I am not aware if there is any automated ways of unit testing
this functionality, whether using JUnit or some other unit
testing framework.
If anyone having any answer to this, I will highly appreciate
if we can discuss those frameworks here in this thread.
Thanks anyways.
|
| | |
Commented By -> Sneha | Hi Amit..
if instead of the line sysout ,i wanna embedd my existing project of struts 2.which runs periodically,how do
i do it??plz gimme some idea |
| | |
Commented By -> Amit | Hi Sneha,
If I have understood your question properly:
If you are saying that there is a Struts2 Based
web application, in which there is a scheduled
job type of functionality is desired, and if this
is a separate process all together, then I don't
see any change in approach is required, one can
just use store or XML file for configuring Jobs
and those will be automatically triggered at
some pre-defined timings with the Quartz scheduler
loaded during web application loading/starting up.
If you still would like to explain your question
a bit, if this is not what is your question is???
then please write your question with some more
details (if you think you can). |
| |
|
|
| Home >>> Web Technology >>> Quartz Scheduler Example >>> Load on startup |
|
|
Visitor/User referred related external URL:
(Visible upon review and approved by this site Administrator)
|
|
|
|
|
<- requires login | Log in or Register | |
Copyright © 2008-2010, Interview-Questions-Tips-Forum, All Rights Reserved. | CONTACT PRIVACY POLICY DISCLAIMER |
| |  |
Oracle and Java are registered trademarks of Oracle and/or its affiliates.
Other names may be trademarks of their respective owners
This web site's Terms of Use and Disclaimer :
This web site provides some of the information about various technologies, example code, tips, tutorials etc. Like any printed materials,
content of these pages may become out of date over a period of time. Therefore all visitor/users of this web site are requested/advised
to refer to the originating parties/sources for the latest changes and happenings for detailed information. This information is not
intended to be a substitute for the original reference provided by the originating parties/sources. These examples with source code or
without source code, have not been thoroughly tested under all conditions. Interview-questions-tips-forum.net therefore, cannot guarantee
or imply reliability of these example source code or programs.
By accessing and using this website in any ways, including, without limitation, browsing the website pages, using any information, using
any content and/or downloading any materials, you agree to and are bound by the terms of use described in this page and Usage Terms and Conditions.
If you do not agree to all of the terms and conditions contained in the terms of use described in this page and
Usage Terms and Conditions, do not use this website in any manner. If you are using the website on behalf of your
employer, you represent that you are authorized to accept these Terms of Use on your employer's behalf.
All Trademarks are property of their respective owner. Appropriate measure is being taken for providing accurate and up-to-date
information but like any printed materials, these blog(s)/contents may eventually be outdated one day, so if you are using any of these
information, please refer original content/documentation from respective sources. And under no circumstances shall the Author of these
contents and/or this web site be liable for any loss, damage, expense incurred or suffered which is claimed to have occurred because of
usage of the contents of this web site. If you have any questions/queries/feedback/suggestions then please write to this web site owner
at contact.
|  |
|
|
|
|
|