Yesterday, a group of people reported an error similar to the following when writing a test timed task based on the previous post “Implementing timed tasks with Elastic Job” :
Caused by: org.apache.shardingsphere.elasticjob.infra.exception.JobConfigurationException: Job conflict with register center. The job 'my-simple-job' in register center's class is 'com.didispace.chapter72.MySimpleJob', your job class is 'com.didispace.chapter74.MySimpleJob' at org.apache.shardingsphere.elasticjob.lite.internal.config.ConfigurationService.checkConflictJob(ConfigurationService.jav A: 86) ~ [elasticjob - lite - core - 3.0.0. Jar: 3.0.0] the at org.apache.shardingsphere.elasticjob.lite.internal.config.ConfigurationService.setUpJobConfiguration(ConfigurationServic E.j ava: 70) ~ [elasticjob - lite - core - 3.0.0. Jar: 3.0.0] the at org.apache.shardingsphere.elasticjob.lite.internal.setup.SetUpFacade.setUpJobConfiguration(SetUpFacade.java:66) ~ [elasticjob - lite - core - 3.0.0. Jar: 3.0.0] the at org.apache.shardingsphere.elasticjob.lite.internal.schedule.JobScheduler.<init>(JobScheduler.java:84) ~ [elasticjob - lite - core - 3.0.0. Jar: 3.0.0] the at org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.ScheduleJobBootstrap.<init>(ScheduleJobBootstrap.java:36) ~ [elasticjob - lite - core - 3.0.0. Jar: 3.0.0] the at org.apache.shardingsphere.elasticjob.lite.spring.boot.job.ElasticJobBootstrapConfiguration.registerClassedJob(ElasticJob BootstrapConfiguration. Java: 101) ~ [elasticjob - lite - spring - the boot - starter - 3.0.0. Jar: 3.0.0] the at org.apache.shardingsphere.elasticjob.lite.spring.boot.job.ElasticJobBootstrapConfiguration.constructJobBootstraps(Elasti CJobBootstrapConfiguration. Java: 84) ~ [elasticjob - lite - spring - the boot - starter - 3.0.0. Jar: 3.0.0] the at org.apache.shardingsphere.elasticjob.lite.spring.boot.job.ElasticJobBootstrapConfiguration.createJobBootstrapBeans(Elast IcJobBootstrapConfiguration. Java: 57) ~ [elasticjob - lite - spring - the boot - starter - 3.0.0. Jar: 3.0.0] the at Sun. Reflect. NativeMethodAccessorImpl. Invoke0 (Native Method) ~ [na: 1.8.0 comes with _151] the at Sun. Reflect. NativeMethodAccessorImpl. Invoke (NativeMethodAccessorImpl. Java: 62) ~ [na: 1.8.0 comes with _151] the at Sun. Reflect. DelegatingMethodAccessorImpl. Invoke (43) DelegatingMethodAccessorImpl. Java: ~ [na: 1.8.0 comes with _151] the at Java. Lang. Reflect. Method. Invoke (498) Method. The Java: ~ [na: 1.8.0 comes with _151] the at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyA NnotationBeanPostProcessor. Java: 389) ~ [spring - beans - 5.3.8. Jar: 5.3.8] the at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods( InitDestroyAnnotationBeanPostProcessor. Java: 333) ~ [spring - beans - 5.3.8. Jar: 5.3.8] the at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(Init DestroyAnnotationBeanPostProcessor. Java: 157) ~ [spring - beans - 5.3.8. Jar: 5.3.8]... 17 common frames omittedCopy the code
Error message Job conflict with register center. The Job ‘my-simple-job’ in register Center’s. The task name is the same, but the implementation class is different.
After some communication, it turned out that he used the ZooKeeper example of the company’s test environment to write the test. At the same time, a colleague (also a DD reader) wrote similar tasks before. Because the configured task names were copied, the task names were opposite, but the implementation classes were different.
In fact, if we were working on a larger team and there were multiple systems, the naming of timed tasks would probably happen. For example, many applications may have some tasks to periodically clear some resources, so they may have the same name, register with the same ZooKeeper, and finally conflict occurs. So is there a good way to solve this problem?
Method 1: Manage task creation in a unified manner
The most primitive approach is to centrally manage the task creation process. For example, you can open a Wiki page and register all tasks on this page. When each person registers, they can check whether the name they think of already exists. If it exists, think of another name and register it.
This method is simple and easy to understand. The problem is that when you have a very, very large number of tasks, the page content is very large and cumbersome to maintain.
Method 2: Use the namespace attribute of the Elastic Job to isolate the task name
Recall from the first article about scheduled tasks that there are two registry configurations:
elasticjob.reg-center.server-lists=localhost:2181
elasticjob.reg-center.namespace=didispace
Copy the code
[elasticJob. Reg-center. Server-lists] [elasticJob. Reg-center. The second parameter is elasticJob.reg-center.namespace.
In fact, when registering tasks in ZooKeeper, the real conflict is not only because of the task name, but also because of the namespace + task name.
Therefore, we just need to isolate the tasks created by each application in its own separate namespace, so that there will be no conflicts with other applications?
Finally, I offer the following advice:
spring.application.name=chapter74
elasticjob.reg-center.server-lists=localhost:2181
elasticjob.reg-center.namespace=${spring.application.name}
Copy the code
That is, set the name of elasticJob.reg-center. namespace of the scheduled task service to the same as that of the current Spring Boot application: Spring.application. name.
Usually, we plan each Spring Boot application with uniqueness, so that we can ensure uniqueness when registering with Eureka, Nacos and other registries in the future.
Taking advantage of this unique parameter can also conveniently help us to isolate the scheduled tasks of each application, which solves the scenario we mentioned at the beginning of this article.
Spring Boot 2.x Basic Tutorial , welcome to collect and forward! If you encounter difficulties in learning? You can join our Spring technology exchange group, participate in exchange and discussion, better learning and progress!
Code sample
The full project for this article can be viewed in the chapter7-4 directory in the repository below:
- Github:github.com/dyc87112/Sp…
- Gitee:gitee.com/didispace/S…
If you found this article good, welcomeStar
Support, your attention is my motivation!
Welcome to pay attention to my public account: program ape DD, share the outside can not see the dry goods and thinking!