Search code examples
javaspring-booteventtrigger

How to trigger event on specific date automatically to reset count in java?


I am trying to update count of button clicks on every button click. I am using spring boot and angular as frontend.I managed to do that programatically count button clicks and save in database. But I need to reset those count to zero on day 1 of every month. How can I do that?

/** On what day of each month should the count be reset? 1..28 */
static final int DAY_OF_MONTH_TO_RESET_COUNT = 1;
/** In what time zone should above day-of-month be interpreted? */
static final ZoneId timeZone = ZoneId.of("Asia/Pontianak");

public static void resetIfDue() {
    // substitute reset_date from DB here
    LocalDate lastResetDate = LocalDate.of(2020, Month.SEPTEMBER, 24);

    LocalDate nextResetDate = lastResetDate.plusMonths(1)
                                .withDayOfMonth(DAY_OF_MONTH_TO_RESET_COUNT);
    LocalDate today = LocalDate.now(timeZone);
    // "not after today" means today or before today
    if (! nextResetDate.isAfter(today)) {
        System.out.println("Reset the count and update the reset_date in the database");
        updateClickCounts();

    }
}

here I got this function on which date I have to reset the count. It will call updateClickCounts() function which contains update query. But I want to trigger event to call resetIfDue() on day 1 of every month automatically. How can I do that? I am new to event triggers thats why I do not understand How it works.


Solution

  • You can use cron for it, here simple example.