xdxTao opened a new issue #1917:
URL: https://github.com/apache/shardingsphere-elasticjob/issues/1917
**MyJob**
```java
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.springframework.stereotype.Component;
@Component
public class MyElasticJob implements SimpleJob {
@Override
public void execute(ShardingContext context) {
System.out.println("--------------------");
System.out.println(context.getShardingTotalCount() + " " +
context.getShardingItem());
System.out.println("--------------------");
}
}
```
<br/>
**MyYML**
```yml
elasticjob:
regCenter:
serverLists: 127.0.0.1:2181
namespace: my-job2
jobs:
simpleJob:
elasticJobClass: com.elastic.job.MyElasticJob
cron: 0/1 * * * * ?
shardingTotalCount: 2
jobBootstrapBeanName: myElasticJob
```
<br/>
**MyController**
```java
import
org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.OneOffJobBootstrap;
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;
import javax.annotation.Resource;
@RestController
public class OneOffJobController {
// 通过 "@Resource" 注入
@Resource(name = "myElasticJob")
private OneOffJobBootstrap myOneOffJob;
@GetMapping("/execute")
public String executeOneOffJob() {
myOneOffJob.execute();
return "{\"msg\":\"OK\"}";
}
// 通过 "@Autowired" 注入
@Autowired
@Qualifier("myElasticJob")
private OneOffJobBootstrap myOneOffJob2;
@GetMapping("/execute2")
public String executeOneOffJob2() {
myOneOffJob2.execute();
return "{\"msg\":\"OK\"}";
}
}
```
<br/>
**StratError**
```
Could not register object
[org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.ScheduleJobBootstrap@5e1fc2aa]
under bean name 'myElasticJob': there is already object
[com.elastic.job.MyElasticJob@688d411b] bound
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]