Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

WorflowProcess,Scheduler,JobManger and a JobConsumer in workflow

Avatar

Level 1

HI !

i have a problem that ive been trying to solve in my mind but its appears to be a little bit problematic.

the problem is that i need to created a workflow that will process some data from content fragments via payload and then that data will be use to launch multiple jobs/event with a different time, lets say for example that we have : cf = contentFragment

cfa - cron expre 1 = x | cron expre 2 = y 

cfb - cron expre 1 = d | cron expre 2 = c

cfb - cron expre 1 = s | cron expre 2 = a

this three cf  will be processed via payload path in the workflow custom step process, all of the cf will have two different cron expression that will represent the date when a property is gonna be published and the date when that property is gonna be unpublished. all those cron expressions can be different.

now thats the problem but for my solution i this steps :

  • 1.- create a custom adapter factory to adapt the cf  via adapTo in java.
  • 2.- create inside the customworkflow class a jobmanager with a job_topic to use
  • 3.- create a jobconsumer with a scheduler in it  and passing the required value to use via job properties map. 

now my problem comes in the third step, as by using this 

List<Cf> propertyListRetrieveFromPropertiesInJobProperties;

for (Cf object : propertyListRetrieveFromPropertiesInJobProperties) {

map = may with properties take from the Cf object

scheduler.scheduler(this,map)

}

now this solution is not working as the jobs are not starting or doing anything when the time in cron express happens.

can someone advise me on how to do this ?  my main problem is that i am not being able to see how to connect the workflowProcess and make that workflowProcess to launch the multiple jobs.

thanks in advance and any help will be greatly appreciated 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @workflownoob,

If I understand your case correctly, I think you should rather use scheduled jobs instead scheduler. I did a quick test, and was able to successfully create scheduled job from workflow process. The job was executed at given/scheduled time and was handled/consumed by dedicated job consumer. So your concept will change to this:

  1. Create a custom adapter factory to adapt the cf via adaptTo in java.
  2. Create inside the customworkflow class a jobmanager that will create scheduled job for given topic
  3. Create job consumer for given topic - it will be triggered automatically once specific job will run at time that was scheduled

From implementation point of view you will have something like this:

Workflow process

@Component(
        service = WorkflowProcess.class,
        property = "process.label=CustomWorkflowProcess"
)
public class CustomWorkflowProcess implements WorkflowProcess {

    @Reference
    private JobManager jobManager;

    @Override
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
      Map properties = // map with props you would like to pass to JobConsumer
      // depending on how would you like to schedule your job you can use different options than cron
      String cron = // cron expresssion
      createScheduldedJob(properties, cron);
    }

    private void createScheduldedJob(Map<String, Object> properties, String cron) {
        JobBuilder jobBuilder = jobManager.createJob("custom/topic");
        JobBuilder.ScheduleBuilder scheduleBuilder = jobBuilder.properties(properties).schedule();
        // you can also use other methods like at to schedule single execution for given date
        scheduleBuilder.cron(cron);
        if (scheduleBuilder.add() == null) {
            // error logging in case creation of scheduled job failed
        }
    }
}

Job consumer

@Component(
        immediate = true,
        service = JobConsumer.class,
        property = {
                JobConsumer.PROPERTY_TOPICS + "=custom/topic"
        }
)
public class CustomJobConsumer implements JobConsumer {

    @Override
    public JobResult process(Job job) {
        // do some processing
        return JobResult.OK;
    }
}

You can read more about sling jobs options under:

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @workflownoob,

If I understand your case correctly, I think you should rather use scheduled jobs instead scheduler. I did a quick test, and was able to successfully create scheduled job from workflow process. The job was executed at given/scheduled time and was handled/consumed by dedicated job consumer. So your concept will change to this:

  1. Create a custom adapter factory to adapt the cf via adaptTo in java.
  2. Create inside the customworkflow class a jobmanager that will create scheduled job for given topic
  3. Create job consumer for given topic - it will be triggered automatically once specific job will run at time that was scheduled

From implementation point of view you will have something like this:

Workflow process

@Component(
        service = WorkflowProcess.class,
        property = "process.label=CustomWorkflowProcess"
)
public class CustomWorkflowProcess implements WorkflowProcess {

    @Reference
    private JobManager jobManager;

    @Override
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
      Map properties = // map with props you would like to pass to JobConsumer
      // depending on how would you like to schedule your job you can use different options than cron
      String cron = // cron expresssion
      createScheduldedJob(properties, cron);
    }

    private void createScheduldedJob(Map<String, Object> properties, String cron) {
        JobBuilder jobBuilder = jobManager.createJob("custom/topic");
        JobBuilder.ScheduleBuilder scheduleBuilder = jobBuilder.properties(properties).schedule();
        // you can also use other methods like at to schedule single execution for given date
        scheduleBuilder.cron(cron);
        if (scheduleBuilder.add() == null) {
            // error logging in case creation of scheduled job failed
        }
    }
}

Job consumer

@Component(
        immediate = true,
        service = JobConsumer.class,
        property = {
                JobConsumer.PROPERTY_TOPICS + "=custom/topic"
        }
)
public class CustomJobConsumer implements JobConsumer {

    @Override
    public JobResult process(Job job) {
        // do some processing
        return JobResult.OK;
    }
}

You can read more about sling jobs options under: