Reputation: 1237
I have requirement where I need to schedule a job in every 10 minutes, this job runes in simple web application and we can not use Spring. I wend through some of the tutorial but could not understand what do I need to do, can you please let me know the steps I need to follow?
I have some specific doubts:
quartz.properties
?quartz.xml
? Cant I do the same using some class/servlet?I got a standalone program which execute job, I can write that in init of any servlet and start that servlet on start of container <load-on-startup>1</load-on-startup>
. Is it right thing to do?
Upvotes: 2
Views: 6141
Reputation: 1957
public class HelloJob implements Job {
@Override
public void execute(JobExecutionContext context)
throws JobExecutionException {
//put your code here
}
}
Since you do not want to use xml and want java code instead you can get a StdSchedulerFactory instance from the ServletContext to configure the scheduler and in order for that code to be called on initialization you must put it in a listener:
public class HelloQuartzListener implements ServletContextListener {
private Scheduler scheduler;
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
@Override
public void contextInitialized(ServletContextEvent ctx) {
// define the job and tie it to our HelloJob class
JobDetail job = JobBuilder.newJob(HelloJob.class)
.withIdentity("myJob", "group1").build();
// Trigger the job to run now, and then every 10 minutes
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("myTrigger", "group1")
.startNow()
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInMinutes(10).repeatForever())
.build();
// Tell quartz to schedule the job using our trigger
try {
scheduler = ((StdSchedulerFactory) ctx.getServletContext()
.getAttribute(
QuartzInitializerListener.QUARTZ_FACTORY_KEY))
.getScheduler();
scheduler.scheduleJob(job, trigger);
} catch (SchedulerException e) {
}
}
}
In order to initialize quartz from your web app you need to configure QuartzInitializerListener by adding the following to your web.xml. Note that in the end we also added our own listener we created previously that configures the job via java code.It is important that it goes after the QuartzInitializerListener because QuartzInitializerListener needs to be called first so that it puts StdSchedulerFactory inside the context in order for HelloJobListener to get it :
<context-param>
<param-name>quartz:shutdown-on-unload</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>quartz:wait-on-shutdown</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>quartz:start-on-load</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<listener-class>org.quartz.ee.servlet.QuartzInitializerListener</listener-class>
</listener>
<listener>
<listener-class>yourpackage.HelloQuartzListener</listener-class>
</listener>
****** In case you do want to set your own property values, you can add the properties file and include its path by adding also this to your web.xml:
<context-param>
<param-name>quartz:config-file</param-name>
<param-value>/WEB-INF/quartz.properties</param-value>
</context-param>
****** .. and if you decide that you prefer to use xml, then you can specify that inside the properties file by adding the following:
org.quartz.plugin.jobInitializer.class =org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = quartz.xml
where "quartz.xml" will contain the job details (of course in that case remove the HelloJobListener configuration and class):
<job-scheduling-data
xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData
http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
version="1.8">
<schedule>
<job>
<name>HelloJob</name>
<group>group1</group>
<description>hello</description>
<job-class>yourpackage.HelloJob</job-class>
</job>
<trigger>
<cron>
<name>myTrigger</name>
<job-name>HelloJob</job-name>
<job-group>group1</job-group>
<!-- It will run every 10 minutes -->
<cron-expression>0 0/10 * * * ?</cron-expression>
</cron>
</trigger>
</schedule>
</job-scheduling-data>
Upvotes: 4