Disable Quartz startup on first scheduale - java

I am using Quartz as followed:
schedulerFactory = new StdSchedulerFactory();
scheduler = schedulerFactory.getScheduler();
JobDetail startECMSJob = new JobDetail("startECMSJob", "group1", StartECMSJob.class);
Trigger trigger = TriggerUtils.makeMinutelyTrigger(30);
trigger.setName("TriggersGroup1");
trigger.setGroup("group1");
scheduler.scheduleJob(startECMSJob, trigger);
scheduler.start();
Problem is that Quartz starting straight away on deploy. I do want it to start only 30 mins after deploy.
same thign when I rescehduale it. I dont want it to start straight away as it rescheduale.
rescheduale code:
//JobDetail startECMSJob = new JobDetail("startECMSJob", "group1", StartECMSJob.class);
JobDetail jobDetail=jobContext.getJobDetail();
Trigger trigger = TriggerUtils.makeSecondlyTrigger(30);
trigger.setName("aa");
trigger.setGroup("group1");
trigger.setJobName(jobContext.getJobDetail().getName());
trigger.setJobGroup(jobContext.getJobDetail().getGroup());
Scheduler scheduler = jobContext.getScheduler();
scheduler.rescheduleJob("TriggersGroup1", "group1", trigger);
any idea how can i choose the first trigger?
thanks,
ray.

trigger.setStartTime(new Date(System.currentTimeMillis() + 30 * 60 * 1000));

Related

How to correctly schedule this 5 minutes Spring Batch Job? Why is it starting immediatly instead of waiting for the set time?

I am working on a Spring Batch application and I am finding some difficulties trying to correctly schedule a job.
I have this class where my job was scheduled:
/**
* This bean schedules and runs our Spring Batch job.
*/
#Component
#Profile("!test")
public class SpringBatchExampleJobLauncher {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringBatchExampleJobLauncher.class);
#Autowired
#Qualifier("launcher")
private JobLauncher jobLauncher;
#Autowired
#Qualifier("updateNotaryDistrictsJob")
private Job updateNotaryDistrictsJob;
#Autowired
#Qualifier("updateNotaryListInfoJob")
private Job updateNotaryListInfoJob;
#Scheduled(cron = "0 */5 * * * *")
public void runUpdateNotaryDistrictsJob() {
LOGGER.info("SCHEDULED run of updateNotaryDistrictsJob STARTED");
Map<String, JobParameter> confMap = new HashMap<>();
confMap.put("time", new JobParameter(System.currentTimeMillis()));
JobParameters jobParameters = new JobParameters(confMap);
try {
jobLauncher.run(updateNotaryDistrictsJob, jobParameters);
}catch (Exception ex){
LOGGER.error(ex.getMessage());
}
}
}
As you can see on my runUpdateNotaryDistrictsJob() method I set this Spring CRON expression:
#Scheduled(cron = "0 */5 * * * *")
in order to start my updateNotaryDistrictsJob every 5 minutes.
The problem is that when I run my application in debug mode I can see that the job is immediately performed (it stop on the first breakpoint). It seems that it is not waiting the 5 minutes set by the cron expression.
What is wrong? How can I try to solve this issue?
The cron expression 0 */5 * * * * does not read as you expect. "It seems that it is not waiting the 5 minutes set by the cron expression.", this is not what the cron expression defines. The cron expression will run at second 0, every 5 minutes starting at minute 0, of every hour, which means that it will never wait 5 minutes after the service has started. As an example, if you started it at 10:22, it will run at 10:25.
If you really need it to wait 5 minutes after service has started, you should consider using #Scheduled(fixedRate = 5000, initialDelay = 5000).

Quartz - Spring - Java - Jobstore. how to set job in completed state

