作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个用 @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/
嘿。本周的一个教程,其中一个问题要求通过使用其他函数 formatLine 和 formatList 创建一个函数 formatLines,以格式化行列表。 我的代码是这样的; type Line =
我是一名优秀的程序员,十分优秀!