Unable to generate the cron expression using cron-utils - java

I am using cron-utils jar for generating cron expression
0 0 8 ? 1/1 5#3 *
3rd Thursday of every month at 8 o' clock
Below is my code for above expression:
CronBuilder withMonth = CronBuilder.cron(
CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ)
).withYear(always()).withDoM(QuestionMark.questionMark())
.withMonth(on(3, SpecialChar.HASH));
withMonth.withDoW(on(5)).withHour(on(8)).withMinute(on(0)).withSecond(on(0));
Cron instance = withMonth.instance();
System.out.println(instance.asString());
But I'm getting the following exception:
value missing for a#b cron expression

finally found solution
.withDoW(on(NUM_DAY_WEEK,SpecialChar.HASH, NUM_DAY_MONTH);
it will generate like 5#3

Related

AWS cron expression to run a job every 2 days once

I have a requirement to schedule a job in AWS CloudWatch events to be run once in every two days. I am using the below expression but getting a error Parameter ScheduleExpression is not valid
cron(0 0 */2 * ? *)
The below is the java code,
String cronExpression = "cron(0 0 */2 * ? *)"
PutRuleRequest request = new PutRuleRequest();
request
.withName(eventName)
.withRoleArn("arn:aws:iam::****")
.withScheduleExpression(cronExpression)
.withState(RuleState.ENABLED);
PutRuleResult result = cloudwatchConfig.cloudwatch().putRule(request);
cron(0 0 1/2 * ? *)
You can verify here.
If you put your syntax into cloudwatch, it too will report the same error you are seeing in the terraform,but fix it simple
cron(0 0 1-31/2 * ? *)
The explanation for each field is here: https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html
1-31 covers all the possible number of days in a month

Quartz CronScheduleBuilder.atHourAndMinuteOnGivenDaysOfWeek don't working

I needing to use Quartz to execute a job. and when I try create a trigger on this mode :
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(identityTRIGGER, "grupo 01") .withSchedule(CronScheduleBuilder.atHourAndMinuteOnGivenDaysOfWeek(11, 20, DateBuilder.THURSDAY)).build();
don´t fired at 11:20 on thursday.
I´ve tried to do (with cron expression):
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(identityTRIGGER, "grupo 01").withSchedule(CronScheduleBuilder.cronSchedule("0 20 11 * * 5 *") ).build();
and don't work eigther- shows the following message:
Caused by: java.text.ParseException: Support for specifying both a day-of-week AND a day-of-month parameter is not implemented
**OBS: If I use another chron expression (like "0/10 * * * * ?") this work **
what´s wrong with my trigger ?
tnx advanced
To prevent exception "ParseException: Support for specifying both a day-of-week AND a day-of-month parameter is not implemented" do not use the * (all) value for day-of-month, but you could use ? (any) instead.
This is the strange implementation limitation of quartz.
So, you could use expression
0 20 11 ? * 5 *

Daily Cron expression Required

I want to create Cron expression for 5 days and run every day from starting date.
start date :- 30-04-2017
and
End date:- 03-05-2017
Run every day at 1:00PM
try following lines of code,
SimpleTriggerFactoryBean simpleTriggerFactoryBean=new SimpleTriggerFactoryBean(); simpleTriggerFactoryBean.setJobDetail(job.getObject()); simpleTriggerFactoryBean.setStartTime(startDateTimeInMillis); simpleTriggerFactoryBean.setRepeatInterval(24*60*60*1000); simpleTriggerFactoryBean.setRepeatCount(5); simpleTriggerFactoryBean.setName("name"); simpleTriggerFactoryBean.afterPropertiesSet();
I hope,this will resolve your problem.

How to validate CronSequenceGenerator cron expressions?

How could I validate cron expressions that are prepared for use of CronSequenceGenerator?
I mean, I cannot wait until the cron executes automatically as I'm defining like monthly intervals.
Is the following correct? How can I be sure?
monthly at midnight: `0 0 0 1 * *`
monthly at 1 am: `0 0 1 1 * *`
weekly, on sunday at midnight: `0 0 0 * * SUN`
The Spring's CronSequenceGenerator
class has a method
isValidExpression(String expression)
which takes the cron expression and returns a boolean.
I guess if you are ok with quartz api then you should use org.quartz.CronExpression.isValidExpression(String s)
Api ref http://quartz-scheduler.org/api/2.2.0/org/quartz/CronExpression.html
Actually I prefer to directly use "new CronSequenceGenerator(cronExpression)" instead of the static method isValidExpression.
Doing the new directly gives you a java.lang.IllegalArgumentExpression with a message telling you where the problem lies.
Unfortunately "isValidExpression" chokes the exception and simply returns false.

Java Scheduler Quartz Cron Trigger Time Setting

I am using Quartz for Scheduling my job in java. I have used "CronTrigger" for setting my time.
I want to fire my Job each day at 11:55 Pm in night. What should i write in the setCronExpression(" ") for having my Job Done. .??
What i thought of the Code is:---
CronTrigger trigger = new CronTrigger();
trigger.setName("runMeJob");
trigger.setCronExpression("0 55 23 * * ?");
Is the above code correct or should i do some modifications in it????
It would be: 0 55 23 1/1 * ? *
There is a nice website exactly for your case: CronMaker
CronMaker is a utility which helps you to build cron expressions.
CronMaker uses Quartz open source scheduler. Generated expressions are
based on Quartz cron format.

Categories