How to create a scheduler class in Java

I need to write a scheduler class that should be called every 5, 15 and 30 minutes.

How do I get started on this planner? I want to use Java 7.

One way is to find out the current time and then schedule the calculation of the difference for each timer.

Is there any library or class that already does a similar job?

+3


source to share


7 replies


In Quartz 2 it will be

Trigger trigger = TriggerBuilder
    .newTrigger()
    .withIdentity("someTriggerName", "someGroup")
    .withSchedule(
        CronScheduleBuilder.cronSchedule("0 5,15,30 * * * ?"))
    .build();

      

This creates a trigger that fires at the right minutes minute of the hour. The first field is seconds; next - minutes; then the clock; day of the month; month; day of the week (which you want to be unspecified, hence ?

). You can specify multiple entries for each, which *

means always; so it's all days, after 5, 10 or 15 minutes and 0 seconds.

You can now create a quart assignment



public class MyJob implements Job
{
    public void execute(JobExecutionContext context throws JobExecutionException {
        // do something useful  
    }
}

      

and schedule it with this trigger:

Scheduler sched = new StdSchedulerFactory().getScheduler();
    sched.start();
    sched.scheduleJob(new MyJob(), trigger);

      

+1


source


Don't reinvent wheels, use an existing app, use a quartz scheduler or similar app.



+5


source


If you use Spring

, you can use @Scheduled

to schedule a task.

Check here to get statred https://spring.io/guides/gs/scheduling-tasks/ .

If simple periodic scheduling is not expressive enough, then a cron expression can be provided . For example, the following will only run on weekdays every 15 minutes.

@Scheduled(cron="* */15 * * * MON-FRI")
public void doSomething() {
    // something that should execute on weekdays only
}

      

You should pay attention to *

and /

cron expressions in your case

* :("all values") - used to select all values within a field. For example, "*" in the minute field means "every minute".

/ :- used to specify increments. For example, "0/15" in the seconds field means "the seconds 0, 15, 30, and 45". And "5/15" in the seconds field means "the seconds 5, 20, 35, and 50". 

      

+2


source


You have to use Quartz for the job, get started quickly here

http://www.mkyong.com/java/quartz-scheduler-example/

As for the exact expression, here are the docs loaded with examples

http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger

+1


source


I think you can use Quartz scheduler to do any scheduling jobs. It's very easy to use.

+1


source


From b_erb :

Use ScheduledExecutorService :

 private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
 scheduler.scheduleAtFixedRate(yourRunnable, 0, 5, MINUTES);

 private final ScheduledExecutorService scheduler1 = Executors.newScheduledThreadPool(1);
 scheduler1.scheduleAtFixedRate(yourRunnable1, 0, 15, MINUTES);

 private final ScheduledExecutorService scheduler2 = Executors.newScheduledThreadPool(1);
 scheduler2.scheduleAtFixedRate(yourRunnable2, 0, 30, MINUTES);

      

0


source


see below code for scheduler:

public class SimpleScheduler 
{
    public static void main( String[] args ) throws Exception
    {
        JobDetail job = new JobDetail();
        job.setName("hello");
        job.setJobClass(SchedulerJob .class);

        //configure the scheduler time
        SimpleTrigger trigger = new SimpleTrigger();
        trigger.setName("hello name");
        trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
        trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
        trigger.setRepeatInterval(30000);

        //schedule it
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
        scheduler.scheduleJob(job, trigger);

    }
}

      

interface name:

public class SchedulerJob implements Job
{
    public void execute(JobExecutionContext context)
    throws JobExecutionException {

        System.out.println("u develop the code here");  

    }

}

      

0


source







All Articles