获取所有bean的方法,做定制化处理。

该文章讲述了如何实现Spring的BeanPostProcessor接口,对类对象的所有方法进行定制化处理,特别是在处理带有Recurring注解的定时任务时,检查方法参数,根据cron表达式调度或删除Job。通过反射和方法回调机制,动态管理Job的执行。
摘要由CSDN通过智能技术生成

实现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;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值