0

JobLancher次のように構成することで、ジョブを非同期的に開始できることは承知していますTaskExecutor

@Configuration
public class BatchConfig extends DefaultBatchConfigurer {

    @Bean
    public TaskExecutor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(20);
        executor.setQueueCapacity(200);
        executor.initialize();
        return executor;
    }

    @Override
    @Bean
    public JobLauncher getJobLauncher() {
        SimpleJobLauncher jobLauncher = null;
        try {
            jobLauncher = new SimpleJobLauncher();
            jobLauncher.setJobRepository(getJobRepository());
            jobLauncher.setTaskExecutor(getAsyncExecutor());
            jobLauncher.afterPropertiesSet();
        } catch (Exception e) {
            e.printStackTrace(...)
        }
        return jobLauncher;
    }
}

ただし、例として次のサービスがあるとします。

@Service
public class ProcessorService implements InitializingBean {

    private JobLauncher jobLauncher;
    private JobExplorer jobExplorer;
    private JobLocator jobLocator;

    public ProcessorService(final JobLocator jobLocator,
                         final JobExplorer jobExplorer,
                         final JobLauncher jobLauncher) {
        this.jobLocator = jobLocator;
        this.jobExplorer = jobExplorer;
        this.jobLauncher = jobLauncher;
    }
    
    public JobResultResponse process(JobRequest request, String trackingUUID)  {
        Job job = getJob(request);
        JobParameters parameters = buildJobParams(request, trackingUUID);
        JobExecution execution = kickOfJob(job, request, parameters);
        return provideAResponse(request, trackingUUID, execution); //previously return response but now perhaps just update the record in the database when it becomes async
    }

    private JobExecution kickOfJob(Job job, JobRequest request, JobParameters parameters) {
        JobExecution execution;
        try {
            execution = jobLauncher.run(job, parameters);         
            if (execution.getStatus() != COMPLETED) {
                //log exception
            }
        } catch (JobExecutionAlreadyRunningException e) {
            //****
        }
        return execution;
    }
    
}

process()そして、ジョブのキックを呼び出すコントローラー:

@RestController
@RequestMapping("/test")
public class TestController {

    private ProcessorService processor;
    private RequestDataRepo repo;

    public TestController(final ProcessorService  processor, final RequestDataRepo repo) {
        this.processor = processor;
        this.repo = repo;
    }

    @PostMapping(value = "/kickOfJob", consumes = MediaType.APPLICATION_JSON_VALUE)
    public HttpEntity<MyReponse> trigger(@Valid @RequestBody JobRequest request) {

        final String trackingId = UUID.randomUUID().toString();

        final RequestEntity entity = RequestEntity.builder()
                .trackingId(trackingId)
                .name(request.getName())
                .build();

        this.repo.save(entity);

        processor.process(request, trackingId); //async call, get execution id
        
        //create the response object for immediate return


        return new ResponseEntity<>(response, CREATED);
    }

}

すぐにprocess()作成に戻ることができるように、バックグラウンドで非同期に実行するにはどうすればよいですか? responseアプリで非同期TaskExecutorBean を既に作成しています。process()非同期を呼び出す方法がわかりません。process()ジョブの実行だけでなく、非同期全体が必要です。

4

0 に答える 0