魔改xxl-job,彻底告别手动配置任务!( 四 )

@XxlJob注解的方法

  • 对上面获取到的方法进行检查,是否添加了我们自定义的@XxlRegister注解,如果没有则跳过,不进行自动注册
  • 对同时添加了@XxlJob@XxlRegister的方法,通过执行器id和jobHandler的值判断是否已经在调度中心注册过了,如果已存在则跳过
  • 对于满足注解条件且没有注册过的jobHandler,调用接口注册到调度中心
  • 具体代码如下:
    private void addJobInfo() {List<XxlJobGroup> jobGroups = jobGroupService.getJobGroup();XxlJobGroup xxlJobGroup = jobGroups.get(0);String[] beanDefinitionNames = applicationContext.getBeanNamesForType(Object.class, false, true);for (String beanDefinitionName : beanDefinitionNames) {Object bean = applicationContext.getBean(beanDefinitionName);Map<Method, XxlJob> annotatedMethods= MethodIntrospector.selectMethods(bean.getClass(),new MethodIntrospector.MetadataLookup<XxlJob>() {@Overridepublic XxlJob inspect(Method method) {return AnnotatedElementUtils.findMergedAnnotation(method, XxlJob.class);}});for (Map.Entry<Method, XxlJob> methodXxlJobEntry : annotatedMethods.entrySet()) {Method executeMethod = methodXxlJobEntry.getKey();XxlJob xxlJob = methodXxlJobEntry.getValue();//自动注册if (executeMethod.isAnnotationPresent(XxlRegister.class)) {XxlRegister xxlRegister = executeMethod.getAnnotation(XxlRegister.class);List<XxlJobInfo> jobInfo = jobInfoService.getJobInfo(xxlJobGroup.getId(), xxlJob.value());if (!jobInfo.isEmpty()){//因为是模糊查询,需要再判断一次Optional<XxlJobInfo> first = jobInfo.stream().filter(xxlJobInfo -> xxlJobInfo.getExecutorHandler().equals(xxlJob.value())).findFirst();if (first.isPresent())continue;}XxlJobInfo xxlJobInfo = createXxlJobInfo(xxlJobGroup, xxlJob, xxlRegister);Integer jobInfoId = jobInfoService.addJobInfo(xxlJobInfo);}}}}4、自动装配创建一个配置类,用于扫描bean
    @Configuration@ComponentScan(basePackages = "com.xxl.job.plus.executor")public class XxlJobPlusConfig {}将它添加到META-INF/spring.factories文件:
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.xxl.job.plus.executor.config.XxlJobPlusConfig到这里starter的编写就完成了,可以通过maven发布jar包到本地或者私服:
    mvn clean install/deploy测试新建一个springboot项目,引入我们在上面打好的包:
    <dependency><groupId>com.cn.hydra</groupId><artifactId>xxljob-autoregister-spring-boot-starter</artifactId><version>0.0.1</version></dependency>application.properties中配置xxl-job的信息,首先是原生的配置内容:
    xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-adminxxl.job.accessToken=default_tokenxxl.job.executor.appname=xxl-job-executor-testxxl.job.executor.address=xxl.job.executor.ip=127.0.0.1xxl.job.executor.port=9999xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandlerxxl.job.executor.logretentiondays=30此外还要额外添加我们自己的starter要求的新配置内容:
    # admin用户名xxl.job.admin.username=admin# admin 密码xxl.job.admin.password=123456# 执行器名称xxl.job.executor.title=test-title完成后在代码中配置一下XxlJobSpringExecutor,然后在测试接口上添加原生@XxlJob注解和我们自定义的@XxlRegister注解:
    @XxlJob(value = "https://www.huyubaike.com/biancheng/testJob")@XxlRegister(cron = "0 0 0 * * ? *",author = "hydra",jobDesc = "测试job")public void testJob(){System.out.println("#公众号:码农参上");}@XxlJob(value = "https://www.huyubaike.com/biancheng/testJob222")@XxlRegister(cron = "59 1-2 0 * * ?",triggerStatus = 1)public void testJob2(){System.out.println("#作者:Hydra");}@XxlJob(value = "https://www.huyubaike.com/biancheng/testJob444")@XxlRegister(cron = "59 59 23 * * ?")public void testJob4(){System.out.println("hello xxl job");}

    经验总结扩展阅读