com.vip.saturn.job.console.service.impl.UtilsServiceImpl Maven / Gradle / Ivy
/**
* Copyright 2016 vip.com.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
**/
package com.vip.saturn.job.console.service.impl;
import com.vip.saturn.job.console.domain.ForecastCronResult;
import com.vip.saturn.job.console.exception.SaturnJobConsoleException;
import com.vip.saturn.job.console.service.UtilsService;
import com.vip.saturn.job.console.utils.CronExpression;
import com.vip.saturn.job.console.utils.SaturnConstants;
import org.apache.commons.lang3.StringUtils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
/**
* @author hebelala
*/
public class UtilsServiceImpl implements UtilsService {
@Override
public ForecastCronResult checkAndForecastCron(final String timeZone, final String cron)
throws SaturnJobConsoleException {
if (StringUtils.isBlank(timeZone)) {
throw new SaturnJobConsoleException("timeZone不能为空");
}
if (StringUtils.isBlank(cron)) {
throw new SaturnJobConsoleException("cron不能为空");
}
String timeZoneTrim = timeZone.trim();
String cronTrim = cron.trim();
if (!SaturnConstants.TIME_ZONE_IDS.contains(timeZoneTrim)) {
throw new SaturnJobConsoleException(String.format("timeZone(%s)无效", timeZoneTrim));
}
TimeZone tz = TimeZone.getTimeZone(timeZoneTrim);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(tz);
CronExpression cronExpression = null;
try {
cronExpression = new CronExpression(cronTrim);
} catch (ParseException e) {
throw new SaturnJobConsoleException(String.format("cron(%s)格式错误:%s", cronTrim, e.getMessage()));
}
cronExpression.setTimeZone(tz);
ForecastCronResult forecastCronResult = new ForecastCronResult();
forecastCronResult.setTimeZone(timeZoneTrim);
forecastCronResult.setCron(cronTrim);
forecastCronResult.setNextFireTimes(new ArrayList());
Date now = new Date();
for (int i = 0; i < 10; i++) {
Date next = cronExpression.getNextValidTimeAfter(now);
if (next != null) {
forecastCronResult.getNextFireTimes().add(dateFormat.format(next));
now = next;
}
}
if (forecastCronResult.getNextFireTimes().isEmpty()) {
throw new SaturnJobConsoleException(String.format("cron(%s)可能描述的是一个过去的时间,将不会被执行", cronTrim));
}
return forecastCronResult;
}
@Override
public List getTimeZones() throws SaturnJobConsoleException {
return SaturnConstants.TIME_ZONE_IDS;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy