Skip to content

Commit e363e6e

Browse files
author
令狐兄
committed
添加手动执行任务
1 parent a039122 commit e363e6e

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

src/main/java/org/linghuxiong/spring/batch/admin/controller/JobController.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
import org.linghuxiong.spring.batch.admin.model.TriggerEntity;
55
import org.linghuxiong.spring.batch.admin.service.JobService;
66
import org.quartz.SchedulerException;
7+
import org.springframework.batch.core.JobParametersInvalidException;
8+
import org.springframework.batch.core.launch.NoSuchJobException;
9+
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
10+
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
11+
import org.springframework.batch.core.repository.JobRestartException;
712
import org.springframework.beans.factory.annotation.Autowired;
813
import org.springframework.data.domain.Page;
914
import org.springframework.data.domain.PageRequest;
@@ -58,4 +63,11 @@ public Page<JobEntity> loadTriggerPageable(@RequestParam(required = false) Integ
5863

5964
}
6065

66+
@PostMapping("/launch")
67+
public String launch(@RequestParam Long jobId) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, NoSuchJobException {
68+
jobService.launch(jobId);
69+
return "success";
70+
}
71+
72+
6173
}

src/main/java/org/linghuxiong/spring/batch/admin/service/JobService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import org.linghuxiong.spring.batch.admin.model.JobEntity;
44
import org.linghuxiong.spring.batch.admin.model.TriggerEntity;
55
import org.quartz.SchedulerException;
6+
import org.springframework.batch.core.JobParametersInvalidException;
7+
import org.springframework.batch.core.launch.NoSuchJobException;
8+
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
9+
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
10+
import org.springframework.batch.core.repository.JobRestartException;
611
import org.springframework.data.domain.Page;
712
import org.springframework.data.domain.Pageable;
813

@@ -16,4 +21,5 @@ public interface JobService {
1621
void delete(Long jobId);
1722
JobEntity toggleStatus(Long jobId,Integer status) throws SchedulerException;
1823
Page<JobEntity> loadJobPageable(Pageable pageable, String name,Integer type,Integer status,String triggerName,String springJobName);
24+
void launch(Long jobId) throws NoSuchJobException, JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException;
1925
}

src/main/java/org/linghuxiong/spring/batch/admin/service/impl/JobServiceImpl.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
import org.quartz.JobKey;
77
import org.quartz.Scheduler;
88
import org.quartz.SchedulerException;
9+
import org.springframework.batch.core.JobParameters;
10+
import org.springframework.batch.core.JobParametersBuilder;
11+
import org.springframework.batch.core.JobParametersInvalidException;
12+
import org.springframework.batch.core.configuration.JobRegistry;
13+
import org.springframework.batch.core.launch.JobLauncher;
14+
import org.springframework.batch.core.launch.NoSuchJobException;
15+
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
16+
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
17+
import org.springframework.batch.core.repository.JobRestartException;
918
import org.springframework.beans.factory.annotation.Autowired;
1019
import org.springframework.data.domain.Page;
1120
import org.springframework.data.domain.Pageable;
@@ -33,6 +42,12 @@ public class JobServiceImpl implements JobService {
3342
@Autowired
3443
private Scheduler scheduler ;
3544

45+
@Autowired
46+
JobLauncher jobLauncher;
47+
48+
@Autowired
49+
JobRegistry jobRegistry;
50+
3651
@Override
3752
public Page<JobEntity> loadJobPageable(Pageable pageable, String name, Integer type, Integer status, String triggerName, String springJobName) {
3853
Specification<JobEntity> queryCondition = new Specification<JobEntity>() {
@@ -65,6 +80,12 @@ public Predicate toPredicate(Root<JobEntity> root, CriteriaQuery<?> criteriaQuer
6580
return jobDao.findAll(queryCondition, pageable);
6681
}
6782

83+
@Override
84+
public void launch(Long jobId) throws NoSuchJobException, JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
85+
JobEntity jobEntity = jobDao.getOne(jobId);
86+
jobLauncher.run(jobRegistry.getJob(jobEntity.getSpringJobName()),new JobParametersBuilder().addDate("#CURRENNCY",new Date()).toJobParameters());
87+
}
88+
6889
@Override
6990
public JobEntity save(JobEntity jobEntity) {
7091
jobEntity.setUpdatedAt(new Date());
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2019-07-25 16:33:52.708 INFO 11667 --- [ restartedMain] org.linghuxiong.batch.BatchApplication : Starting BatchApplication on ericdeMacBook-Pro.local with PID 11667 (/Users/eric/Documents/dev/linghuxiong/spring-boot-demo/batch/target/classes started by eric in /Users/eric/Documents/dev/linghuxiong/spring-boot-demo/batch)
1+
2019-07-25 16:33:52.708 INFO 11667 --- [ restartedMain] org.linghuxiong.batch.BatchApplication : Starting BatchApplication on

0 commit comments

Comments
 (0)