Search code examples
javamavencronquartz-scheduler

Quartz Scheduler Issue in setting multiple triggers for a single job


I'm trying to create multiple triggers for a single job. Everything seems fine until I use scheduler.scheduleJobs(triggerAndJobs, true);
Maven shows build failure with 1 error and 0 warning and no further informations. What might be the reason for failure?

 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");
}

The error is like below:

 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

Solution

  • So, the ultimate need was to create multiple triggers for a single quartz job. So following code helped me do it.

    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");
    }