You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * application 事件 * @param event the event to respond to */@OverridepublicvoidonApplicationEvent(ContextRefreshedEventevent) {
if (event.getApplicationContext() == this.applicationContext) {
// Running in an ApplicationContext -> register tasks this late...// giving other ContextRefreshedEvent listeners a chance to perform// their work at the same time (e.g. Spring Batch's job registration).// 注册定时任务finishRegistration();
}
}
@OverridepublicObjectpostProcessAfterInitialization(Objectbean, StringbeanName) {
if (beaninstanceofAopInfrastructureBean || beaninstanceofTaskScheduler ||
beaninstanceofScheduledExecutorService) {
// Ignore AOP infrastructure such as scoped proxies.returnbean;
}
// 当前类Class<?> targetClass = AopProxyUtils.ultimateTargetClass(bean);
if (!this.nonAnnotatedClasses.contains(targetClass)) {
// 方法扫描,存在 Scheduled、Schedules 注解的全部扫描Map<Method, Set<Scheduled>> annotatedMethods = MethodIntrospector.selectMethods(targetClass,
(MethodIntrospector.MetadataLookup<Set<Scheduled>>) method -> {
Set<Scheduled> scheduledMethods = AnnotatedElementUtils.getMergedRepeatableAnnotations(
method, Scheduled.class, Schedules.class);
return (!scheduledMethods.isEmpty() ? scheduledMethods : null);
});
if (annotatedMethods.isEmpty()) {
this.nonAnnotatedClasses.add(targetClass);
if (logger.isTraceEnabled()) {
logger.trace("No @Scheduled annotations found on bean class: " + targetClass);
}
}
else {
// Non-empty set of methodsannotatedMethods.forEach((method, scheduledMethods) ->
// 处理 scheduled 相关信息scheduledMethods.forEach(scheduled -> processScheduled(scheduled, method, bean)));
if (logger.isTraceEnabled()) {
logger.trace(annotatedMethods.size() + " @Scheduled methods processed on bean '" + beanName +
"': " + annotatedMethods);
}
}
}
returnbean;
}
处理定时任务注解
protectedvoidprocessScheduled(Scheduledscheduled, Methodmethod, Objectbean) {
try {
Runnablerunnable = createRunnable(bean, method);
booleanprocessedSchedule = false;
StringerrorMessage =
"Exactly one of the 'cron', 'fixedDelay(String)', or 'fixedRate(String)' attributes is required";
Set<ScheduledTask> tasks = newLinkedHashSet<>(4);
// Determine initial delay// 是否延迟执行longinitialDelay = scheduled.initialDelay();
// 延迟执行时间StringinitialDelayString = scheduled.initialDelayString();
// 是否有延迟执行的时间if (StringUtils.hasText(initialDelayString)) {
Assert.isTrue(initialDelay < 0, "Specify 'initialDelay' or 'initialDelayString', not both");
if (this.embeddedValueResolver != null) {
initialDelayString = this.embeddedValueResolver.resolveStringValue(initialDelayString);
}
if (StringUtils.hasLength(initialDelayString)) {
try {
initialDelay = parseDelayAsLong(initialDelayString);
}
catch (RuntimeExceptionex) {
thrownewIllegalArgumentException(
"Invalid initialDelayString value \"" + initialDelayString + "\" - cannot parse into long");
}
}
}
// Check cron expression// 获取cron表达式Stringcron = scheduled.cron();
// cron表达式是否存在if (StringUtils.hasText(cron)) {
// 获取时区Stringzone = scheduled.zone();
if (this.embeddedValueResolver != null) {
// 字符串转换cron = this.embeddedValueResolver.resolveStringValue(cron);
zone = this.embeddedValueResolver.resolveStringValue(zone);
}
if (StringUtils.hasLength(cron)) {
// cron 是否延迟Assert.isTrue(initialDelay == -1, "'initialDelay' not supported for cron triggers");
processedSchedule = true;
if (!Scheduled.CRON_DISABLED.equals(cron)) {
TimeZonetimeZone;
if (StringUtils.hasText(zone)) {
// 时区解析timeZone = StringUtils.parseTimeZoneString(zone);
}
else {
// 默认时区获取timeZone = TimeZone.getDefault();
}
// 创建任务tasks.add(this.registrar.scheduleCronTask(newCronTask(runnable, newCronTrigger(cron, timeZone))));
}
}
}
// At this point we don't need to differentiate between initial delay set or not anymoreif (initialDelay < 0) {
initialDelay = 0;
}
// Check fixed delay// 获取间隔调用时间longfixedDelay = scheduled.fixedDelay();
// 间隔时间>0if (fixedDelay >= 0) {
Assert.isTrue(!processedSchedule, errorMessage);
processedSchedule = true;
// 创建任务,间隔时间定时任务tasks.add(this.registrar.scheduleFixedDelayTask(newFixedDelayTask(runnable, fixedDelay, initialDelay)));
}
// 延迟时间StringfixedDelayString = scheduled.fixedDelayString();
if (StringUtils.hasText(fixedDelayString)) {
if (this.embeddedValueResolver != null) {
fixedDelayString = this.embeddedValueResolver.resolveStringValue(fixedDelayString);
}
if (StringUtils.hasLength(fixedDelayString)) {
Assert.isTrue(!processedSchedule, errorMessage);
processedSchedule = true;
try {
fixedDelay = parseDelayAsLong(fixedDelayString);
}
catch (RuntimeExceptionex) {
thrownewIllegalArgumentException(
"Invalid fixedDelayString value \"" + fixedDelayString + "\" - cannot parse into long");
}
// 创建延迟时间任务tasks.add(this.registrar.scheduleFixedDelayTask(newFixedDelayTask(runnable, fixedDelay, initialDelay)));
}
}
// Check fixed rate// 获取调用频率longfixedRate = scheduled.fixedRate();
if (fixedRate >= 0) {
Assert.isTrue(!processedSchedule, errorMessage);
processedSchedule = true;
// 创建调用频率的定时任务tasks.add(this.registrar.scheduleFixedRateTask(newFixedRateTask(runnable, fixedRate, initialDelay)));
}
StringfixedRateString = scheduled.fixedRateString();
if (StringUtils.hasText(fixedRateString)) {
if (this.embeddedValueResolver != null) {
fixedRateString = this.embeddedValueResolver.resolveStringValue(fixedRateString);
}
if (StringUtils.hasLength(fixedRateString)) {
Assert.isTrue(!processedSchedule, errorMessage);
processedSchedule = true;
try {
fixedRate = parseDelayAsLong(fixedRateString);
}
catch (RuntimeExceptionex) {
thrownewIllegalArgumentException(
"Invalid fixedRateString value \"" + fixedRateString + "\" - cannot parse into long");
}
tasks.add(this.registrar.scheduleFixedRateTask(newFixedRateTask(runnable, fixedRate, initialDelay)));
}
}
// Check whether we had any attribute setAssert.isTrue(processedSchedule, errorMessage);
// Finally register the scheduled taskssynchronized (this.scheduledTasks) {
// 定时任务注册Set<ScheduledTask> regTasks = this.scheduledTasks.computeIfAbsent(bean, key -> newLinkedHashSet<>(4));
regTasks.addAll(tasks);
}
}
catch (IllegalArgumentExceptionex) {
thrownewIllegalStateException(
"Encountered invalid @Scheduled method '" + method.getName() + "': " + ex.getMessage());
}
}