使用非周期性 cron 表达式运行作业 groovy

Running jobs with a non periodic cron expression groovy

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

我问了你两次基本相同的问题,但我想我明白你想做什么。

你似乎采纳了我在 my answer to your previous question 中的一两个建议;请随意接受它作为正确答案 :) 你真的需要停止滥用 GString 表达式 - 你只是让自己变得更难。

我会稍微修改 Config.groovy 内容:

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']
   ]
}

并将 Quartz 代码更改为:

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