How to dynamically add a Quartz Job in JBoss6
I'm using JBoss6 and want to dynamically create Quartz-Jobs. During the processing of the job the next start time will be defined (e.g. in 1, 5 or 10 hours).
I didn't find any solutions for this, it's even hard to get access to the org.quartz.Scheduler
(see QuartzScheduler injection in JBoss AS 6).
The next problem is the creation of new Jobs, I followed the tutorial http://www.quartz-scheduler.org/docs/tutori开发者_如何学编程al/TutorialLesson02.html:
import static org.quartz.JobBuilder.*;
import static org.quartz.SimpleScheduleBuilder.*;
import static org.quartz.CronScheduleBuilder.*;
import static org.quartz.CalendarIntervalScheduleBuilder.*;
import static org.quartz.TriggerBuilder.*;
import static org.quartz.DateBuilder.*;
// define the job and tie it to our HelloJob class
JobDetail job = newJob(HelloJob.class)
.withIdentity("myJob", "group1") // name "myJob", group "group1"
.build();
// Trigger the job to run now, and then every 40 seconds
Trigger trigger = newTrigger()
.withIdentity("myTrigger", "group1")
.startNow()
.withSchedule(simpleSchedule()
.withIntervalInSeconds(40)
.repeatForever())
.build();
// Tell quartz to schedule the job using our trigger
sched.scheduleJob(job, trigger);
But it seems the org.quartz.JobBuilder
is not available for JBoss6. If i manually add the quartz-dependency have errors on startup (class loading issues). This artifacts are defined (without explicitly using Quartz):
<dependency>
<groupId>org.jboss.jbossas</groupId>
<artifactId>jboss-as-client</artifactId>
<version>6.0.0.Final</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.jboss.security</groupId>
<artifactId>jbosssx-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.security</groupId>
<artifactId>jbosssx</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>1.0.0.Final</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
In JBoss 6 you can get at the Quartz scheduler using a factory class provided within the Quartz library. This should be all you need:
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
We use this within a context listener at start up to dynamically schedule jobs. HTH.
It seems you are following Quartz 2.0.x tutorial. Have you tried Quartz 1.x tutorial?
The version provided with JBoss 6 is Quartz 1.8.3, and there are significant API changes in Quartz 2.x.
精彩评论