Skip to content

Commit

Permalink
Fixes the issue that OneOffJobBootstrap cannot be used under ElasticJ…
Browse files Browse the repository at this point in the history
…ob Spring Boot Starter
  • Loading branch information
linghengqian committed Aug 10, 2024
1 parent e240fbb commit d9e2cdb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
/**
* One off job bootstrap.
*/
public final class OneOffJobBootstrap implements JobBootstrap {
public class OneOffJobBootstrap implements JobBootstrap {

private final JobScheduler jobScheduler;

Expand Down
5 changes: 3 additions & 2 deletions docs/content/user-manual/operation/dump.cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

## 执行导出命令

Expand Down
5 changes: 3 additions & 2 deletions docs/content/user-manual/operation/dump.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
38 changes: 23 additions & 15 deletions docs/content/user-manual/usage/job-api/spring-boot-starter.cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 做任何可用性假设。

## 作业配置

### 实现作业逻辑
Expand Down Expand Up @@ -74,39 +77,44 @@ elasticjob:
一次性调度的作业的执行权在开发者手中,开发者可以在需要调用作业的位置注入 `OneOffJobBootstrap`,
通过 `execute()` 方法执行作业。

用户不应该使用 `jakarta.annotation.Resource` 等部分违反 Spring Boot 最佳实践的注解来注入定义的一次性任务的 Spring Bean。

`OneOffJobBootstrap` bean 的名称通过属性 jobBootstrapBeanName 配置,注入时需要指定依赖的 bean 名称。
具体配置请参考[配置文档](/cn/user-manual/elasticjob/configuration/spring-boot-starter)。

```yaml
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<OneOffJobBootstrap> myOneOffJobProvider;
@GetMapping("/execute2")
public String executeOneOffJob2() {
myOneOffJob2.execute();
OneOffJobBootstrap myOneOffJob = myOneOffJobProvider.getIfAvailable();
Objects.requireNonNull(myOneOffJob);
myOneOffJob.execute();
return "{\"msg\":\"OK\"}";
}
}
Expand Down
40 changes: 24 additions & 16 deletions docs/content/user-manual/usage/job-api/spring-boot-starter.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -76,39 +79,44 @@ 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).

```yaml
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<OneOffJobBootstrap> myOneOffJobProvider;
@GetMapping("/execute2")
public String executeOneOffJob2() {
myOneOffJob2.execute();
OneOffJobBootstrap myOneOffJob = myOneOffJobProvider.getIfAvailable();
Objects.requireNonNull(myOneOffJob);
myOneOffJob.execute();
return "{\"msg\":\"OK\"}";
}
}
Expand Down

0 comments on commit d9e2cdb

Please sign in to comment.