Search in sources :

Example 1 with ResourceRunnable

use of com.adobe.acs.commons.util.visitors.ResourceRunnable in project acs-aem-commons by Adobe-Consulting-Services.

the class ReplicateWithOptionsWorkflowProcess method execute.

@Override
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
    ResourceResolver resourceResolver = null;
    final long start = System.currentTimeMillis();
    try {
        resourceResolver = workflowHelper.getResourceResolver(workflowSession);
        final String originalPayload = (String) workItem.getWorkflowData().getPayload();
        final List<String> payloads = workflowPackageManager.getPaths(resourceResolver, originalPayload);
        final ProcessArgs processArgs = new ProcessArgs(metaDataMap);
        final AtomicInteger count = new AtomicInteger(0);
        // Anonymous inner class to facilitate counting of processed payloads
        final ResourceRunnable replicatorRunnable = new ResourceRunnable() {

            @Override
            public void run(final Resource resource) throws Exception {
                if (processArgs.isThrottle()) {
                    throttledTaskRunner.waitForLowCpuAndLowMemory();
                }
                replicator.replicate(resource.getResourceResolver().adaptTo(Session.class), processArgs.getReplicationActionType(), resource.getPath(), processArgs.getReplicationOptions(resource));
                count.incrementAndGet();
            }
        };
        final ContentVisitor visitor = new ContentVisitor(replicatorRunnable);
        for (final String payload : payloads) {
            final Resource resource = resourceResolver.getResource(payload);
            if (processArgs.isTraverseTree()) {
                // Traverse the tree
                visitor.accept(resource);
            } else {
                // Only execute on the provided payload
                replicatorRunnable.run(resource);
            }
        }
        log.info("Replicate with Options processed [ {} ] total payloads in {} ms", count.get(), System.currentTimeMillis() - start);
    } catch (Exception e) {
        throw new WorkflowException(e);
    }
}
Also used : ContentVisitor(com.adobe.acs.commons.util.visitors.ContentVisitor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WorkflowException(com.day.cq.workflow.WorkflowException) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) WorkflowException(com.day.cq.workflow.WorkflowException) ResourceRunnable(com.adobe.acs.commons.util.visitors.ResourceRunnable) WorkflowSession(com.day.cq.workflow.WorkflowSession) Session(javax.jcr.Session)

Example 2 with ResourceRunnable

use of com.adobe.acs.commons.util.visitors.ResourceRunnable in project acs-aem-commons by Adobe-Consulting-Services.

the class SyntheticWrapperWorkflowProcess method execute.

@Override
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
    ResourceResolver resourceResolver = null;
    final SyntheticWorkflowRunner syntheticWorkflowRunner = syntheticWorkflowRunnerAccessor.getSyntheticWorkflowRunner();
    final String payload = (String) workItem.getWorkflowData().getPayload();
    final ProcessArgs processArgs = new ProcessArgs(metaDataMap);
    try {
        resourceResolver = workflowHelper.getResourceResolver(workflowSession);
        final SyntheticWorkflowModel syntheticWorkflowModel = syntheticWorkflowRunner.getSyntheticWorkflowModel(resourceResolver, processArgs.getWorkflowModelId(), true);
        final AtomicInteger count = new AtomicInteger(0);
        // Anonymous inner class to facilitate counting of processed payloads
        final ResourceRunnable syntheticRunnable = new ResourceRunnable() {

            @Override
            public void run(final Resource resource) throws java.lang.Exception {
                if (processArgs.isThrottle()) {
                    throttledTaskRunner.waitForLowCpuAndLowMemory();
                }
                syntheticWorkflowRunner.execute(resource.getResourceResolver(), resource.getPath(), syntheticWorkflowModel, false, false);
                // Commit as needed
                if (processArgs.getSaveInterval() > 0 && count.incrementAndGet() % processArgs.getSaveInterval() == 0 && resource.getResourceResolver().hasChanges()) {
                    resource.getResourceResolver().commit();
                }
            }
        };
        final ContentVisitor visitor = new ContentVisitor(syntheticRunnable);
        final Resource resource = resourceResolver.getResource(payload);
        if (processArgs.isTraverseTree()) {
            visitor.accept(resource);
        } else {
            syntheticRunnable.run(resource);
        }
        if (processArgs.getSaveInterval() > 0 && resourceResolver.hasChanges()) {
            // Commit any stranglers
            resourceResolver.commit();
        }
        log.info("Synthetic Workflow Wrapper processed [ {} ] total payloads", count.get());
    } catch (Exception e) {
        throw new WorkflowException(e);
    }
}
Also used : ContentVisitor(com.adobe.acs.commons.util.visitors.ContentVisitor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SyntheticWorkflowRunner(com.adobe.acs.commons.workflow.synthetic.SyntheticWorkflowRunner) WorkflowException(com.day.cq.workflow.WorkflowException) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) WorkflowException(com.day.cq.workflow.WorkflowException) SyntheticWorkflowModel(com.adobe.acs.commons.workflow.synthetic.SyntheticWorkflowModel) ResourceRunnable(com.adobe.acs.commons.util.visitors.ResourceRunnable)

Aggregations

ContentVisitor (com.adobe.acs.commons.util.visitors.ContentVisitor)2 ResourceRunnable (com.adobe.acs.commons.util.visitors.ResourceRunnable)2 WorkflowException (com.day.cq.workflow.WorkflowException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Resource (org.apache.sling.api.resource.Resource)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 SyntheticWorkflowModel (com.adobe.acs.commons.workflow.synthetic.SyntheticWorkflowModel)1 SyntheticWorkflowRunner (com.adobe.acs.commons.workflow.synthetic.SyntheticWorkflowRunner)1 WorkflowSession (com.day.cq.workflow.WorkflowSession)1 Session (javax.jcr.Session)1