AlarmClock

AlarmClock with features

GitHub license Build Status Maven Central

HitCount Open Source Love svg2 Maintenance

Basic Example and Usage

  1. AlarmClock
  2. Scheduling
  3. Recurring
  4. Task

Maven dependency

<dependency>
  <groupId>com.github.priytam</groupId>
  <artifactId>alarm</artifactId>
  <version>1.0.0</version>
</dependency>

Gradle dependency

compile 'com.github.priytam:alarm:1.0.0'

AlarmClock

public class AlarmClockExample {
    static {
        LogManager.getRootLogger().setLevel(Level.INFO);
        BasicConfigurator.configure();
    }

    public static void main(String[] args) throws InterruptedException {
        // Get and instance of clock
        AlarmClock clock = AlarmClock.getInstance("TestClock");

        //register to print 'ring ring' after 2 sec
        clock.register(2000, id -> System.out.println("ring ring"));

        //register to print 'ring ring1' after 3 sec
        clock.register(3000, id -> System.out.println("ring ring1"));

        //start this
        clock.start();

        //register to print 'ring ring2' after 2 sec
        clock.register(2000, id -> System.out.println("ring ring2"));

        //register to print 'ring ring3' after 2 sec
        clock.register(1000, id -> System.out.println("ring ring3"));

        //show all alarms set
        System.out.println(clock.getTaskList().showEntries());

        Thread.sleep( 6000);
        clock.shutdown();
    }
}

Output

object = com.clock.AlarmClockHeartbeatTask@17550481, time remaining = 98
object = com.clock.example.AlarmClockExample$$Lambda$5/321142942@2c6a3f77, time remaining = 999
object = com.clock.example.AlarmClockExample$$Lambda$1/1032616650@2ed94a8b, time remaining = 1993
object = com.clock.example.AlarmClockExample$$Lambda$4/1935637221@180bc464, time remaining = 1999
object = com.clock.example.AlarmClockExample$$Lambda$2/649734728@5f2050f6, time remaining = 2995

ring ring3
ring ring
ring ring2
ring ring1

Useful Methods

Back to top

Scheduling

public class ScheduleExample {
    static {
        LogManager.getRootLogger().setLevel(Level.INFO);
        BasicConfigurator.configure();
    }

    public static void main(String[] args) {
        //create a schedule
        Schedule helloWorldSchedule = new Schedule("HelloWorldSchedule");

        //register a task to print 'Hello world'
        helloWorldSchedule.register(() -> System.out.println("Hello world"));

        //will print 'Hello world' every day 5:10
        helloWorldSchedule.addEntry(new DailyScheduleEntry(5, 10));

        //will print 'Hello world' every hour 1' o clock , 2' o clock, 3' clock
        helloWorldSchedule.addEntry(new HourlyScheduleEntry(0, 0));

        //will print 'Hello world' every monday at 5:10
        helloWorldSchedule.addEntry(new WeeklyScheduleEntry(WeeklyScheduleEntry.MONDAY, 5, 10));

        //print schedule Status
        System.out.println(helloWorldSchedule.getStatus());

        //register another task to print hello Priytam and will run with all entries added above
        helloWorldSchedule.register(() -> System.out.println("Hello Priytam"));
        
        //shutdown
        helloWorldSchedule.shutDown();
    }
}

output

	                HourlyEntry (0m:0s) 
				Next time will be at: Sun Jun 14 02:00:00 IST 2020
			DailyEntry (5h:10m:0s) 
				Next time will be at: Sun Jun 14 05:10:00 IST 2020
			WeeklyEntry (2d:5h:10m:0s) 
				Next time will be at: Mon Jun 15 05:10:00 IST 2020

Useful methods

Back to top

Recurring

public class RecurringTaskExample {

    static {
        LogManager.getRootLogger().setLevel(Level.INFO);
        BasicConfigurator.configure();
    }

    public static void main(String[] args) throws InterruptedException {
        recurringSyncTaskExecution();//default
        recurringAsyncTaskExecution();
    }

    private static void recurringSyncTaskExecution() throws InterruptedException {
        // Create a recurring task with interval 1000 ms by 
        // implementing 'performTask' and 'onTaskFailure'
        RecurringTask recurringTask = new RecurringTask(1000) {
            @Override
            public boolean performTask(Object context) {
                System.out.println("Performing task");;
                return true;
            }

            @Override
            public void onTaskFailure(Object context) {
                System.out.println("Performing task failed");;
            }
        };

        //start recurring task
        recurringTask.start();
        Thread.sleep(4000);

        //stop  task
        recurringTask.stop();
    }
    
    private static void recurringAsyncTaskExecution() throws InterruptedException {
        // Create a recurring task with interval 1000 ms by 
        // implementing 'performTask' and 'onTaskFailure'
        RecurringTask recurringTask = new RecurringTask(1000) {
            @Override
            public boolean performTask(Object context) {
                System.out.println("Performing task");;
                return true;
            }

            @Override
            public void onTaskFailure(Object context) {
                System.out.println("Performing task failed");;
            }
        };
        // set async task execution
        recurringTask.setTaskExecution(new AsyncTaskExecution(4));

        //start recurring task
        recurringTask.start();
        Thread.sleep(4000);
        //stop  task
        recurringTask.stop();
    }
}
Performing task
Performing task
Performing task
Performing task
4005 [main] INFO com.clock.AlarmClock  - shutdown()
4008 [main] INFO com.clock.AlarmClock  - start() - starting clock Alarmclock
Performing task
Performing task
Performing task
Performing task
8011 [main] INFO com.clock.AlarmClock  - shutdown()

Useful methods

Note: if performTask() returns false then onTaskFailure() will be called

Back to top

Task

public class TaskExample {
    static {
        LogManager.getRootLogger().setLevel(Level.INFO);
        BasicConfigurator.configure();
    }
    public static void main(String[] args) {
        // Create a task to execute after 1000 ms by 
        // implementing 'performTask' and 'onTaskFailure'
        new Task(1000) {
            @Override
            public boolean performTask(Object context) {
                System.out.println("Perform task");
                return true;
            }

            @Override
            public void onTaskFailure(Object context) {
                System.out.println("failed execution");
            }
        }.start();
    }
}

Useful methods

Note: if performTask() returns false then onTaskFailure() will be called

Back to top