如何自动触发特定日期的事件以重置Java中的计数?

问题描述

我正在尝试在每次按钮单击时更新按钮单击数。我使用spring boot和angular作为前端。我设法以编程方式计算按钮的点击次数并保存在数据库中。但是我需要在每个月的第一天将这些计数重置为零。 我该怎么办?

/** 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();

    }
}

在这里,我必须在该日期重新设置计数。它将调用包含更新查询的updateClickCounts()函数。但是我想触发事件以在每月的第一天自动调用resetIfDue()。我怎样才能做到这一点?我是事件触发器的新手,这就是为什么我不了解其工作原理的原因。

解决方法

您可以使用cron,这里是简单的example

,

您可以使用以下弹簧cron:

my_dict = dict()
for index in df[(df['product_number'] == 101)].index:
    key = (df.loc[index,"product_size"],df.loc[index,"country"],"week"])
        my_dict[key] = df.loc[index,"selling_price"]

for index in df[(df['product_number'] == 102) | (df['product_number'] == 103)].index:
    unlock = (df.loc[index,"week_hf"])
        df.loc[index,"selling_price"] = my_dict[unlock]

在Spring Boot主类中添加@EnableScheduling注释。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...