I'm working on a Service class which is annotated with @RequestScope, the problem is that there is one method which takes too long time to proceed and I'm wondering if it's possible to create new Thread in which part of the code will be executed.
I've tried using ManagedExecutorService, CompletableFuture.runAsync(()) and a simple Thread, but none of them seems to work?
@RequestScoped
public class OfferService
And the method:
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;
}
And I want the if part to be run asynchronous? //
callHistoricalDataUpload(malo.getOfferNo(), malo.getProcessId());
I believe the reason that it's not working is because the class is annotated with @RequestScope and after it returns the response is being destroyed and it's context too? When I try to simply create a new Thread:
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
What I have tried:
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();
None of that worked