How to perform a parallel CMIS requests in Alfresco?

I have several CMIS requests and need to do them in parallel. But when I try it (using CompletableFutire or stream().parallel(), I got:

 java.util.concurrent.ExecutionException: net.sf.acegisecurity.AuthenticationCredentialsNotFoundException: A valid SecureContext was not provided in the RequestContext

For that queries I do :

 @Autowired
    @Qualifier("searchService")
    private org.alfresco.service.cmr.search.SearchService searchService;
....
 searchService.query(searchParameters);

What am I doing wrong? The following code is one of my attempt to perform CMIS in parallel:

    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;
        });
    }

Everything works fine in serial execution

Upvotes: 2

Views: 242

Answers (1)

JuanH
JuanH

Reputation: 51

Security context is binded to main thread and is not propagated to children threads, you would need to set one. Maybe you could try to adapt this snippet:

CompletableFuture.runAsync(() -> {
    try {
        AuthenticationUtil.pushAuthentication();
        AuthenticationUtil.setFullyAuthenticatedUser(userName);
        // Do your stuff...
    } finally {
        AuthenticationUtil.popAuthentication();
    }
});

Upvotes: 2

Related Questions