3

I need to write a scheduler class which should be invoked every 5th, 15th and 30th minute of hour.

How should I start working on this scheduler? I want to use Java 7.

One way is to know the current time and then schedule calculating the difference of each timer.

Is there any library or class which already does similar work?

Bankelaal
  • 408
  • 1
  • 9
  • 24

7 Answers7

5

Don't reinvent the wheels, use existing application, use Quartz scheduler or similar app.

1ac0
  • 2,875
  • 3
  • 33
  • 47
3

If you are using 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 may be provided. For example, the following will only execute on weekdays for every 15 minutes.

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

You need to pay attention to * and / of cron expresssion 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". 
JaskeyLam
  • 15,405
  • 21
  • 114
  • 149
1

You should use Quartz for the job, quick getting started available here

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

Regarding the exact expression, here are the docs, loaded with examples

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

Master Slave
  • 27,771
  • 4
  • 57
  • 55
1

In Quartz 2, this would be

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

This creates a trigger that activates at the right minutes past the hour. The first field is seconds; next is minutes; then hours; day of month; month; day of week (which you want to be unspecified, hence the ?). You can specify multiple entries for each, and a * means always; so this is all days, at 5, 10 or 15 minutes and 0 seconds past.

Now you can create a Quartz job

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

and schedule it using this trigger:

Scheduler sched = new StdSchedulerFactory().getScheduler();
    sched.start();
    sched.scheduleJob(new MyJob(), trigger);
chiastic-security
  • 20,430
  • 4
  • 39
  • 67
  • Thanks , could throw some light on cronSchedule parameters. What I forgot to mention in my question was that, I need to schedule job with granularity of 5 minutes , 15 minutes , 30 minutes etc. so 5 minutes job should run @ 00:00 , 00:05 , 00:10 ,... 15 minutes job should run at 00:00 , 00:15. So this way at 00:00 , 00:30 tasks with granularity 5 , 15 , 30 all will run. At 00:15 , 00:45 - Tasks with 5 minute and 15 minute granularity would run. – Bankelaal Nov 14 '14 at 11:09
  • Also would like to know , when the job is scheduled is there a way to know if it was 5th minute or 15th Minute job ... which was triggered. Of course I can programatically find it but if its available out of box. – Bankelaal Nov 14 '14 at 11:24
  • @Bankelaal Ah, if you want them to be scheduled at fixed intervals, some every five minutes and some every fifteen minutes, then you're probably best off not worrying about Quartz! The point of Quartz was that you can specify a complex schedule, such as the one above. – chiastic-security Nov 14 '14 at 12:25
  • @Bankelaal I've added something to explain the parameters. – chiastic-security Nov 14 '14 at 12:27
1

I think, you can use Quartz scheduler to implement any scheduling jobs. Its very easy to use.

Rakesh
  • 311
  • 2
  • 11
0

From b_erb:

Use a 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);
Community
  • 1
  • 1
Davide Pastore
  • 8,678
  • 10
  • 39
  • 53
  • 1
    That can be used to schedule a task every 15mins, but not for the 15th minute of every hour. – Thilo Nov 14 '14 at 08:57
0

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");  

    }

}
suresh manda
  • 659
  • 1
  • 8
  • 25