0

スケジュールされたジョブが並行して実行されない理由を理解しようとしています。多分私の取引管理に何か問題がありますか?方法JobScheduledExecutionService.execute()@ScheduledfixedRate=250なので、前のジョブが終了しても250msごとに起動する必要があります。ログのため、期待通りに動作しません。スケジュールされたジョブが並行して実行されない理由

ログ:https://pastebin.com/M6FaXpeE

私のコードは以下の通りです。

@Service 
@Slf4j 
public class JobExecutionService { 

    private final TransactionalJobExecutionService transactionalJobExecutionService; 

    @Autowired 
    public JobExecutionService(TransactionalJobExecutionService transactionalJobExecutionService) { 
     this.transactionalJobExecutionService = transactionalJobExecutionService; 
    } 

    public void execute() { 
     TestJob job = transactionalJobExecutionService.getJob(); 
     executeJob(job); 
     transactionalJobExecutionService.finishJob(job); 
    } 

    private void executeJob(TestJob testJob) { 
     log.debug("Execution-0: {}", testJob.toString()); 
     Random random = new Random(); 
     try { 
      Thread.sleep(random.nextInt(3000) + 200); 
     } catch (InterruptedException e) { 
      log.error("Error", e); 
     } 
     log.debug("Execution-1: {}", testJob.toString()); 
    } 

} 

@Service 
@Slf4j 
public class JobScheduledExecutionService { 

    private final JobExecutionService jobExecutionService; 

    @Autowired 
    public JobScheduledExecutionService(JobExecutionService jobExecutionService) { 
     this.jobExecutionService = jobExecutionService; 
    } 

    @Scheduled(fixedRate = 250) 
    public void execute() { 
     log.trace("Job fired"); 
     jobExecutionService.execute(); 
    } 

} 

@Service 
@Slf4j 
@Transactional 
public class TransactionalJobExecutionService { 

    private final Environment environment; 
    private final TestJobRepository testJobRepository; 
    private final TestJobResultRepository testJobResultRepository; 

    @Autowired 
    public TransactionalJobExecutionService(Environment environment, TestJobRepository testJobRepository, TestJobResultRepository testJobResultRepository) { 
     this.environment = environment; 
     this.testJobRepository = testJobRepository; 
     this.testJobResultRepository = testJobResultRepository; 
    } 

    public TestJob getJob() { 
     TestJob testJob = testJobRepository.findFirstByStatusOrderByIdAsc(
       0 
     ); 
     testJob.setStatus(1); 
     testJobRepository.save(testJob); 
     return testJob; 
    } 

    public void finishJob(TestJob testJob) { 
     testJobResultRepository.save(
       new TestJobResult(
         null, 
         testJob.getId(), 
         environment.getProperty("local.server.port") 
       ) 
     ); 
    } 

} 

@Configuration 
public class SchedulingConfigurerConfiguration implements SchedulingConfigurer { 

    @Override 
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { 
     ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); 
     taskScheduler.setPoolSize(32); 
     taskScheduler.initialize(); 
     taskRegistrar.setTaskScheduler(taskScheduler); 
    } 

} 

答えて

0

理由は、スケジューラは、1つのスレッドによって実行され、その後、私はあなたが並列実行するために、あなたのロジックで複数のスレッドを生成しているが表示されないだけで一つのイベントを、発生しますです。その呼び出しはjobExecutionService.execute(); JobScheduledExecutionServiceのexecute()はそのスレッド内にあります。だから全体的に、それは逐次実行を終了します。

ジョブ[transactionalJobExecutionService.getJob()]を選択し、その中でexecuteJob()を呼び出すには、JobExecutionService:execute()にマルチスレッド[Callable-Future based]ロジックを配置する必要があるようです。これが助けてくれることを願って。

関連する問題