【工具笔记】AviatorEvaluator制作一个时间校验

 定时任务需要,使用AviatorEvaluator做一个与当前判断的规则引擎。使用时间格式yyyy-MM-dd HH:mm和cron表达式进行校验。

例子1(单次时间校验):timer:2022-04-18 15:55

例子2(多次时间校验):cron:* 56 * * * ?

		<dependency>
		    <groupId>com.googlecode.aviator</groupId>
		    <artifactId>aviator</artifactId>
		    <version>5.3.0</version>
		</dependency>
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.googlecode.aviator.AviatorEvaluator;

/**
 * 
 * @DESCRIPTION:定时校验
 * @TODO:未实现的事情
 * @author xiaokong
 * @date 2022年4月15日
 * @version 1.0.0
 * @file com.frame.util.RuleUtil.java
 */
public class AviatorUtil {
	private static Map<String, Long> REPETITION_TIME = new HashMap<String, Long>();
	static {
		System.out.println("..........................");
		AviatorEvaluator.addFunction(new AviatorTimerFunction()); 
	}
	
	/**
	 * 
	 * @DESCRIPTION:时间规则
	 * @TODO:未实现的事情
	 * @param timerVerifyParam:验证 参数;例如【循环验证(cron表达式):cron:* 3 * * * ? ;  单次验证:timer:2022-04-15 17:08】   
	 * @param key:重复判断验证标志,防止一段时间内重复判断
	 * @return
	 * @throws Exception
	 */
	public static boolean verifyTime(String expression, String key) throws Exception{
    	Map<String, Object> env = new HashMap<String, Object>(); 
    	System.out.println("expression:"+expression);
    	Integer result = (Integer) AviatorEvaluator.execute("timeComparison(\""+expression+"\")", env, true); 
    	synchronized (REPETITION_TIME) {
    		String keyTmp = "timer-"+key;
    		Long repetionFlag = REPETITION_TIME.get(keyTmp);
    		
    		boolean res = false;
    		//判断一分钟是否重复验证,第一次验证为true,后续验证为false
    		if(repetionFlag==null) {
    			if(result == 1) {
    				REPETITION_TIME.put(keyTmp, System.currentTimeMillis());
    				res = true; 
            	}
    		}else {
    			long interval = System.currentTimeMillis()-repetionFlag;
    			if(interval>1000*60) {
    				REPETITION_TIME.remove(keyTmp);
    				if(result == 1) {
    					REPETITION_TIME.put(keyTmp, System.currentTimeMillis());
        				res = true; 
                	}
    			}
    		}
    		return res;
		}
	}
	
	public static void main(String[] args) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date now = new Date();
		//定时
		System.out.println(AviatorUtil.verifyTime("timer:"+sdf.format(new Date(now.getTime()-1000*61)), "112233"));
		System.out.println(AviatorUtil.verifyTime("timer:"+sdf.format(now), "112233"));
		System.out.println(AviatorUtil.verifyTime("timer:"+sdf.format(now), "112233"));
		//循环
		System.out.println(AviatorUtil.verifyTime("cron:* 55 * * * ?", "223344"));
		System.out.println(AviatorUtil.verifyTime("cron:* 55 * * * ?", "223344"));
		System.out.println(AviatorUtil.verifyTime("cron:* 56 * * * ?", "777777"));
	}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.quartz.CronExpression;

import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.function.FunctionUtils;
import com.googlecode.aviator.runtime.type.AviatorBigInt;
import com.googlecode.aviator.runtime.type.AviatorObject;

/**
 * 
 * @DESCRIPTION:定时验证规则
 * @author xiaokong
 * @date 2022年4月15日
 * @version 1.0.0
 * @file com.modules.hardwareplatform.TimerAviatorFunction.java
 */
public class AviatorTimerFunction extends AbstractFunction{
	private static final long serialVersionUID = 1L;

	@Override
	public String getName() {
		return "timeComparison";
	}
	
	/**
	 * 时间规则-与当前时间对比<br/>
	 * 循环  cron:秒 分 时 天 月 周<br/>
	 *  单次  time:yyyy-MM-dd HH:mm<br/>
	 *  返回1
	 */
	@Override
    public AviatorObject call(Map<String, Object> env, AviatorObject arg1) {
		String formatRule = FunctionUtils.getStringValue(arg1, env);
		int result = 0;
		
		if(formatRule.startsWith("timer:")) {
			String cStr = formatRule.replace("timer:", "");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String now = sdf.format(new Date());
			if(now.equals(cStr)) {
				result = 1;
			}
		}else if(formatRule.startsWith("cron:")) {
			String cStr = formatRule.replace("cron:", "");
			try {
				CronExpression exp = new CronExpression(cStr);
				boolean restemp= exp.isSatisfiedBy(new Date());
				if(restemp){
					result = 1;
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
		}
        return new AviatorBigInt(result);
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孔小胖子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值