实现org.springframework.beans.factory.config.BeanPostProcessor接口
重写后置处理。
对类对象的所有方法做定制化处理
ReflectionUtils.doWithMethods(bean.getClass(), this.recurringJobFinderMethodCallback);
参考
org.jobrunr.scheduling.RecurringJobPostProcessor
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
ReflectionUtils.doWithMethods(bean.getClass(), this.recurringJobFinderMethodCallback);
return bean;
}
public void afterPropertiesSet() {
this.recurringJobFinderMethodCallback = new RecurringJobPostProcessor.RecurringJobFinderMethodCallback(this.jobScheduler, this.embeddedValueResolver);
}
private static class RecurringJobFinderMethodCallback implements MethodCallback {
private final JobScheduler jobScheduler;
private final StringValueResolver embeddedValueResolver;
public RecurringJobFinderMethodCallback(JobScheduler jobScheduler, StringValueResolver resolver) {
this.jobScheduler = jobScheduler;
this.embeddedValueResolver = resolver;
}
public void doWith(Method method) throws IllegalArgumentException {
if (method.isAnnotationPresent(Recurring.class)) {
if (this.hasParametersOutsideOfJobContext(method)) {
throw new IllegalStateException("Methods annotated with " + Recurring.class.getName() + " can only have zero parameters or a single parameter of type JobContext.");
} else {
Recurring recurringAnnotation = (Recurring)method.getAnnotation(Recurring.class);
String id = this.getId(recurringAnnotation);
String cron = this.resolveStringValue(recurringAnnotation.cron());
if ("-".equals(cron)) {
if (id == null) {
RecurringJobPostProcessor.LOGGER.warn("You are trying to disable a recurring job using placeholders but did not define an id.");
} else {
this.jobScheduler.delete(id);
}
} else {
JobDetails jobDetails = this.getJobDetails(method);
CronExpression cronExpression = CronExpression.create(cron);
ZoneId zoneId = this.getZoneId(recurringAnnotation);
this.jobScheduler.scheduleRecurrently(id, jobDetails, cronExpression, zoneId);
}
}
}
}
private boolean hasParametersOutsideOfJobContext(Method method) {
if (method.getParameterCount() == 0) {
return false;
} else if (method.getParameterCount() > 1) {
return true;
} else {
return !method.getParameterTypes()[0].equals(JobContext.class);
}
}
private String getId(Recurring recurringAnnotation) {
String id = this.resolveStringValue(recurringAnnotation.id());
return StringUtils.isNullOrEmpty(id) ? null : id;
}
private JobDetails getJobDetails(Method method) {
JobDetails jobDetails = new JobDetails(method.getDeclaringClass().getName(), (String)null, method.getName(), new ArrayList());
jobDetails.setCacheable(true);
return jobDetails;
}
private ZoneId getZoneId(Recurring recurringAnnotation) {
String zoneId = this.resolveStringValue(recurringAnnotation.zoneId());
return StringUtils.isNullOrEmpty(zoneId) ? ZoneId.systemDefault() : ZoneId.of(zoneId);
}
private String resolveStringValue(String value) {
return this.embeddedValueResolver != null && value != null ? this.embeddedValueResolver.resolveStringValue(value) : value;
}
}