diff --git a/bootstrap/src/main/java/org/apache/shardingsphere/elasticjob/bootstrap/type/OneOffJobBootstrap.java b/bootstrap/src/main/java/org/apache/shardingsphere/elasticjob/bootstrap/type/OneOffJobBootstrap.java index dc0599ccdd..31cfe784d1 100644 --- a/bootstrap/src/main/java/org/apache/shardingsphere/elasticjob/bootstrap/type/OneOffJobBootstrap.java +++ b/bootstrap/src/main/java/org/apache/shardingsphere/elasticjob/bootstrap/type/OneOffJobBootstrap.java @@ -30,7 +30,7 @@ /** * One off job bootstrap. */ -public final class OneOffJobBootstrap implements JobBootstrap { +public class OneOffJobBootstrap implements JobBootstrap { private final JobScheduler jobScheduler; diff --git a/docs/content/user-manual/operation/dump.cn.md b/docs/content/user-manual/operation/dump.cn.md index 75c2c1515d..0f103fec2f 100644 --- a/docs/content/user-manual/operation/dump.cn.md +++ b/docs/content/user-manual/operation/dump.cn.md @@ -11,8 +11,9 @@ chapter = true ## 开启监听端口 -使用 Java 开启导出端口配置请参见[Java API 使用指南](/cn/user-manual/elasticjob/usage/job-api/java-api)。 -使用 Spring 开启导出端口配置请参见[Spring 使用指南](/cn/user-manual/elasticjob/usage/job-api/spring-namespace)。 +使用 Java API 开启导出端口配置请参见[Java API 作业信息导出配置](/cn/user-manual/elasticjob/configuration/java-api)。 +使用 Spring Boot Starter 开启导出端口配置请参见[Spring Boot Starter 作业信息导出配置](/cn/user-manual/elasticjob/configuration/java-api)。 +使用 Spring 命名空间 开启导出端口配置请参见[Spring 命名空间 作业信息导出配置](/cn/user-manual/elasticjob/configuration/spring-namespace)。 ## 执行导出命令 diff --git a/docs/content/user-manual/operation/dump.en.md b/docs/content/user-manual/operation/dump.en.md index 23458b90ff..95ebbaa9b7 100644 --- a/docs/content/user-manual/operation/dump.en.md +++ b/docs/content/user-manual/operation/dump.en.md @@ -12,8 +12,9 @@ For security reason, the information dumped had already mask sensitive informati ## Open Listener Port -Using Java API please refer to [Java API usage](/en/user-manual/elasticjob/usage/job-api/java-api) for more details. -Using Spring please refer to [Spring usage](/en/user-manual/elasticjob/usage/job-api/spring-namespace) for more details. +To open listener port using Java, refer to [Java API job information export configuration](/en/user-manual/elasticjob/configuration/java-api). +To open listener port using Spring Boot Starter, refer to [Spring Boot Starter job information export configuration](/en/user-manual/elasticjob/configuration/java-api). +To open listener port using Spring Namespace, refer to [Spring namespace job information export configuration](/en/user-manual/elasticjob/configuration/spring-namespace). ## Execute Dump diff --git a/docs/content/user-manual/usage/job-api/spring-boot-starter.cn.md b/docs/content/user-manual/usage/job-api/spring-boot-starter.cn.md index e21e6f849d..73576879bb 100644 --- a/docs/content/user-manual/usage/job-api/spring-boot-starter.cn.md +++ b/docs/content/user-manual/usage/job-api/spring-boot-starter.cn.md @@ -8,6 +8,9 @@ ElasticJob 提供自定义的 Spring Boot Starter,可以与 Spring Boot 配合 基于 ElasticJob Spring Boot Starter 使用 ElasticJob ,用户无需手动创建 CoordinatorRegistryCenter、JobBootstrap 等实例, 只需实现核心作业逻辑并辅以少量配置,即可利用轻量、无中心化的 ElasticJob 解决分布式调度问题。 +以下内容仅通过 Spring Boot 3 作为演示。 +相关内容在 Spring Boot 2 上仍可能有效,但由于 Spring Boot 2 已经结束维护,不对 Spring Boot 2 做任何可用性假设。 + ## 作业配置 ### 实现作业逻辑 @@ -74,6 +77,8 @@ elasticjob: 一次性调度的作业的执行权在开发者手中,开发者可以在需要调用作业的位置注入 `OneOffJobBootstrap`, 通过 `execute()` 方法执行作业。 +用户不应该使用 `jakarta.annotation.Resource` 等部分违反 Spring Boot 最佳实践的注解来注入定义的一次性任务的 Spring Bean。 + `OneOffJobBootstrap` bean 的名称通过属性 jobBootstrapBeanName 配置,注入时需要指定依赖的 bean 名称。 具体配置请参考[配置文档](/cn/user-manual/elasticjob/configuration/spring-boot-starter)。 @@ -81,32 +86,35 @@ elasticjob: elasticjob: jobs: myOneOffJob: + elasticJobType: SCRIPT jobBootstrapBeanName: myOneOffJobBean - .... + shardingTotalCount: 9 + props: + script.command.line: "echo Manual SCRIPT Job: " ``` ```java -@RestController -public class OneOffJobController { +import org.apache.shardingsphere.elasticjob.bootstrap.type.OneOffJobBootstrap; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; - // 通过 "@Resource" 注入 - @Resource(name = "myOneOffJobBean") - private OneOffJobBootstrap myOneOffJob; - - @GetMapping("/execute") - public String executeOneOffJob() { - myOneOffJob.execute(); - return "{\"msg\":\"OK\"}"; - } +import java.util.Objects; +@RestController +public class OneOffJobController { // 通过 "@Autowired" 注入 @Autowired - @Qualifier(name = "myOneOffJobBean") - private OneOffJobBootstrap myOneOffJob2; + @Qualifier("myOneOffJobBean") + private ObjectProvider myOneOffJobProvider; @GetMapping("/execute2") public String executeOneOffJob2() { - myOneOffJob2.execute(); + OneOffJobBootstrap myOneOffJob = myOneOffJobProvider.getIfAvailable(); + Objects.requireNonNull(myOneOffJob); + myOneOffJob.execute(); return "{\"msg\":\"OK\"}"; } } diff --git a/docs/content/user-manual/usage/job-api/spring-boot-starter.en.md b/docs/content/user-manual/usage/job-api/spring-boot-starter.en.md index 2592c27092..901389477b 100644 --- a/docs/content/user-manual/usage/job-api/spring-boot-starter.en.md +++ b/docs/content/user-manual/usage/job-api/spring-boot-starter.en.md @@ -8,6 +8,9 @@ ElasticJob provides a customized Spring Boot Starter, which can be used in conju Developers are free from configuring CoordinatorRegistryCenter, JobBootstrap by using ElasticJob Spring Boot Starter. What developers need to solve distributed scheduling problem are job implementations with a little configuration. +The following content is only demonstrated through Spring Boot 3. +The relevant content may still be valid on Spring Boot 2, but since Spring Boot 2 has ended maintenance, no availability assumptions are made for Spring Boot 2. + ## Job configuration ### Implements ElasticJob @@ -76,6 +79,8 @@ When to execute OneOffJob is up to you. Developers can inject the `OneOffJobBootstrap` bean into where they plan to invoke. Trigger the job by invoking `execute()` method manually. +Users should not use annotations such as `jakarta.annotation.Resource` which partially violate Spring Boot best practices to inject Spring beans that define one-time tasks. + The bean name of `OneOffJobBootstrap` is specified by property "jobBootstrapBeanName", Please refer to [Spring Boot Starter Configuration](/en/user-manual/elasticjob/configuration/spring-boot-starter). @@ -83,32 +88,35 @@ Please refer to [Spring Boot Starter Configuration](/en/user-manual/elasticjob/c elasticjob: jobs: myOneOffJob: + elasticJobType: SCRIPT jobBootstrapBeanName: myOneOffJobBean - .... + shardingTotalCount: 9 + props: + script.command.line: "echo Manual SCRIPT Job: " ``` ```java -@RestController -public class OneOffJobController { +import org.apache.shardingsphere.elasticjob.bootstrap.type.OneOffJobBootstrap; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; - // Inject via "@Resource" - @Resource(name = "myOneOffJobBean") - private OneOffJobBootstrap myOneOffJob; - - @GetMapping("/execute") - public String executeOneOffJob() { - myOneOffJob.execute(); - return "{\"msg\":\"OK\"}"; - } +import java.util.Objects; - // Inject via "@Autowired" +@RestController +public class OneOffJobController { + // 通过 "@Autowired" 注入 @Autowired - @Qualifier(name = "myOneOffJobBean") - private OneOffJobBootstrap myOneOffJob2; + @Qualifier("myOneOffJobBean") + private ObjectProvider myOneOffJobProvider; @GetMapping("/execute2") public String executeOneOffJob2() { - myOneOffJob2.execute(); + OneOffJobBootstrap myOneOffJob = myOneOffJobProvider.getIfAvailable(); + Objects.requireNonNull(myOneOffJob); + myOneOffJob.execute(); return "{\"msg\":\"OK\"}"; } }