I need the program to run on certain days at a specific time. How can you do this with java ?
- oneDo you want to implement this using java or using the OS? If using java, the program should be running - Andrew Bystrov
- c java it is only possible to start some thread that performs a specific action. If you want to run exactly the program then look towards cron (for linux) en.wikipedia.org/wiki/Cron or something like that - Artem Konovalov
- Corrected a question. I do not want to cron'u, I want java. - faoxis
- @faoxis java can only until the computer restarts. start the process and wait for the right time. then the process starts either the user or the scheduler, in any case action is needed from outside - Senior Pomidor
|
1 answer
You can use the quartz library - it allows you to create a launch schedule on certain days of the week / month and the like. For example, like this:
1) Create a task that we will run on a schedule:
public class QuartzJob implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { //запускаемая по расписанию задача } } 2) Create a launch schedule and link the task and schedule in the scheduler:
JobDetail job = JobBuilder .newJob(QuartzJob.class) .withIdentity("QuartzJob", "group1") .build(); Trigger trigger = TriggerBuilder .newTrigger() .withIdentity("QuartzTrigger", "group1") .withSchedule(CronScheduleBuilder .cronSchedule("0 0 9-18/1 ? * MON,TUE,WED")) .build(); SchedulerFactory schedFact = new StdSchedulerFactory(); Scheduler sched = schedFact.getScheduler(); sched.start(); sched.scheduleJob(job,trigger); In this example, the task runs every hour at 0 seconds and 0 minutes from 9 to 18 every Monday, Tuesday and Wednesday. More information about the configuration of the scheduler can be found in the documentation link number 1 and link number 2 .
- Could you answer this question: ru.stackoverflow.com/questions/586196/… ? - faoxis
|