I want to create a scheduler with Quartz 2.2 in java dynamic web application. I am new to this task. I tried all the tutorials around the web. I trying context listener method to initialize the scheduler. It doesn't seem like working. The hello world program only works in general java application. for web application its looks tricky.
package com.test; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class HelloJob implements Job { public HelloJob() { } public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("Hello! HelloJob is executing."); } }
servlet.java package com.test;
import static org.quartz.JobBuilder.newJob; import static org.quartz.SimpleScheduleBuilder.simpleSchedule; import static org.quartz.TriggerBuilder.newTrigger; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.http.HttpServlet; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.Trigger; import org.quartz.impl.StdSchedulerFactory; public class MyServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1567185871113714035L; public void init(ServletConfig cfg) { String key = "org.quartz.impl.StdSchedulerFactory.KEY"; ServletContext servletContext = cfg.getServletContext(); StdSchedulerFactory factory = (StdSchedulerFactory) servletContext .getAttribute(key); // Scheduler quartzScheduler = factory.getScheduler("MyQuartzScheduler"); Scheduler sched; try { sched = factory.getScheduler("MyQuartzScheduler"); //sched = factory.getScheduler();//MyQuartzScheduler sched.start(); // define the job and tie it to our HelloJob class JobDetail job = newJob(HelloJob.class).withIdentity("myJob", "group1").build(); // Trigger the job to run now, and then every 40 seconds Trigger trigger = newTrigger() .withIdentity("myTrigger", "group1") .startNow() .withSchedule( simpleSchedule().withIntervalInSeconds(4) .repeatForever()).build(); // Tell quartz to schedule the job using our trigger sched.scheduleJob(job, trigger); } catch (SchedulerException e) { e.printStackTrace(); } } }
Web.xml:
quartz:shutdown-on-unloadfalsequartz:wait-on-shutdowntruequartz:start-scheduler-on-loadtruequartz:config-file/WEB-INF/quartz.properties org.quartz.ee.servlet.QuartzInitializerListener com.test.ApplicationStartupArchetype Created Web Application
I am using maven web app archtype.
回答1:
Contents
Eclipse project
With Maven
XML-Less
Eclipse project
If you are using a typical project in eclipse, the most basic example has a structure similar to:
Where the code of each of the files is as follows:
web.xml
org.quartz.ee.servlet.QuartzInitializerListener
quartz.properties
# ----------------------------- Threads --------------------------- # # How many jobs can run at the same time? org.quartz.threadPool.threadCount=5 # ----------------------------- Plugins --------------------------- # # Class to load the configuration data for each job and trigger. # In this example, the data is in an XML file. org.quartz.plugin.jobInitializer.class=org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
The job is executed every 5 minute(s) (see the expression 0 0/5 * 1/1 * ? * in the cron-expression tag). If you want another expression, you can build it with http://www.cronmaker.com/
TestJob.java
package org.paulvargas.test.quartz; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class TestJob implements Job { @Override public void execute(final JobExecutionContext ctx) throws JobExecutionException { System.out.println("Executing Job"); } }
log4j.xml
With Maven
If you are using Maven, the structure for the same project is:
To avoid conflicts, do not set the default listener in the web.xml at the same time. With this last example, the default number of threads is 10. Since the scheduler started in stand-by mode, it is necessary to call scheduler.start();. The "simple" identity is optional, but you can use it for reschedule the job (That's great!). e.g.: