|
@@ -0,0 +1,132 @@
|
|
|
|
+package com.ruoyi.framework.config.license;
|
|
|
|
+
|
|
|
|
+import org.springframework.scheduling.annotation.EnableAsync;
|
|
|
|
+import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+
|
|
|
|
+import java.time.LocalDate;
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
+import java.time.LocalTime;
|
|
|
|
+import java.time.ZoneId;
|
|
|
|
+import java.util.Calendar;
|
|
|
|
+import java.util.Date;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @ClassName ScheduleTaskMgr
|
|
|
|
+ * @Description: TODO
|
|
|
|
+ * @Author LX
|
|
|
|
+ * @Date 2024/2/22
|
|
|
|
+ * @Version V1.0
|
|
|
|
+ **/
|
|
|
|
+@Component
|
|
|
|
+@EnableScheduling // 1.开启定时任务
|
|
|
|
+@EnableAsync // 2.开启多线程
|
|
|
|
+public class ScheduleTaskMgr {
|
|
|
|
+ public static volatile long UTC_MS_TODAY_BEGIN = 0L;
|
|
|
|
+ public static volatile long UTC_MS_TODAY_END = 0L;
|
|
|
|
+ public static volatile long UTC_MS_THIS_MONTH_BEGIN = 0L;
|
|
|
|
+ public static volatile long UTC_MS_THIS_MONTH_END = 0L;
|
|
|
|
+ public static void refreshUTCTimeRes(){
|
|
|
|
+ UTC_MS_TODAY_BEGIN = getTodayBeginUTC();
|
|
|
|
+ UTC_MS_TODAY_END = getTodayEndUTC();
|
|
|
|
+ UTC_MS_THIS_MONTH_BEGIN = getMonthBeginUTC(System.currentTimeMillis());
|
|
|
|
+ UTC_MS_THIS_MONTH_END = getMonthEndUTC(System.currentTimeMillis());
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 获取当天的开始时间
|
|
|
|
+ * @return UTC时间格式(单位:毫秒)
|
|
|
|
+ * 返回时间格式为:00:00:00.000所对应的时间戳
|
|
|
|
+ */
|
|
|
|
+ public static long getTodayBeginUTC(){
|
|
|
|
+ return LocalDateTime.of(LocalDate.now(), LocalTime.MIN)
|
|
|
|
+ .atZone(ZoneId.systemDefault())
|
|
|
|
+ .toInstant()
|
|
|
|
+ .toEpochMilli();
|
|
|
|
+ /*Calendar todayStart = Calendar.getInstance();
|
|
|
|
+ todayStart.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
+ todayStart.set(Calendar.MINUTE, 0);
|
|
|
|
+ todayStart.set(Calendar.SECOND, 0);
|
|
|
|
+ todayStart.set(Calendar.MILLISECOND, 0);
|
|
|
|
+ return todayStart.getTime().getTime();*/
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 获取当天的结束时间
|
|
|
|
+ * @return UTC时间格式(单位:毫秒)
|
|
|
|
+ * 返回时间格式为:23:59:59.999所对应的时间戳
|
|
|
|
+ */
|
|
|
|
+ public static long getTodayEndUTC(){
|
|
|
|
+ return LocalDateTime.of(LocalDate.now(), LocalTime.MAX)
|
|
|
|
+ .atZone(ZoneId.systemDefault())
|
|
|
|
+ .toInstant()
|
|
|
|
+ .toEpochMilli();
|
|
|
|
+ /*Calendar todayStart = Calendar.getInstance();
|
|
|
|
+ todayStart.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
|
+ todayStart.set(Calendar.MINUTE, 59);
|
|
|
|
+ todayStart.set(Calendar.SECOND, 59);
|
|
|
|
+ todayStart.set(Calendar.MILLISECOND, 999);
|
|
|
|
+ return todayStart.getTime().getTime();*/
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 获取当前时间所在月份的开始时间戳
|
|
|
|
+ * @param curTime
|
|
|
|
+ * @return UTC时间格式(单位:毫秒)
|
|
|
|
+ * 返回时间格式为:xxxx-xx-01 00:00:00.000所对应的时间戳
|
|
|
|
+ */
|
|
|
|
+ public static long getMonthBeginUTC(long curTime){
|
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
|
+ if (!isMsUTC(curTime))
|
|
|
|
+ curTime = curTime*1000;
|
|
|
|
+ try {
|
|
|
|
+ c.setTime(new Date(curTime));
|
|
|
|
+ c.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
+ c.set(Calendar.MINUTE, 0);
|
|
|
|
+ c.set(Calendar.SECOND,0);
|
|
|
|
+ c.set(Calendar.MILLISECOND, 0);
|
|
|
|
+ return c.getTimeInMillis();
|
|
|
|
+ }catch (Exception e){}
|
|
|
|
+ return 0L;
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 获取当前时间所在月份的结束时间戳
|
|
|
|
+ * @param curTime
|
|
|
|
+ * @return UTC时间格式(单位:毫秒)
|
|
|
|
+ * 返回时间格式为:xxxx-xx-31 23:59:59.999所对应的时间戳
|
|
|
|
+ */
|
|
|
|
+ public static long getMonthEndUTC(long curTime){
|
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
|
+ if (!isMsUTC(curTime))
|
|
|
|
+ curTime = curTime*1000;
|
|
|
|
+ try {
|
|
|
|
+ c.setTime(new Date(curTime));
|
|
|
|
+ c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
|
+ c.set(Calendar.MINUTE, 59);
|
|
|
|
+ c.set(Calendar.SECOND,59);
|
|
|
|
+ c.set(Calendar.MILLISECOND, 999);
|
|
|
|
+ return c.getTimeInMillis();
|
|
|
|
+ }catch (Exception e){}
|
|
|
|
+ return 0L;
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 指定UTC是否为精确到毫秒的UTC时间
|
|
|
|
+ * @param lUTC
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static boolean isMsUTC(long lUTC){
|
|
|
|
+ return lUTC > 10000000000L;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Scheduled(cron = "0 0-5 0 * * ?")
|
|
|
|
+ public void taskRefreshTimeRes() {
|
|
|
|
+ refreshUTCTimeRes();
|
|
|
|
+ ThreadTaskMgr.runTask(new Runnable() {
|
|
|
|
+ @Override
|
|
|
|
+ public void run() {
|
|
|
|
+ GlobalData.getInstance().reInitLicense();
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+}
|