You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2021/06/30 03:46:00 UTC
[GitHub] [shardingsphere-elasticjob] xdxTao opened a new issue #1916: I don't understand how to configure one-time scheduling in springboot
xdxTao opened a new issue #1916:
URL: https://github.com/apache/shardingsphere-elasticjob/issues/1916
**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: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [shardingsphere-elasticjob] xdxTao closed issue #1916: I don't understand how to configure one-time scheduling in springboot
Posted by GitBox <gi...@apache.org>.
xdxTao closed issue #1916:
URL: https://github.com/apache/shardingsphere-elasticjob/issues/1916
--
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: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org