我有多个 CMIS 请求,需要并行执行。但是当我尝试它时(使用CompletableFutire或stream().parallel(),我得到了:
java.util.concurrent.ExecutionException: net.sf.acegisecurity.AuthenticationCredentialsNotFoundException: A valid SecureContext was not provided in the RequestContext
对于这个查询,我这样做:
@Autowired
@Qualifier("searchService")
private org.alfresco.service.cmr.search.SearchService searchService;
....
searchService.query(searchParameters);
我做错了什么? 以下代码是我并行执行 CMIS 的尝试之一:
List<CompletableFuture<Form14Row>> requests = Arrays.asList(setUpRow(1,beginnigString, endString, docType, NDBaseDocumentModel.DOC_KIND_GOST_R, searchParameters, "ГОСТ Р"),
setUpRow(2,beginnigString, endString, docType, NDBaseDocumentModel.DOC_KIND_GOST, searchParameters, "ГОСТ") );
CompletableFuture<Void> allRequests = CompletableFuture.allOf(
requests.toArray(new CompletableFuture[requests.size()])
);
CompletableFuture<List<Form14Row>> allPageContentsFuture = allRequests.thenApply(v -> {
return requests.stream()
.map(pageContentFuture -> pageContentFuture.join())
.collect(Collectors.toList());
});
//java.util.concurrent.ExecutionException: net.sf.acegisecurity.AuthenticationCredentialsNotFoundException: A valid SecureContext was not provided in the RequestContext
try {
List<Form14Row> rowss = allPageContentsFuture.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
...
private CompletableFuture<Form14Row> setUpRow(Integer index, String beginnigString, String endString, String docType, String docKind, SearchParameters searchParameters, String groupPosition) {
return CompletableFuture.supplyAsync(() -> {
String cql = "SELECT p.cmis:objectId FROM ecmcnddoc:common_attr_aspect AS d JOIN ecmcnddoc:biblio_attr_aspect AS p ON d.cmis:objectId = p.cmis:objectId JOIN ecmcnddoc:reg_attr_aspect AS s ON s.cmis:objectId = p.cmis:objectId JOIN ecmcnddoc:spec_attr_aspect AS asp ON asp.cmis:objectId = p.cmis:objectId WHERE p.cmis:objectTypeId='D:" + docType + "' AND d.ecmcnddoc:doc_kind_cp_ecmcdict_value='" + docKind + "' AND p.ecmcnddoc:biblio_fond='" + NDBaseDocumentModel.BIBLIO_FUND + "' AND s.ecmcnddoc:doc_reg_date >= TIMESTAMP '" + beginnigString + "T00:00:00.000+00:00' AND s.ecmcnddoc:doc_reg_date <= TIMESTAMP '" + endString + "T00:00:00.000+00:00' AND (asp.ecmcnddoc:doc_status='draft' OR asp.ecmcnddoc:doc_status='actual')";
searchParameters.setQuery(cql);
ResultSet rs = customSearchService.query(searchParameters); // Exception here
Form14Row isoRow = new Form14Row();
isoRow.setCount(rs.length());
isoRow.setIndex(index);
isoRow.setKindName(groupPosition);
return isoRow;
});
}
串行执行时一切正常
最佳答案
安全上下文绑定(bind)到主线程,不会传播到子线程,您需要设置一个。 也许你可以尝试改编这个片段:
CompletableFuture.runAsync(() -> {
try {
AuthenticationUtil.pushAuthentication();
AuthenticationUtil.setFullyAuthenticatedUser(userName);
// Do your stuff...
} finally {
AuthenticationUtil.popAuthentication();
}
});
关于alfresco - 如何在 Alfresco 中执行并行 CMIS 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63706988/