Quartz Scheduler在为单个作业设置多个触发器时出现问题

时间:2016-02-09 10:58:37

标签: java maven cron quartz-scheduler

我正在尝试为单个作业创建多个触发器。在我使用scheduler.scheduleJobs(triggerAndJobs, true);之前,一切似乎都很好 Maven显示构建失败,错误为1,警告为0,没有进一步的信息。可能是失败的原因是什么?

 public void createControlTask(String name, String id, String freq) throws SchedulerException {
    JobBuilder jobBuilder;
    JobDetail jobDetail;
    TriggerBuilder trigBuilder;
    Trigger trigger = null;
    CronTrigger trig;
    String cronFreq;
    List triggerList = new ArrayList();
    Map<JobDetail,List<Trigger>> triggerAndJobs = new HashMap<JobDetail, List<Trigger>>();

    log.info("in createSchedulerTask() for : " + name);
    if ((freq != null) && !(freq.equals("As needed"))) {

        jobDetail = JobBuilder.newJob(QuraJob.class)
                  .withIdentity(name + "_JOB", Constants.CONTROL_TASK)
                  .usingJobData(Constants.NAME, name)
                  .usingJobData(Constants.ID, id)
                  .usingJobData(Constants.FREQUENCY, freq).build();

        if(freq.equals(Constants.MONTHLY)){
            String[] months = Constants.MONTHLY_PERIOD;
            for(int i = 0; i < months.length; i++)
            {
                String cron_for_month = props.getProperty("quartz.cron."+months[i]);

                trig = (CronTrigger) TriggerBuilder
                           .newTrigger()
                           .withIdentity(name + "_TRIGGER", Constants.CONTROL_TASK)
                           .withSchedule(CronScheduleBuilder.cronSchedule(cron_for_month))
                           .build();

                triggerList.add(trig);
            }
        }
        triggerAndJobs.put(jobDetail, triggerList); 
        if (!scheduler.isStarted()) {
               scheduler.start();
        }
        //scheduler.scheduleJobs(triggerAndJobs, true);
        //map.put(jobDetail, triggerList);
        //scheduler.scheduleJob(map, true);

    } 
    else
        log.info("Freq [" + freq + "] is not a schedulable value. So not scheduling a task for this control");
}

错误如下:

 Compiling 63 source files to C:\Users\Rahul\git\et\datacollector\target\classes
 [DEBUG] incrementalBuildHelper#afterRebuildExecution
 [INFO] -------------------------------------------------------------
 [ERROR] COMPILATION ERROR : 
 [INFO] -------------------------------------------------------------
 [ERROR] Found 1 error and 0 warnings.
 [INFO] 1 error
 [INFO] -------------------------------------------------------------
 [INFO] ------------------------------------------------------------------------
 [INFO] Reactor Summary:
 [INFO] 
 [INFO] quadrisk-parent .................................... SUCCESS [  2.765 s]
 [INFO] data-collector ..................................... FAILURE [ 24.602 s]
 [INFO] ------------------------------------------------------------------------
 [INFO] BUILD FAILURE
 [INFO] ------------------------------------------------------------------------
 [INFO] Total time: 29.138 s
 [INFO] Finished at: 2016-02-09T17:25:39+05:30
 [INFO] Final Memory: 16M/80M
 [INFO] ------------------------------------------------------------------------
 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project data-collector: Compilation failure
[ERROR] Found 1 error and 0 warnings.
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project data-collector: Compilation failure
Found 1 error and 0 warnings.

at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at    org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
at    org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:862)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:286)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:197)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.compiler.CompilationFailureException: Compilation failure
Found 1 error and 0 warnings.

at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:858)
at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:129)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
... 20 more
[ERROR] 
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :data-collector

1 个答案:

答案 0 :(得分:1)

因此,最终需要为单个石英作业创建多个触发器。所以下面的代码帮助我做到了。

public void createControlTask(String name, String id, String freq) throws SchedulerException {
    JobBuilder jobBuilder;
    JobDetail jobDetail;
    TriggerBuilder trigBuilder;
    Trigger trigger = null;
    CronTrigger trig;
    Set<Trigger> triggerList = new HashSet<Trigger>();
    String[] months = null;
    String cronFreq;
    log.info("in createSchedulerTask() for : " + name);
    if ((freq != null) && !(freq.equals("As needed"))) {
        jobBuilder = JobBuilder.newJob(QuraJob.class);
        jobBuilder = jobBuilder.withIdentity(name + "_JOB", Constants.CONTROL_TASK);
        jobBuilder = jobBuilder.usingJobData(Constants.NAME, name);
        jobBuilder = jobBuilder.usingJobData(Constants.ID, id);
        jobBuilder = jobBuilder.usingJobData(Constants.FREQUENCY, freq);
        jobDetail = jobBuilder.build();
        log.info("Job created -- [" + jobDetail.getKey() + "]");
        if(freq.equals(Constants.MONTHLY)){
            months = Constants.MONTHLY_PERIOD;
        }
        if(freq.equals(Constants.QUARTERLY)){
            months = Constants.QUARTERLY_PERIOD;
        }
        if(freq.equals(Constants.ANNUALLY)){
            months = Constants.ANNUALLY_PERIOD;
        }
        if(freq.equals(Constants.HALF_YEARLY)){
            months = Constants.HALF_YEARLY_PERIOD;
        }
        for(int i = 0; i < months.length; i++){
            cronFreq = props.getProperty("quartz.cron."+months[i]);
            trigBuilder = TriggerBuilder.newTrigger();
            trigBuilder = trigBuilder.withIdentity(name + "_TRIGGER_" + i, Constants.CONTROL_TASK);
            trigBuilder = trigBuilder.startNow();
            trigBuilder = trigBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cronFreq));
            trigBuilder = trigBuilder.forJob(jobDetail);
            trigger = trigBuilder.build();  
            triggerList.add(trigger);
        }
        if (!scheduler.isStarted()) {
               scheduler.start();
        }
        if(triggerList.size() > 0){
            scheduler.scheduleJob(jobDetail,triggerList,true);
            for(int j=0;j<scheduler.getTriggersOfJob(jobDetail.getKey()).size();j++){
                log.info((scheduler.getTriggersOfJob(jobDetail.getKey()).get(j)).getNextFireTime()+ "" );
            }
        }
    }
    else
        log.info("Freq [" + freq + "] is not a schedulable value. So not scheduling a task for this control");
}