I ask for help, I have jobs scheduled through quartz in a job shop, everything works some jobs that call procedures on Oracle. I would like to put the job in a complete state when a procedure returns a certain result. I can pause the job but not in completed.
if(!result.substring(0,4).equalsIgnoreCase("null")) {
fase_1.callJobResult("JOB_RESULT", key.getName().toString(), key.getGroup().toString(), strJobex+":"+parameter, result);
}else {
// I pause the job because it has been completed
try {
fase_1.callJobResult("JOB_RESULT", key.getName().toString(), key.getGroup().toString(), strJobex+":"+parameter,"SUCCESS. Il job : " + key.getName().toString().toUpperCase() + " job completed");
System.out.println(result);
//Scheduler scheduler;
SchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
JobKey jobKey = new JobKey(key.getName().toString(), key.getGroup().toString());
//scheduler.pauseJob(jobKey); // example this work
Trigger trigger = jobExecutionContext.getTrigger();
//Trigger.CompletedExecutionInstruction.NOOP.SET_TRIGGER_COMPLETE()
TriggerListner tl = new TriggerListner();
tl.triggerComplete(trigger, jobExecutionContext, CompletedExecutionInstruction.SET_TRIGGER_COMPLETE);

Java - Quartz CronSchedule doesn't Work with start date and end date

When I set the startDate and endDate to Trigger, the ChronSchedule call the execute method on the job only once.
JobDetail job = JobBuilder.newJob(service)
.withIdentity(batchService.getName(), CommonConstants.BATCH_JOB_GROUP).build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity(batchService.getName(), CommonConstants.BATCH_JOB_GROUP)
.startAt(startDate)
.endAt(endDate)
.withSchedule(
CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
.build();
System.out.println("##########" + trigger.getEndTime());
System.out.println("##########" + trigger.getStartTime());
As you can see I put some print on trigger for getting start date and end date.
It seems correct:
##########Fri Oct 20 16:00:00 CEST 2017
##########Fri Oct 20 15:00:00 CEST 2017
In the execute method, I put a print:
public void execute(JobExecutionContext context) throws JobExecutionException
{
System.out.println("Executing job. The next: " + context.getNextFireTime());
}
but the result is unconfortable:
Executing job. The next: null
What have I wrong?
I don't know about CronsScheduler or CronsTrigger but you can try following instead of CronScheduleBuilder.
import org.quartz.* Packages.
JobDetail jobDetail = JobBuilder.newJob(MYJOB.class).withIdentity(jobName).withDescription("TryThis").build();
CalendarIntervalTriggerImpl trigger = new CalendarIntervalTriggerImpl("triggername", startTime, endTime,
DateBuilder.IntervalUnit.valueOf("MINUTE"), frequency);
// Try to keep scheduler object as singultant object.
// OR it'll also work fine if you start scheduler it multiple times with diffrebt scheduler object for JDBCJob store.
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler scheduler = sf.getScheduler();
scheduler.start();
scheduler.scheduleJob(jobDetail, trigger);

How to fire a trigger every 30 seconds from 2 pm to 11 pm in quartz using Java?

I am using this statement -
trigger2 = TriggerBuilder.newTrigger()
.withIdentity("abc", "group1")
.withSchedule(CronScheduleBuilder
.cronSchedule("0/30 0 14-23 * * ?"))
.build();
Somehow the trigger is fired at 2 pm, 2:00:30 pm and no more.
What is the problem?
The problem is that you have put 0 in the minute field.So it fires only at 2:00.
Try
trigger2 = TriggerBuilder
.newTrigger()
.withIdentity("abc", "group1")
.withSchedule(
CronScheduleBuilder.cronSchedule("0/30 * 14-23 * * ?"))
.build();
From quartz scheduler documentation I extracted the following example:
Job #1 is scheduled to run every 20 seconds
JobDetail job = new JobDetail("job1", "group1", SimpleJob.class);
CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1", "group1", "0/20 * * * * ?");
sched.addJob(job, true);
Adapting to your situation it should go like this:
CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1", "group1", "0/30 * 14-23 * * ?");

Running two jobs with Quartz in Java

I have Quartz coded as follows and the first job runs perfectly:
JobDetail jd = null;
CronTrigger ct = null;
jd = new JobDetail("Job1", "Group1", Job1.class);
ct = new CronTrigger("cronTrigger1","Group1","0/5 * * * * ?");
scheduler.scheduleJob(jd, ct);
jd = new JobDetail("Job2", "Group2", Job2.class);
ct = new CronTrigger("cronTrigger2","Group2","0/20 * * * * ?");
scheduler.scheduleJob(jd, ct);
But I'm finding that Job2, which is a completely separate job to Job1, will not execute.
The scheduler is started using a listener in Java. I've also tried using scheduler.addJob(jd, true); but nothing changes. I'm running Java through a JVM on windows 7.
How do you know the job does not run? If you substitute Job1.class for Job2.class, does it still fail? When you swap order in which they're added to scheduler, or only leave Job2? Or if you strip down Job2 to only print a message to console?
I suspect Job2 execution dies with an exception.

Categories