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

huangapple go评论111阅读模式
英文:

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

问题

我试图在每次按钮点击时更新按钮点击次数。我正在使用Spring Boot和Angular作为前端。我成功地通过编程的方式计算按钮点击次数并保存在数据库中。但我需要在每个月的第一天将这些计数重置为零。
如何实现这个目标?

    /** 每个月的哪一天应该重置计数? 1..28 */
	static final int DAY_OF_MONTH_TO_RESET_COUNT = 1;
	/** 上述日期应该在哪个时区解释? */
	static final ZoneId timeZone = ZoneId.of("Asia/Pontianak");
	
	public static void resetIfDue() {
	    // 从数据库中获取重置日期
	    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);
	    // “不晚于今天”表示今天或今天之前
	    if (!nextResetDate.isAfter(today)) {
	        System.out.println("重置计数并更新数据库中的重置日期");
			updateClickCounts();
	    }
	}

在这里,我有一个函数,指定了何时需要重置计数。它将调用updateClickCounts()函数,该函数包含更新查询。但我想在每个月的第一天自动触发事件来调用resetIfDue()。我对事件触发器不太了解,所以不太清楚它是如何工作的。

英文:

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.

答案1

得分: 1

你可以使用cron来实现,这里是一个简单的示例

英文:

You can use cron for it, here simple example.

答案2

得分: 1

你可以按照以下方式使用Spring的cron表达式:

/** 每月的哪一天应该重置计数?1 到 28 */
static final int DAY_OF_MONTH_TO_RESET_COUNT = 1;
/** 如何解释上述日期在哪个时区? */
static final ZoneId timeZone = ZoneId.of("Asia/Pontianak");

@Scheduled(cron="0 0 0 1 1/1 *")
public static void resetIfDue() {
    // 在这里替换来自数据库的 reset_date
    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);
    // "不在今天之后" 意味着今天或在今天之前
    if (!nextResetDate.isAfter(today)) {
        System.out.println("重置计数并在数据库中更新 reset_date");
        updateClickCounts();

    }
}

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

英文:

You can make use of the spring cron as below:

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

@Scheduled(cron="0 0 0 1 1/1 *")
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();

    }
}

Add @EnableScheduling annotation in the spring boot main class.

huangapple
  • 本文由 发表于 2020年9月24日 20:56:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64046939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定