At times, you may have wanted to perform some action periodically in your web application. Quartz is an enterprise grade scheduler which can be used for such a task. Read here for the complete list of features of Quartz. For using the Quartz scheduler library in a Java EE web application, following needs to be done:- Include the quartz jars (quartz-all.jar and others in the lib path). In my case, some of the commons-xxx.jar files were already included in the project due to the dependency of another library (displaytag) on those jar files. So in my quartz setup i had to disinclude them. In the lib/build path, i only included, jta.jar and also everything which was not already there in the project from lib/optional path too (they are not many anyway).
- We then had to create the tables required by quartz for storing job details and triggers across restart of application. This is an optional feature but an important one (which made us decide to use quartz in the first place over the JDK Timer). We used the docs/dbTables/tables_mysql.sql script to create the tables.
- Then we copied (example_quartz.properties), modified and saved the quartz.properties file in the project and changed the packaging settings to include the properties file in the WEB-INF/classes path in the IDE. In the properties file, we changed the configuration to have quartz point to the data store we created in step 2.
# Configuring Main Scheduler Properties
org.quartz.scheduler.instanceName = MyScheduler
org.quartz.scheduler.instanceId = 1
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
# Configuring ThreadPool
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 30
org.quartz.threadPool.threadPriority = 5
# Configuring JobStore
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.dataSource = quartzDS
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = false
# Configuring datasource
org.quartz.dataSource.quartzDS.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.quartzDS.URL = jdbc:mysql://localhost:3306/mydb
org.quartz.dataSource.quartzDS.user = me
org.quartz.dataSource.quartzDS.password = secret
org.quartz.dataSource.quartzDS.maxConnections = 31
# Rest of config was retained from example_quartz.properties.
org.quartz.scheduler.instanceName = MyScheduler
org.quartz.scheduler.instanceId = 1
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
# Configuring ThreadPool
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 30
org.quartz.threadPool.threadPriority = 5
# Configuring JobStore
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.dataSource = quartzDS
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = false
# Configuring datasource
org.quartz.dataSource.quartzDS.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.quartzDS.URL = jdbc:mysql://localhost:3306/mydb
org.quartz.dataSource.quartzDS.user = me
org.quartz.dataSource.quartzDS.password = secret
org.quartz.dataSource.quartzDS.maxConnections = 31
# Rest of config was retained from example_quartz.properties.
- We added following lines to our web.xml:
<description>Quartz Initializer Servlet</description>
<servlet-name>QuartzInitializer</servlet-name>
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
<init-param>
<init-param>
<load-on-startup>1</load-on-startup>
</servlet><servlet-name>QuartzInitializer</servlet-name>
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
<init-param>
<param-name>shutdown-on-unload</param-name>
<param-value>true</param-value>
</init-param><param-value>true</param-value>
<init-param>
<param-name>start-scheduler-on-load</param-name>
<param-value>true</param-value>
</init-param><param-value>true</param-value>
<load-on-startup>1</load-on-startup>
This sets up the initializer servlet which can initialize the default scheduler and start the scheduler at application bootstrap time.
- Now in our webservice/jsp/servlet of our web application we do the following:
try {
}
// A. Get the default scheduler.
Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
// B.Generate a unique name identifier for jobs and
// triggers of your application as required.
// One way is to use hashCode() if its a string param.
String dataToPass = "someParamToPassToJob";
int id = dataToPass.hashCode();
// C. Create/Replace a poll job and add it to the scheduler.
JobDetail job =
new JobDetail("job_"+id, "SomeJobGroup", com.mycompany.MyJob.class);
job.setRequestsRecovery(true);
// Pass data to the poll job.
job.getJobDataMap().put("param", dataToPass);
sched.addJob(job, true);
// D. Create a Trigger with unique name
SimpleTrigger trigger = new SimpleTrigger("trig_"+id, "SomeTriggerGroup");
// E. Check if a trigger is already associated with this job
// This step is optional and depends on your application's requirement.
Trigger[] jobTriggers;
jobTriggers = sched.getTriggersOfJob("job_"+id, "SomeJobGroup");
boolean isTriggerAlreadyAssociated = false;
for (Trigger trig : jobTriggers) {
// F. Associate this trigger with the job
trigger.setJobName(job.getName());
trigger.setJobGroup(job.getGroup());
// G. Initialize the trigger with duration and resolution to fire
trigger.setStartTime(startTime.getTime());
trigger.setEndTime(endTime.getTime());
trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger.setRepeatInterval(repeatInterval); //in milliseconds
if (isTriggerAlreadyAssociated) {
// Reschedule the job with the existing trigger.
sched.rescheduleJob("trig_"+id, "SomeTriggerGroup", trigger);
} else {
// Schedule the job with the new trigger.
sched.scheduleJob(trigger);
}
} catch (SchedulerException se) {Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
// B.Generate a unique name identifier for jobs and
// triggers of your application as required.
// One way is to use hashCode() if its a string param.
String dataToPass = "someParamToPassToJob";
int id = dataToPass.hashCode();
// C. Create/Replace a poll job and add it to the scheduler.
JobDetail job =
new JobDetail("job_"+id, "SomeJobGroup", com.mycompany.MyJob.class);
job.setRequestsRecovery(true);
// Pass data to the poll job.
job.getJobDataMap().put("param", dataToPass);
sched.addJob(job, true);
// D. Create a Trigger with unique name
SimpleTrigger trigger = new SimpleTrigger("trig_"+id, "SomeTriggerGroup");
// E. Check if a trigger is already associated with this job
// This step is optional and depends on your application's requirement.
Trigger[] jobTriggers;
jobTriggers = sched.getTriggersOfJob("job_"+id, "SomeJobGroup");
boolean isTriggerAlreadyAssociated = false;
for (Trigger trig : jobTriggers) {
if (trig.getName().equals("trig_"+id) &&
trig.getGroup().equals("SomeTriggerGroup")) {
}trig.getGroup().equals("SomeTriggerGroup")) {
// the job already has this trigger associated with it
isTriggerAlreadyAssociated = true;
}isTriggerAlreadyAssociated = true;
// F. Associate this trigger with the job
trigger.setJobName(job.getName());
trigger.setJobGroup(job.getGroup());
// G. Initialize the trigger with duration and resolution to fire
trigger.setStartTime(startTime.getTime());
trigger.setEndTime(endTime.getTime());
trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger.setRepeatInterval(repeatInterval); //in milliseconds
if (isTriggerAlreadyAssociated) {
// Reschedule the job with the existing trigger.
sched.rescheduleJob("trig_"+id, "SomeTriggerGroup", trigger);
} else {
// Schedule the job with the new trigger.
sched.scheduleJob(trigger);
}
}
- Of course, the last thing is to write the Job class which does the actual work. The following is code from examples of Quartz.
public class PrintPropsJob implements Job {
public PrintPropsJob() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException {
}}
public void execute(JobExecutionContext context)
throws JobExecutionException {
JobDataMap data = context.getJobDetail().getJobDataMap();
System.out.println("someProp = " + data.getString("someProp"));
System.out.println("someObjectProp = " + data.getObject("someObjectProp"));
}System.out.println("someProp = " + data.getString("someProp"));
System.out.println("someObjectProp = " + data.getObject("someObjectProp"));
- Some important points to note about jobs and triggers:
- Jobs have a name and group associated with them, which should uniquely identify them within a single Scheduler.
- A Job can be associated with multiple triggers.
- Triggers are the 'mechanism' by which Jobs are scheduled.
- Many Triggers can point to the same Job, but a single Trigger can only point to one Job.
- JobDataMap holds state information for Job instances. JobDataMap instances are stored once when the Job is added to a scheduler. They are also re-persisted after every execution of StatefulJob instances.
- JobDataMap instances can also be stored with a Trigger. This can be useful in the case where you have a Job that is stored in the scheduler for regular/repeated use by multiple Triggers, yet with each independent triggering, you want to supply the Job with different data inputs.
- The JobExecutionContext passed to a Job at execution time also contains a convenience JobDataMap that is the result of merging the contents of the trigger's JobDataMap (if any) over the Job's JobDataMap (if any).
- We can have different job types:
- Stateful Jobs - where state passed to job (in JobDataMap) is remembered (like static values) across executions of the job. Also, stateful jobs are not allowed to execute concurrently, which means new triggers that occur before the completion of the execute(xx) method will be delayed.
- Interruptable job - provide a mechanism for having the Job execution interrupted by implementing a callback method interrupt(), which will be called when scheduler's interrupt method is invoked on the Job.
That's all, in short, about how to integrate Quartz scheduler library in a web application.
23 comments:
Hi,
When Quartz is initialized thru initializer servlet, the factory object needs to be put in servlet context. Can i create scheduler object and put it in servlet context and down the line in app, i can simply get scheduler from the context and use it.
Can you give some code, how can we schedule single job with multiple trigger.
I am having trouble shutting down my scheduler whe using QuartzInitializerServlet.
I keep getting SchedulerException : Scheduler with name 'xyz' already exists.
Any ideas? Is this because of the web.xml entries for the QuartzInitializer.
In English class, one girl Cheap WoW Gold said never buy WoW Gold give up when discussing. It just reminded me wow goldsomething about myself.WoW Gold Maybe it is wow power leveling something about love.WoW Gold My love, which began on July 3rd of WoW Gold 2005 and finished on August wow leveling 23rd of 2008, taught me many things. wow leveling It is not a pleasant world of warcraft power leveling thing to look back on world of warcraft power leveling that. But I know I world of warcraft power leveling must learn something from it,world of warcraft leveling no matter what it is, world of warcraft leveling happiness or sorrow.Our wow gold love did not go on so smoothly and we went through many things.
Hi ,
Actually i have requirement to create scheduler for Report generation of Log files .Using Web app(jsp/servlet) .A user should be able to create schedule to generate report .So how can i perform this using Quartz .How can i customize this with User input and generate report .
thanks
China Highlights
China Highlights
China Tours
China Hotels
China Attractions
Beijing China Travel
Shanghai China Travel
Xi'an China Travel
Guilin China Travel
Yangshuo China Travel
Welcome to the 2moons dil, In here you can buy the 2moons gold, Do you know that the 2moon dil in the game is very important, If you had more cheap 2moons gold. I think you can get the tall level, quickly come here to buy 2moons dil.
You know ,I have some maple mesos,and my friend also has some
mesos,do you kouw they have the same meaning,Both of them can be called
maplestory mesos,I just want to
buy flyff penya ,because there are many
cheap mesos
You know ,I have some requiem gold,and my friend also has some
requiem lant,do you kouw they have the same meaning,Both of them can be called requiem money,I just want to
requiem online gold,because there are many
cheap requiem lant
I can getSword of the New World Vischeaply,
Yesterday i boughtSword of the New World Gold for my brother.
i hope him like it. i will give Sword of the New World money to him
as birthday present. i like the cheap snw vis very much.
I usually buy vis and keep it in my store.
i can get silkroad gold cheaply,
Yesterday i bought sro gold for my brother.
i hope him like it. i will give silkroad online gold to him
as birthday present. i like the cheap silkroad gold very much.
I usually buy the silk road gold and keep it in my store.
You know ,I have some Anarchy credits, and my friend also has some
Anarchy Online credits, do you know they have the same meaning,Both of them can be called
Anarchy gold,I just want to buy AO credits, because there are many cheap Anarchy online gold.
You know ,I have some Atlantica online Gold, and my friend also has some
Atlantica Gold, do you know they have the same meaning,Both of them can be called
Atlantica online money,I just want to
buy Atlantica online Gold, because there are many
cheap Atlantica online Gold.
I can get Solstice Kron cheaply.
Yesterday i bought Solstice Online Kronfor my brother.
i hope him like it. i will give Solstice Gold to him
as birthday present. i like the Solstice Online money very much.
I usually buy cheap Solstice Kron and keep it in my store.
I can get Megaten Gold cheaply.
Yesterday i bought Megaten online Gold for my brother.
i hope him like it. i will give Megaten money to him
as birthday present. i like the cheap Megaten Gold very much.
I usuallybuy Megaten Gold and keep it in my store.
I enjoy the Megaten online money.
rf gold which in RF Online Game is very popular for many players. Play this online game the premise that we have more enough rf online gold first. They are able to combine creative tools and weapons with some rf money and the Light form of universal magic. Under such sustained attacks they fell from power, yet they have bided their rf cp and time. Due to make cheap rf gold the intense gravity on their home planet, the Bellato are the smallest people.
runescape gold of RS Online Game, we have made several changes to Fun Orb. If you wish to buy runescape to explore other spell books, you should subscribe as a Fun Orb member. Shattered Plans - a galaxy-spanning strategy epic that use rs gold to allow up to six players to battle for supremacy. We only plan to pay cheap rs gold when we feel you will appreciate the updates. And we certainly feel that these changes and some runescape money are worth telling you about.
We have holic gold. He gave me some holic money, he said that I could buy holic online gold, but I did not have money, then I played it all my spare time. From then on, I got some cheap holic gold. We also sell holic online moneyI did not know how to get strong, someone told me that you must have last chaos gold. He gave me some lastchaos gold, he said that I could buy last chaos gold, but I did not have money, then I played it all my spare time. From then on, I got some lastchaos money, if I did not continue to play it, I can sell cheap lastchaos gold to anyone who want.
As a new player , you may need some game guides or information to enhance yourself.
knight gold is one of the hardest theme for every class at the beginning . You must have a good way to manage your knight noah.If yor are a lucky guy ,you can earn so many knight online gold by yourself . But if you are a not , I just find a nice way to get knight online noah. If you need , you can buy cheap knight gold at our website . Go to the related page and check the detailed information . Once you have any question , you can connect our customer service at any time .
Making Sho Online Mun is the old question : Honestly there is no fast way to make lots of Sho Mun . Sadly enough a lot of the people that all of a sudden come to with millions of Sho Online gold almost overnight probably duped . Although there are a lot of ways to make lots of Sho gold here I will tell you all of the ways that I know and what I do to buy Sho Online gold.
silkroad gold in the silkroad you may or may not notice. I know vitality is very tempting in sro gold. You may be feeling intelligence in silk road gold. You are going to be getting crab pincers which like silkroad online gold, depending on which server you are on. cheap silkroad gold can help you get a high level in short time.
Sword of the New World Vis in which world have one feature. First of all, realize the Sword of the New World Gold is useful to the each new player. You only play with others and buy vis can get happy from the game. You may found the cheap snw vis. However, Sword of the New World money can also save a lot your time.
Making requiem gold is the old question : Honestly there is no fast way to make lots of requiem lant . Sadly enough a lot of the people that all of a sudden come to with millions of requiem money almost overnight probably duped . Although there are a lot of ways to make lots of requiem online gold here I will tell you all of the ways that I know and what I do to buycheap requiem lant
As a new player , you may need some game guides or information to enhance yourself.
Rose zuly is one of the hardest theme for every class at the beginning . You must have a good way to manage your rose zulie.If yor are a lucky guy ,you can earn so many rose online zuly by yourself . But if you are a not , I just find a nice way to get rose online zulie. If you need , you can Arua ROSE zuly at our website . Go to the related page and check the detailed information . Once you have any question , you can connect our customer service at any time .
The Gameim.com is offering you with Scions Of Fate money and all kinds of cheap game gold. Scions Of Fate gold is sometimes need to pay for in this Scions of Fate game. For example in this game, we have to buy sof gold to exchange premium in order to protect our character. The only part of this game will require you to pay SOF gold but they are optional for you to choose in the premium store. One very common item bought with cheap SOF gold at the store would be a pet.
Once you get through the orientation you will be well on your way to exploring the ways of making linden dollars. buy lindens is the step process and is the required step for our players have to do. I think that it is right and required, because nothing is free and in particular the cheap linden in Second Life game. You do not have to spend second life linden, you can be as solitary as you want to be too. So instead I am going to give you some secondlife money and the tip of the iceberg.
Do you know the Archlord gold, in the game you need the
Archlord money. It can help you increase your level. My friends always asked me how to
buy Archlord gold, and I do not know he spend how much money to buy the
archlord online Gold, when I see him in order to play the game and search which the place can buy the
cheap Archlord gold. I am happy with him.
Do you know the cabal alz , in the game you need the
cabal gold. It can help you increase your level. My friends always asked me how to
buy cabal alz, and I do not know he spend how much money to buy the
cabal money, when I see him in order to play the game and search which the place can buy the
cabal online alz. I am happy with him.
I like play online game, I also buy Aion gold and Aion gold, the Aion china gold is very cheap, and use the Aion China kina can buy many things, I like Aion chinese gold, thanks, it is very good.
ITSolusenz departments manage all components web application, software development including, Application Development Company, software development company india, Software Development Services.
Post a Comment