HomeCode Reference › Workflow snippets

Workflow snippets

Purpose: Process-step skeleton and the programmatic workflow operations that come up in automation.

Who this page is for

AudienceWhy it matters to you
Backend engineersWorkflow builders

Process step

@Component(service = WorkflowProcess.class,
           property = {"process.label=PHI Premium Validation"})
public class PremiumValidationProcess implements WorkflowProcess {

    @Reference private ResourceResolverFactory resolverFactory;

    @Override
    public void execute(WorkItem item, WorkflowSession wfSession, MetaDataMap args)
            throws WorkflowException {
        String payloadPath = item.getWorkflowData().getPayload().toString();
        String minPremium = args.get("PROCESS_ARGS", "0");   // configured on the step

        Map<String, Object> auth = Map.of(ResourceResolverFactory.SUBSERVICE, "wf-validate");
        try (ResourceResolver rr = resolverFactory.getServiceResourceResolver(auth)) {
            Resource page = rr.getResource(payloadPath + "/jcr:content");
            if (page == null) throw new WorkflowException("Payload missing: " + payloadPath);
            double premium = page.getValueMap().get("monthlyPremium", 0.0);
            if (premium < Double.parseDouble(minPremium)) {
                throw new WorkflowException("Premium below configured minimum");  // fails the step, visibly
            }
        } catch (LoginException e) {
            throw new WorkflowException("Service login failed", e);
        }
    }
}

Start a workflow programmatically

WorkflowSession wfSession = resolver.adaptTo(WorkflowSession.class);
WorkflowModel model = wfSession.getModel("/var/workflow/models/phi-plan-approval");
WorkflowData data = wfSession.newWorkflowData("JCR_PATH", "/content/phi/plans/gold-hospital");
wfSession.startWorkflow(model, data, Map.of("initiatorComment", "Automated premium refresh"));

Query stuck/running instances (ops automation)

Workflow[] running = wfSession.getAllWorkflows();   // filter by model id + state + age
// or via JMX: com.adobe.granite.workflow MBeans for counts per model — feed monitoring

Reminders

Steps idempotent (retry is a feature — semantics); payload validated; service users per step purpose; long external work → enqueue a Sling Job and complete.

Quick navigation