2017-04-17 68 views
1

我正在運行Spring Boot並啓用了Async配置。Spring上的Async Rest方法不是異步執行的

這是休息控制器:

@RestController 
@RequestMapping("/logCentralLoja") 
@Component 
public class LogCentralLojaController { 

    private final LogCentralLojaCompactoRepository logRepo; 
    private final Logger logger = LoggerFactory.getLogger(this.getClass()); 

    @Autowired 
    public LogCentralLojaController(LogCentralLojaCompactoRepository logRepo) { 
     this.logRepo = logRepo; 
    } 

    @CrossOrigin 
    @RequestMapping(value = "/getLogCompacto", method = RequestMethod.GET) 
    public DeferredResult<ResponseEntity<?>> getLogCompacto(@PathParam(value = "cd_filial") int cd_filial, 
     @PathParam(value = "cd_log") long cd_log) { 

     DeferredResult<ResponseEntity<?>> deferredResult = new DeferredResult<>(); 

     logger.info("/getLogCompacto -> Início"); 

     ListenableFuture<List<LogCentralLojaCompactoEntity>> logsFuture = logRepo 
      .getByCdLogGreaterThanAndCdFilial(cd_log, cd_filial); 

     logsFuture.addCallback(
      new ListenableFutureCallback<List<LogCentralLojaCompactoEntity>>() { 
       @Override 
       public void onSuccess(List<LogCentralLojaCompactoEntity> logs) { 
        logger.info("/getLogCompacto -> Callback -> onSucess"); 

        if (logs.size() == 0) { 
         ResponseEntity<Void> not_modified = new ResponseEntity<>(HttpStatus.NOT_MODIFIED); 
         deferredResult.setResult(not_modified); 
        } else { 
         deferredResult.setResult(ResponseEntity.ok(new LogCentralCompactoLojaCollectionDto(logs))); 
        } 
       } 

       @Override 
       public void onFailure(Throwable t) { 
        logger.info("/getLogCompacto -> Callback -> onFailure"); 
        ResponseEntity<Void> responseEntity = new ResponseEntity<>(HttpStatus.SERVICE_UNAVAILABLE); 
        deferredResult.setResult(responseEntity); 
       } 
      } 
     ); 

     logger.info("/getLogCompacto -> Fim"); 

     return deferredResult; 
    } 
} 

這是日誌輸出中:

 
2017-04-17 11:26:30.410 INFO 2776 --- [nio-8080-exec-4] b.c.c.d.c.LogCentralLojaController  : /getLogCompacto -> Início 
2017-04-17 11:26:31.641 INFO 2776 --- [nio-8080-exec-4] b.c.c.d.c.LogCentralLojaController  : /getLogCompacto -> Callback -> onSucess 
2017-04-17 11:26:31.641 INFO 2776 --- [nio-8080-exec-4] b.c.c.d.c.LogCentralLojaController  : /getLogCompacto -> Fim 

我期待看到 「/ getLogCompacto - >魚翅」 前「/ getLogCompacto - >回調 - > onSucess「。我對嗎?我錯過了什麼?

的logRepo.getByCdLogGreaterThanAndCdFilial看起來是這樣的:

public interface LogCentralLojaCompactoRepository extends CrudRepository<LogCentralLojaCompactoEntity, Long>, 
    LogCentralLojaCompactoRepositoryCustom { 

    @Async 
    ListenableFuture<List<LogCentralLojaCompactoEntity>> getByCdLogGreaterThanAndCdFilial(long cdLog, int cdFilial); 
} 

異步配置:

@Configuration 
@EnableAsync 
public class AsyncConfiguration extends AsyncConfigurerSupport { 

    @Override 
    public Executor getAsyncExecutor() { 
     ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); 
     executor.setCorePoolSize(4); 
     executor.setMaxPoolSize(4); 
     executor.setQueueCapacity(40); 
     executor.setThreadNamePrefix("Thread-"); 
     executor.initialize(); 

     return executor; 
    } 
} 

謝謝!

+0

它不是異步運行,日誌中的所有條目都來自同一個線程。 Async的配置是什麼? – jny

+0

已更新爲異步配置。謝謝。 –

回答

0

改變這樣的春天啓動配置解決了這個問題:

@Bean 
public TaskExecutor getTaskExecutor() { 
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); 
    executor.setCorePoolSize(4); 
    executor.setMaxPoolSize(4); 
    executor.setQueueCapacity(40); 
    executor.setThreadNamePrefix("Thread-"); 
    executor.initialize(); 

    return executor; 
} 

類聲明有一個@EnableAsync。