How to dynamically add a Quartz Job in JBoss6

落花浮王杯 提交于 2019-12-08 05:13:36

问题


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/tutorial/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>

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/6161555/how-to-dynamically-add-a-quartz-job-in-jboss6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!