2011-12-19 4 views
5

Sto provando a scrivere un'espressione cron semplice per lo scheduler del quarzo. Voglio che il lavoro venga eseguito ogni mese il giorno 30 alle 3 del mattino.cron di quarzo - cosa succede se il giorno del mese non esiste?

0 0 3 30 JAN-DEC ? * 

Mi chiedo cosa succede per il mese di febbraio? Il lavoro verrà eseguito o non funzionerà?

Non sto cercando una soluzione per l'ultimo giorno del mese, ho bisogno che l'utente selezioni il giorno del mese in cui il lavoro verrà eseguito (idealmente una volta per tutti i mesi).

risposta

4

L ("ultimo") - ha un significato diverso in ciascuno dei due campi in cui è consentito. Ad esempio, il valore "L" nel campo giorno-mese indica "l'ultimo giorno del mese" - il giorno 31 per gennaio, il giorno 28 per febbraio negli anni non bisestili. Se usato nel campo del giorno della settimana da solo, significa semplicemente "7" o "SAT". Ma se usato nel campo del giorno della settimana dopo un altro valore, significa "l'ultimo xxx giorno del mese" - ad esempio "6L" significa "l'ultimo venerdì del mese". Quando si utilizza l'opzione "L", è importante non specificare elenchi o intervalli di valori poiché si ottengono risultati confusi.

È possibile utilizzare questa opzione per specificare direttamente anziché specificare 30 nel proprio lavoro.

http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger

Verificare la presenza di caratteri speciali.

Grazie.

+1

Beh, non mi interessa per eseguire il lavoro anche nell'ultima, o secondo l'ultimo giorno del mese. Ho solo bisogno che sia il 30 (se quel giorno esiste). – dcernahoschi

1

Non funziona. Se si desidera eseguire il 28 nel caso di febbraio, è necessario creare più CronExpressions per ogni caso di giorni nel mese e un trigger per ciascuno, quindi aggiungere tutti i trigger al lavoro richiesto.

Questo è quello che ho fatto: creazione

CronExpressions:

public static List<CronExpression> getCronExpressionList(int seconds, int minutes, 
      int hours, int dayInMonth, Month month, 
      DayOfWeek dayOfWeek) { 
    final String monthsWith30Days = Month.APR + "," + Month.JUN + "," 
        + Month.SEP + "," + Month.NOV; 
    List<CronExpression> crons = new LinkedList<CronExpression>(); 

    String timeString = String.format(("%s %s %s "), seconds, minutes, 
        hours, 0, 0, 0); 
    String dateString = "%s %s %s"; 
    String cron = null; 

    cron = timeString + String.format(dateString, dayInMonth, "*", "?"); 
    crons.add(new CronExpression(cron)); 
    if (dayInMonth > 28) { 
     String febCron = timeString + getFebruarLastDayDateString(dateString); 
     crons.add(new CronExpression(febCron)); 
     if (dayInMonth == 31) { 
      String monthsWithThirtyDaysCron = timeString + String.format(dateString, 
        "L", monthsWith30Days, "?"); 
      crons.add(new CronExpression(monthsWithThirtyDaysCron)); 
     } 
    } 
    return crons; 
} 

private static String getFebruarLastDayDateString(String initialCron) 
       throws ParseException { 
    return String.format(initialCron, "L", Month.FEB, "?"); 
} 

creazione di trigger:

 Set<CronTrigger> triggers = new HashSet<>(); 

     int i = 1; 
     for (CronExpression cronEx : cronsList) { 
      CronTrigger trigger = newTrigger() 
        .withIdentity("trigger" + i, groupName) 
        .withSchedule(cronSchedule(cronEx)) 
        .build(); 
       triggers.add(trigger); 
       i++; 
     } 
0

controllo semplice codice

public class TestCronTrigger { 

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); 

    public static void main(String[] args) throws Exception { 

     String dateStr = "2015-02-10"; 
     String cron = "0 0 0 31 * ?"; 
     Date nextFireTime = getNextFireTimeFromDateForCron(DATE_FORMAT.parse(dateStr), cron); 
     System.out.println(String.format("For cron '%s' next fire time after '%s' will be '%s'", cron, dateStr, DATE_FORMAT.format(nextFireTime))); 

     dateStr = "2015-02-10"; 
     cron = "0 0 0 30 * ?"; 
     nextFireTime = getNextFireTimeFromDateForCron(DATE_FORMAT.parse(dateStr), cron); 
     System.out.println(String.format("For cron '%s' next fire time after '%s' will be '%s'", cron, dateStr, DATE_FORMAT.format(nextFireTime))); 

     dateStr = "2015-02-10"; 
     cron = "0 0 0 28 * ?"; 
     nextFireTime = getNextFireTimeFromDateForCron(DATE_FORMAT.parse(dateStr), cron); 
     System.out.println(String.format("For cron '%s' next fire time after '%s' will be '%s'", cron, dateStr, DATE_FORMAT.format(nextFireTime))); 

     dateStr = "2015-03-10"; 
     cron = "0 0 0 31 * ?"; 
     nextFireTime = getNextFireTimeFromDateForCron(DATE_FORMAT.parse(dateStr), cron); 
     System.out.println(String.format("For cron '%s' next fire time after '%s' will be '%s'", cron, dateStr, DATE_FORMAT.format(nextFireTime))); 

    } 

    static Date getNextFireTimeFromDateForCron(Date from, String cron) throws ParseException { 
     CronTriggerImpl cronTrigger = new CronTriggerImpl(); 
     cronTrigger.setCronExpression(cron); 
     cronTrigger.setStartTime(from); 
     return cronTrigger.computeFirstFireTime(null); 
    } 
} 

Per questo codice l'outp ut sarà:

For cron '0 0 0 31 * ?' next fire time after '2015-02-10' will be '2015-03-31' 
For cron '0 0 0 30 * ?' next fire time after '2015-02-10' will be '2015-03-30' 
For cron '0 0 0 28 * ?' next fire time after '2015-02-10' will be '2015-02-28' 
For cron '0 0 0 31 * ?' next fire time after '2015-03-10' will be '2015-03-31' 

giocare con argomenti si possono trovare le risposte

+0

Questa è solo una risposta indiretta. Sarei fantastico se potessi aggiungere l'output che ottieni per i valori forniti dall'OP. Ciò renderebbe la tua risposta più completa. – honk