feat(1.3.0-beta2): 优化cron表达式解析错误异常信息
This commit is contained in:
		
							parent
							
								
									04bd145e09
								
							
						
					
					
						commit
						c3d594fc41
					
				@ -1,5 +1,6 @@
 | 
			
		||||
package com.aizuda.snailjob.server.common.strategy;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.lang.Assert;
 | 
			
		||||
import com.aizuda.snailjob.common.core.exception.SnailJobCommonException;
 | 
			
		||||
import com.aizuda.snailjob.common.core.util.CronExpression;
 | 
			
		||||
import com.aizuda.snailjob.server.common.WaitStrategy;
 | 
			
		||||
@ -179,6 +180,7 @@ public class WaitStrategies {
 | 
			
		||||
 | 
			
		||||
            try {
 | 
			
		||||
                Date nextValidTime = new CronExpression(context.getTriggerInterval()).getNextValidTimeAfter(new Date(context.getNextTriggerAt()));
 | 
			
		||||
                Assert.notNull(nextValidTime, () -> new SnailJobServerException("表达式错误:{}", context.getTriggerInterval()));
 | 
			
		||||
                return DateUtils.toEpochMilli(nextValidTime);
 | 
			
		||||
            } catch (ParseException e) {
 | 
			
		||||
                throw new SnailJobServerException("解析CRON表达式异常 [{}]", context.getTriggerInterval(), e);
 | 
			
		||||
 | 
			
		||||
@ -12,6 +12,7 @@ import java.time.ZonedDateTime;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author opensnail
 | 
			
		||||
@ -29,6 +30,9 @@ public class CronUtils {
 | 
			
		||||
            try {
 | 
			
		||||
                ZonedDateTime zdt = now.atZone(ZoneOffset.ofHours(8));
 | 
			
		||||
                nextValidTime = new CronExpression(cron).getNextValidTimeAfter(Date.from(zdt.toInstant()));
 | 
			
		||||
                if (Objects.isNull(nextValidTime)) {
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
                now = LocalDateTime.ofEpochSecond(nextValidTime.getTime() / 1000, 0, ZoneOffset.ofHours(8));
 | 
			
		||||
                list.add(DateUtils.format(now, DateUtils.NORM_DATETIME_PATTERN));
 | 
			
		||||
            } catch (ParseException ignored) {
 | 
			
		||||
@ -40,7 +44,8 @@ public class CronUtils {
 | 
			
		||||
 | 
			
		||||
    public static long getExecuteInterval(String cron) {
 | 
			
		||||
        List<String> executeTimeByCron = getExecuteTimeByCron(cron, 2);
 | 
			
		||||
        Assert.isTrue(!executeTimeByCron.isEmpty(), () -> new SnailJobServerException("[{}]表达式解析有误", cron));
 | 
			
		||||
        Assert.isTrue(!executeTimeByCron.isEmpty(), () -> new SnailJobServerException("表达式解析有误.[{}]", cron));
 | 
			
		||||
        Assert.isTrue(executeTimeByCron.size() == 2, () -> new SnailJobServerException("表达式必须支持多次执行.[{}]", cron));
 | 
			
		||||
        LocalDateTime first = LocalDateTime.parse(executeTimeByCron.get(0), DateUtils.NORM_DATETIME_PATTERN);
 | 
			
		||||
        LocalDateTime second = LocalDateTime.parse(executeTimeByCron.get(1), DateUtils.NORM_DATETIME_PATTERN);
 | 
			
		||||
        Duration duration = Duration.between(first, second);
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user