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.
Related
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).
I have around 1000 entries in my datastore and this is likely to increase with time to around 10,000 entries. My task is to update each row's certain properties and save it back and this task has to be performed every 24 hours.
So, what should I use?
First, you create a cron job that runs every 24 hours.
Second, you need to decide what this cron job will do. The simplest option is to update all 1,000 records. You can retrieve and save all entities in large batches (i.e. 500 per call). If this is a simple update of values, it will take just a few seconds.
Since cron jobs are not retried if they fail, a better option is to create a task and add it to the queue. All updates will happen within that task.
NB: Make sure that if your task is retried, it won't mess the data. If this is not possible, you will have to use some kind of flag (i.e. timestamp of last update) to separate updated entities from those that still need updates.
As your data set grows, your cron job can start multiple tasks to update, for example, 1,000 records in each task.
In the task queue the tasks have to be added to the queue manually though code. If you want to do this task automatically every x time, what you need is a cron job.
You need both,
Cron job to start your batch update job every 24 hours
Task-queues to process you records.
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.
Good Day,
I am required to write a java server that performs an action every X minutes. The action is to check a database to see if the current/system time matches any of the times in a database, and to pull out those items, and send a TCP message to them.
Hencen, the database call is local on the machine, so that is no problem. However, at least 10 TCP calls need to be sent out simultaneously. Hence, the tick may actually need to occur on it's own thread. Can I have some suggestions?
Do I need a thread pool?
one thing you can do is create a schedular job and run that job every x minutes. so that job will be perform every x minutes and you need to define your task in job to perform for more info click here
I would use a Timer or else I would use the Quartz Scheduler - the former is more lightweight, while the latter is (optionally) durable (meaning that scheduled tasks will be saved to a database and reloaded when your program restarts).
Either TimerTask (or) ScheduledExecutorServices implementations would be best options for this task. Yes, I think thread pool would be best option because you don't need to create 10 threads every X minutes.
I'm about to create a small application which will be responsible for sending out various reports to various users at various intevals. We might be talking about 50 or 100 different reports going to different people. Some reports needs to be generated every day, some every week, and some every month.
I've been using the Quartz library earlier to run tasks at regular intervals. However, in order to keep things simple I like the thought of having a single Quartz thread taking care of all reports. That is, the thread should loop through all reports, say every 15 minutes, and determine wether it is time for one or more to be generated and sent. It does not matter if a report is generated at 12:00 or 12:15.
I'm thinking about wether it would be possible, somehow, for each report to set up specific times such as "mon#12:00,wed#12:00" or "fri#09:30". Then, based on that, the thread would determine if it was time to send a report or not.
My question is; has anyone else done something like this and does any libraries exist which can make it easy to implement this task?
why not simply register a separate quartz task instance for each report and let Quartz handle all the scheduling for you? That is after all the point behind it.
you can create just single thread and it would ping a "job schedule data structure" at some time interval to see if it needs to run a report. If yes, it would run the report, otherwise, it would go for a short nap and ping again after specified sleep time.
It will cause problem if one job takes too much time to complete and you start accumulating jobs.
The job schedule data structure would keep its record sorted by time stamp.