Spring上的Async Rest方法不是异步执行的

时间:2017-04-17 14:57:37

标签: spring spring-boot spring-data-jpa spring-async

我正在运行启用了异步配置的Spring Boot。

这是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 - &gt; Callback - &gt; onSucess”之前看到“/ getLogCompacto - &gt; Fim”。我对吗?我缺少什么?

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;
    }
}

谢谢!

1 个答案:

答案 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。