0
votes

config.groovy-->

  Query
  {
      Map{
        time.'Tue Dec 30 14:48:00 EST 2014' =  ['T1']
        time.'Wed Dec 30 14:44:00 EST 2014'   =  ['T2']
        templates.'T1'   =  ['P1','P2','P3']
        templates.'T2'   =  ['Table']
         }    
  }
  Query
  {
       parameterValuesMap
       {
        parameterValues.'T1'   =  ['2014071600','2014072000','segment_id_file']
        parameterValues.'T2'   =  ['Elyon']
       }
  }

QuartzJob.groovy-->

import org.codehaus.groovy.grails.commons.GrailsApplication;

class MultipleJob 
{

    GrailsApplication grailsApplication;

    static triggers = {

       cron name: 'MultipleJobs', cronExpression: "* * * * * ?"
     }

     def execute() 
    {
        HashMap<String, String> parameter  =  new HashMap();
        grailsApplication.config.ais.Query.Map.time.each
        { k, v ->
              if(currentTime=="${k}")
              {
                      String templateId=v[0]
                      parameterKey = grailsApplication.config.Query.Map.templates.getAt("${v[0]}")
                      parameterValue = grailsApplication.config.Query.parameterValuesMap.parameterValues.getAt("${v[0]}")

                      for(int j=0;j<parameterKey.size();j++)
                      { 
                          parameter.put(parameterKey[j],parameterValue[j])
                      }

                      log.info(mediaQueryClient.executeQuery("0", templateId,"arbit", parameter))
              }    
        }

     }

}

I want to execute the same execute() method at different times for T1 and T2 at Tue Dec 30 14:48:00 EST 2014 and Wed Dec 30 14:44:00 EST 2014 respectively(I have a total of 25 such templates and all have a different time of execution which cannot be expressed by a single cron expression)can someone provide me with some sample code as to how can i keep executing them all at different time , i don'nt know what cron expression should i keep as the jobs may not be periodically apart hence i cannot have a general cron expression like every 15 minutes ? also can we create multiple cron expressions in a single quartz job ? please provide some sample code

1
You can have any number of triggers on a quartz job. So yes, you can have multiple. - Joshua Moore
Hi joshua ...thanks for the reply ..but how should i proceed with the above problem if i have 20 entries for each template which needs to be executed ...will i be creating 20 triggers for them ?i am new to quartz scheduler - elyon
Look at using jobManagerService.getQuartzScheduler().scheduleJob(jobTrigger) This way you can define your job without any triggers, and add them as you need (e.g. in BootStrap.groovy or in a service). - Joshua Moore
sorry i am new to groovy/grails.So what i understand is that i inject def jobManagerService in bootstrap.groovy and assign a corresponding trigger which i will write in bootstrap.groovy. Trigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerName,triggerGroupName).withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds()).forJob(JobKey.jobKey(jobName, groupName)).build() and then i call this from my quartz job like --> jobManagerService.getQuartzScheduler().scheduleJob(trigger); ? am i right - elyon
Once you .scheduleJob() Quartz will take care of triggering your jobs on the schedules you defined. - Joshua Moore

1 Answers

1
votes

It took your asking basically the same question twice for me to get it, but I think I see what you're trying to do.

You seem to have incorporated one or two of my suggestions from my answer to your previous question; feel free to accept it as the correct answer :) You really need to stop abusing GString expressions though - you're just making things harder for yourself.

I'd rework the Config.groovy stuff a bit:

Query {
   Map {
      time = [
         'Tue Dec 30 14:48:00 EST 2014': 'T1',
         'Wed Dec 30 14:44:00 EST 2014': 'T2'
      ]
      templates = [
         T1: ['P1','P2','P3'],
         T2: ['Table']
      ]
   }

   parameterValues = [
      T1: ['2014071600','2014072000','segment_id_file'],
      T2: ['Elyon']
   ]
}

and change the Quartz code to this:

class MultipleJob {

   def grailsApplication

   static triggers = {
      cron name: 'MultipleJobs', cronExpression: "* * * * * ?"
   }

   void execute() {

      def parameter = [:]

      def queryMap = grailsApplication.config.Query.Map
      def queryValues = grailsApplication.config.Query.parameterValues

      queryMap.time.each { String time, String templateId ->
         if (currentTime != time) {
            return
         }

         List parameterKeys = queryMap.templates[templateId]
         List parameterValues = queryValues[templateId]

         parameterKeys.size().times { int j ->
            parameter[parameterKeys[j]] = parameterValues[j]
         }

         log.info(mediaQueryClient.executeQuery("0", templateId, "arbit", parameter))
      }
   }
}