package com.xjrsoft.common.license;

import com.xjrsoft.common.utils.LicenseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Properties;

/**
 * @Author: tzx
 * @Date: 2023/7/3 13:49
 */
@Component
@EnableScheduling
@ConditionalOnProperty(prefix = "xjrsoft.license", name = "enabled", havingValue = "true")
public class LicenseTask  {

    @Autowired
    private ApplicationContext applicationContext;

    /**
     * 60秒检查一次
     */
    @Scheduled(fixedRate = 60 * 1000)
    private void check(){
        try {
            LicenseUtil.loadLicense(new Properties());
        }
        catch (Exception e){
            System.out.println("当前授权已经过期 请联系管理员！");

            ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) applicationContext;
            ctx.close();
        }


    }
}
