How to start a spring data hadoop job later? - java

I'm new for SpringData Hadoop. I checkout some examples from www, such as [1]:https://github.com/pkainulainen/spring-data-apache-hadoop-examples/tree/master/mapreduce
All of them are configured to run at start-up:
<hdp:job-runner id="wordCountJobRunner" job-ref="wordCountJob" run-at-startup="true"/>
But I want to run the job via a Servlet. How to?

i think you have to try this:
http://quartz-scheduler.org/
examples of code you could see the official site, but here is some fragment:
// compute a time that is on the next round minute
Date runTime = evenMinuteDate(new Date());
// Trigger the job to run on the next round minute
Trigger trigger = newTrigger()
.withIdentity("trigger1", "group1")
.startAt(runTime)
.build();

Related

Quartz Scheduler getFinalFireTime null even with an expired cron

I'm using Quartz Scheduler in Java to schedule jobs based on entries in a database. I'm trying to automate disabling those entries when they will never run again, so I don't try to read them every time the application starts.
I have a cron for a very specific date, for example 0 25 12 4 DEC ? 2021. That is a specific date, it has passed, and cannot ever fire again.
I build a chrontrigger and check endTrigger.getFinalFireTime() and it returns null, which I believe should only happen when the cron allows future firing.
// Build the end cron trigger
CronTrigger endTrigger = TriggerBuilder.newTrigger()
.withIdentity("eventEndCron", "quartzGroup")
.withSchedule(CronScheduleBuilder.cronSchedule("0 25 12 4 DEC ? 2021"))
.forJob("eventEnd", "quartzGroup")
.build();
If I try to schedule the job, it throws an error Based on configured schedule, the given trigger 'quartzGroup.eventStartCron' will never fire.
Why isn't getFinalFireTime giving me a date? Is there some other approach I need to use?

Quartz scheduler incorrect schedule time

I have a spring boot application in which I am trying to schedule a job using quartz scheduler to run daily at a specific time of the day. The following is my code to build the trigger.
DailyTimeIntervalScheduleBuilder scheduleBuilder = DailyTimeIntervalScheduleBuilder
.dailyTimeIntervalSchedule()
.startingDailyAt(TimeOfDay.hourAndMinuteFromDate(activeStartTime))
.endingDailyAfterCount(1)
.withMisfireHandlingInstructionFireAndProceed();
MutableTrigger trigger = scheduleBuilder.build();
The problem I am facing is that the job is scheduled but starting from the next day. So for instance, if I schedule the job for May 22 16:45, then the first fire time for the job is set to May 23 16:45.
I have tried using the builder with withIntervalInHours(24) instead of endingDailyAfterCount(1), but the result is the same.
I am not sure what seems to be the problem.
Note: This behavior is the same regardless of when I schedule my job, i.e., it doesn't matter if I execute this code before or after 16:45, the job is always scheduled for the next day
I am using spring boot version 1.5.10 and spring-boot-starter-quartz version 2.2.5.RELEASE
Can you try below code
CalendarIntervalScheduleBuilder schedule = CalendarIntervalScheduleBuilder
.calendarIntervalSchedule()
.inTimeZone(TimeZone.getDefault())
.withIntervalInDays((int) 1)
.withMisfireHandlingInstructionFireAndProceed();
Trigger trigger = TriggerBuilder
.newTrigger()
.startAt(startDateTime)
.withSchedule(schedule).build();
For the field startDateTime please use current Date time. if you want to start from May 22 16:45 then create the Date object accordingly.
And set the timezone also, or it will pick default system's timezone.

Scheduled task using ScheduledExecutorService ? A good idea?

I need to execute some task daily. Can I use ScheduledExecutorService for this? Say a piece of code that is running always at particular interval. My doubt I am keeping CPU busy always running my code, but this code will run my actual task once in a day,week or month depends on configuration.
ScheduledExecutorService schedular = Executors.newScheduledThreadPool(1);
schedular.scheduleAtFixedRate(new MySchedular(), 1, 24, TimeUnit.HOUR);
Or do we have any better option?
For very a basic need you can indeed use a ScheduledExecutorService as you do, but if you want to do complex things like scheduling your task at a given hour in the day, you should consider using quartz-scheduler.
Here is how to schedule a task at 10 PM:
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobDetail job = JobBuilder.newJob(MySchedular.class)
.withIdentity("job1", "group1")
.build();
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger1", "group1")
.withSchedule(CronScheduleBuilder.dailyAtHourAndMinute(22, 00))
.build();
// Tell quartz to schedule the job using our trigger
scheduler.scheduleJob(job, trigger);
// and start it off
scheduler.start();
The same trigger created with a cron expression would be:
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger3", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0 22 00 * * ?"))
.build();
Considering that you are using Windows and CronJob is not available, ScheduledExecutorService is a very good choice on your part. The only problem you need to look into is what happens if for whatever reason your process terminates not due to Windows reboot. Usually critical systems have some sort of Watchdog processes that monitor your process and restart it if it was terminated. But this is probably out of scope for this question. In short the answer to your question is: ScheduledExecutorService is the correct choice.

Start Quartz job in five minutes

I would like to start a quartz job in five minutes. Be aware that I don't want to start the job EVERY five minutes but only AFTER five minutes.
Currently I'm using the following code but it does not work properly because the quartz job gets executed every 'intervalInMinutes' minutes:
MutableTrigger trigger = (MutableTrigger) TriggerBuilder.newTrigger()
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMinutes(intervalInMinutes).withRepeatCount(repeatCount)).build();
return new Rule(trigger);
Thanks for any help
Found answer. I used this code:
trigger = (SimpleTrigger) newTrigger()
.withIdentity("trigger5", "group1")
.startAt(futureDate(5, IntervalUnit.MINUTE)) // use DateBuilder to create a date in the future
.forJob(myJobKey) // identify job with its JobKey
.build();

Managing quartz jobs, deleting

Consider the following sequence of events. Using quartz 1.8.0
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.0</version>
</dependency>
I schedule a job to be executed in the future
job.setGroup(MY_GROUP);
Date date = scheduler.scheduleJob(job, trigger); // Valid date received
Job executes as expected.
I then try to delete the job by running
boolean unscheduled = scheduler.deleteJob(event.getName(), MY_GROUP); // Always false
Attempt to delete the job always results in **false**
If i let the application to run past the time it was scheduled to execute, after having failed to delete it, it ... does not run (as if it was deleted successfully)
What could explain such a behavior? How can i know what is scheduled in quartz as part of the group?
EDIT:
Trigger is set as:
SimpleTrigger trigger = new SimpleTrigger();
trigger.setStartTime(new Date(event.getStartTime().inMillis()));
trigger.setName("trigger" + event.getTriggerName());
trigger.setRepeatInterval(event.getFrequency());
trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
What trigger are you using? If you haven't specified that the trigger should fire multiple times e.g. on a recurring interval, then it will only fire once and will then be discarded; if your job detail isn't durable then the scheduler will automatically remove it once no more triggers point to it.

Categories