Java Executor in timerange at specific time - java

I need to call a task every x hour in a defined time range and repeat this every day.
My only idea is to calculate the executions times as delay from current time and start each in a seperated thread via scheduleAtFixedRate and a rate of 24 hours.
Do anyone has a better idea?
Regards

Use your machines in built task schedule to call a script and call your java program from that script.

You can use a library dedicated to schedule code: quartz
It use cron like syntax and can schedule Job implementations.

Related

How to schedule a camel route to start at specific time and run once in a day

I want to poll a directory at a specific time each day. Right now I am using timer component with period=86400000 to run periodically every 24 hrs. But I wanted to start the route at a specific time and run for only weekdays mon-fri and not on weekends. Any help would be appreciated. Thanks
You can use the camel-quartz2 component for this.
From Camel Docs:
USING CRON TRIGGERS Quartz supports Cron-like expressions for
specifying timers in a handy format. You can use these expressions in
the cron URI parameter; though to preserve valid URI encoding we allow
+ to be used instead of spaces.
For example, the following will fire a message every five minutes
starting at 12pm (noon) to 6pm on weekdays:
from("quartz2://myGroup/myTimerName?cron=0+0/5+12-18+?+*+MON-FRI")
.to("activemq:Totally.Rocks");
which is equivalent to using the cron expression
0 0/5 12-18 ? * MON-FRI
You can get more usages of the cron scheduler at http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/crontrigger.html

Dynamic Job Scheduling with Quartz or any other java api

I have an UI Interface where user can define Job name, interval, active/Inactive etc.
How we can achieve this with Quartz Scheduler or any java/Spring api ?
Ex. Suppose any Quartz job is started and interval is set as 10 min, So in ideal case job will run in next 10 min interval. But every time job runs we want to fetch the latest interval from database and schedule it.
10:00 Job runs and in the database interval is set to 10 min
10:10 Job runs and in the database interval is set to 20 min
So next time job should run at 10:30
If you use Quartz, you can implement a custom Trigger. Your implementation would lookup the value in the database and return when the next time the run should happen in the getFireTimeAfter.
Another option is to use Spring Scheduling APIs and implement the Trigger interface. Same here, the nextExecutionTime method would decide when the next run should happen.
The advantage of using a custom implementation is that you have full control over the triggering logic (like in your case, do a lookup in the database and dynamically set the next run time).

scheduling task execution in Java

How can I execute task in Java every x months?,
and how can I execute task in given days? (for example I want to execute my task every week in Sunday Tuesday and Saturday)
Thanks, Michal
You can do this thing easily with third party APIs. Quartz scheduler is one of it. Have a look over below links :
1) http://quartz-scheduler.org/
2) http://www.mkyong.com/tutorials/quartz-scheduler-tutorial/

How to configure a thread that execute on a given time

I am making a meeting broadcast application which will broadcast the message to the participant of meeting.
There is a meeting data checker thread which execute after 5 minute.
I have done this using this code :
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
scheduledExecutorService.scheduleWithFixedDelay(new DataAccessSchedule(), 0, 5,TimeUnit.MINUTES);
Now the in DataAccessScedule the query get the data of meeting which are expected to be started after 15 minutes,so I get the data before 15 minutes and the time in table are: 01:45 ,... 01:50 , ... 01:52 ,..
so i have calculated the time of all in millisecond by taking difference from current time so now the time in milis are x,y and z.
I want to fire the thread on x , y and z time and these thread destroy itself after execution.I do not want to run scheduler here like above did using Executors.
Please tell me how should i do that ?
You need a scheduler library for that, for example Quartz Scheduler, that supports cron expressions to specify exactly when the task should be launched.
You can also configure it to run the task finite number of times.
An alternative for writing sheduler library would be to write the similar functionality yourself. You'll need one task running periodically, with the precision you require (it could be 1 minute, but also 1 second) and checking all entries in the data structure, if their start condition matches current date (for example, by matching the date/time against cron expression). It would be, however, reinventing the wheel so don't do as long if you don't want some extra funktionality the existing libraries doesn't provide.

Quartz StatefulJob / non-StatefulJob

Would you please explain to me the exact mean of the StatefulJob in quartz and it's difference with none StatefulJob?
StatefulJob interface, provides 2 things,
first: only one job will be run any time
second: in (SimpleTriggerBean) you will not worry about your job running duration. it means that the next run will be done after delay time after ending of previous one.
StatefulJob guarantees only one job will be running at one time. For example, if you schedule your job to run every 1 minute, but your job took 5 minutes to complete, then the job will not be run again until the previous job has completed.
This is useful to make sure there is only one job running at any given time.
The next job will be run on the next schedule, not immediately after the previous job completed.
jobDetail.getJobDataMap().put("type","FULL");
This line is will decide we are using statefull or non-statefull.
If we are passing the argument then it will be statefull.
With out statefull there is no way to pass the arguments in execute method
In state full while execution time if we modify any value then the execution job will be lost it wont re-triggered at simultaneous process time.
Only one job will execute at a time the second will be sleep until the first one is completed.
In multi scheduling process the second job argument will be share to first job at run time. this is one type of disadvantage in multi scheduling process.

Categories

Resources