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

原因是調度將觸發只有一個事件,這將是一個線程來執行,然後我看不出你是產卵多線程在你的邏輯並行執行。 jobExecutionService.execute();在JobScheduledExecutionService的execute()中是在一個線程中。總體來說,它最終成爲順序執行。

似乎您需要在JobExecutionService中執行多線程[基於Callable-Future的]邏輯:execute()來選擇作業[transactionalJobExecutionService.getJob()]並調用其中的executeJob()。希望這有助於..

相關問題