@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");}
经验总结扩展阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 牙龈炎怎么办
- 在爱情中容易彻底沦陷的星座
- 新版微信怎么彻底删除微信好友方法(微信如何迁移微信好友)
- 如何彻底的忘记前男友
- 车上有跳蚤怎么彻底消灭
- 电脑弹窗广告怎么彻底关闭
- 金喜善 金喜善怎么变成这样!最新亮相换新发型扮嫩,五官大变彻底认不出!
- 磁石面膜怎么清洗?
- 如何彻底清理微信缓存图片视频垃圾?
- 发型 中美这场较量,美国彻底完败!当着世界的面,崔天凯对美发出强音