1

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

这是休息控制器:

@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 信息 2776 --- [nio-8080-exec-4] bccdcLogCentralLojaController : /getLogCompacto -> Início
2017-04-17 11:26:31.641 信息 2776 --- [nio-8080-exec-4] bccdcLogCentralLojaController : /getLogCompacto -> 回调 -> onSucess
2017-04-17 11:26:31.641 信息 2776 --- [nio-8080-exec-4] bccdcLogCentralLojaController : /getLogCompacto -> Fim

我期待在“/getLogCompacto -> Callback -> onSucess”之前看到“/getLogCompacto -> 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;
    }
}

谢谢!

4

1 回答 1

0

像这样更改 spring boot 配置解决了这个问题:

@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。

于 2017-04-17T16:22:16.680 回答