I have an server and a table my current timed Jobs are started in a class where I have different methodes annotated with the #Schedule annotation.
Now I have another form of timed jobs where different requests should be sent to a service in definable time interval. So the user is able to choose something like every 5 minutes or every hour or daily. I will make a list of valid intervals so that there won't be values like every 38 minutes.
So this new timer has to look into the table in whitch interval a job has to be done and then call the function to get the data from the service.
Is this possible without making a new column for something like "Next run"? and what about timers that run at the same time like a 5 Minute a hourly and a dayly timer run all at once once in a day.
Rather than adding a "Next Run" column, I would add a new table that logs when each job was run. That way when your service worker runs it can check the configured run-interval to the last run-time.
Related
I have a requirement to update a field if a payment date is 30 days late.
Is it possible to trigger an action to occur 30 days after the payment date?
The process is likely to restart in-between those times so it can't be in-memory and it can't be a relative date.
I can create an endpoint like /api/paymentdates so that it could be called from something else like cron.
However, there are likely to be a over a million items at some point in the future, each with its own date.
Is there an effective way to trigger a task like this or is the only option to run a task every morning and query the database?
You can make use of a Queue Triggered Function (Storage Queue or Service Bus Queue). Basically what you will do is put a message in a queue and keep it invisible for 30 days (it is called initial visibility timeout in storage queue and scheduled message in service bus queue).
The message will only appear in the queue after its invisibility expires and the Function will be triggered at that time. Once the Function is triggered, you can do whatever processing you want to do on that message.
You may find following links useful:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-queue
https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus
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 a TimerTask that updates a few tables everyday at specific times(not one task per day, different tasks at different times in a day). I get a few time periods from a database and check if the current time is equal to one of the times in the database when the run() of TimerTask is called and perform the respective task for respective time(time from database which is equal to current time)
I think it's not working for the next day because I set the time(which I obtain from database) for the timer when the service starts. The next day when the timer is supposed to do the task and checks with the current time it checks with the previous day's time(the day service is started and time is set for TimerTask) and does not do the task. I should start the Service everyday to set the time(which is obtained from database to check with current time).
How do I start the service everyday at specific time so that the time to be checked with current time is set everyday.
I can try to schedule to do the tasks everyday 24 hours from the day service is started, but sometimes I have to change the start date and for that the service should be started again and I dont know if a service will run for 24 hours.
You should use Alarmanager for this. The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running.
There are 2 ways user can generate a report.
User click a button on the front end and job will run to generate the report.
User can schedule the report to generate weekly, monthly, etc.
On scenario 1, I decided to first save the request to a table, say "REQUEST_TBL". Right after that, I will run ThreadPoolTaskExecutor which picks up the specific request from "REQUEST_TBL". There could be a lot of users that can request to generate a report. But each user is given only up to 30 reports to generate for life (if user wants to generate a new report, he needs to delete any old reports).
On scenario 2, user can schedule a certain report to generate weekly, or monthly. Then a weekly (or monthly or etc) job will run and generate this report that the user scheduled.
Now, I am not sure on how to implement the report generator job. Whether I use ThreadPoolTaskExecutor or not. Or use the same program to handle user request and user scheduled request for report.
I am planning to let one job to run every minute to read "REQUEST_TBL" and for each record I will run ThreadPoolTaskExecutor.execute(). But if there are 1000 users all the same time they requested report, then how should I implement the creation of thread. Also for the scheduled job, I am planning to run it endofday only. The scheduled job will read from the same "REQUEST_TBL" and look for request that is scheduled. For scenario 1, if I want to run a job for every, say, 2 minutes, until what time should I run it? Cause it may be that at the end of that day, a scheduled report will need to run. Also, I thought of running a job for every, say 2 minutes, because if the server went down, there's no way to regenerate the report once the server is started.
I would appreciate your suggestion
You are asking many question at once. So few thoughts here:
definitely don't create thread yoursels. Use rather one of the Executors and limit the number of threads in this way.
for the rest I'll do it in the most consistent way: every record in REQUEST_TBL will also have time when it needs to be generated. So in scenario 1 you will save current time together with the request. With the scenario 2 you'll create a record(s) with timestamp which is week (months) ahead.
then you can run a job every minute or two to query requests with request time before or equal now. And schedule a job to the executor for each returned record.
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.