gpt4 book ai didi

java - 是否可以在 @RequestScope bean 中创建新线程

转载 作者:行者123 更新时间:2023-12-02 05:19:03 29 4
gpt4 key购买 nike

我正在开发一个用 @RequestScope 注释的 Service 类,问题是有一个方法需要很长时间才能继续,我想知道是否可以在该部分的哪一部分创建新线程代码将被执行。

我尝试过使用 ManagedExecutorService、CompletableFuture.runAsync(())和一个简单的线程,但它们似乎都不起作用?

@RequestScoped
public class OfferService

方法:

  public List<DTO> createLocation(List<DTO> locationAdressDTOS) {
List<DTO> lokationLookupList ;

locationLookupList = offerDao.createMarktlokation(DTOS.get(0).getOfferNo(), DTOS);

DTOS.forEach(malo -> {
if (BooleanUtils.isTrue(malo.isShouldUploadHistoricalData()) && malo.getProcessId() != null) {
callHistoricalDataUpload(malo.getOfferNo(), malo.getProcessId());
}
});
return lokationLookupList;
}

我希望 if 部分异步运行?//

 callHistoricalDataUpload(malo.getOfferNo(), malo.getProcessId());

我相信它不起作用的原因是因为该类用 @RequestScope 进行了注释,并且在它返回后,响应被破坏了,它的上下文也被破坏了?当我尝试简单地创建一个新线程时:

2019-05-23 14:45:31,934 ERROR [stderr] (Thread-225) Exception in thread "Thread-225" org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped

我尝试过的:

CompletableFuture.runAsync(() -> {
try {
this.uploadHistoricalData(offerNo, processId);
} catch (DatatypeConfigurationException | ParseException e) {
logger.severe(e.getMessage());
}
});


managedExecutorService.execute(() -> {
try {
this.uploadHistoricalData(offerNo, processId);

} catch (DatatypeConfigurationException | ParseException e) {
logger.severe(e.getMessage());
}
});


new Thread((() -> {
try {

this.uploadHistoricalData(offerNo, processId);

} catch (DatatypeConfigurationException | ParseException e) {
logger.severe(e.getMessage());
}
})).start();

这些都不起作用

最佳答案

看来你的问题和这个other question on StackOverflow一样.

It's because the request is tied to the server thread processing the request. When you switch threads, there is no longer access to that request context.

您可能不需要 @RequestScoped 注释。

关于java - 是否可以在 @RequestScope bean 中创建新线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56276831/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com