Skip navigation
2011

Shobhit Tyagi's Blog

June 2011 Previous month Next month

I created this web application for using workflow to demonstrate approval process. Following is some part of the code that might help in integrating jBPM 3 with a web app.

 

Some of the methods are :

 

1)- Deploy :

 

public String reDeployProcess(String processName)

    {  

        jbpmConfiguration = JbpmConfiguration.parseResource("jbpm.cfg.xml");

        JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); 

        System.out.println("1");

        ProcessDefinition processDefinition = ProcessDefinition.parseXmlResource(processName); 

        System.out.println("2");

        System.out.println(processDefinition);

        jbpmContext.deployProcessDefinition(processDefinition); 

        System.out.println("3");

        System.out.println("prcoess getting deployed is :"+processDefinition.getName());

        jbpmContext.close();

        System.out.println(processDefinition.getName());

        return processDefinition.getName();

 

 

    }

 

2)- Starting Process Instance :

 

public String startNewProcessInstance(String processDefinitionName)

{

JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); 

GraphSession graphSession = jbpmContext.getGraphSession(); 

ProcessDefinition definition = graphSession.findLatestProcessDefinition(processDefinitionName); 

System.out.println("definition is "+definition);

ProcessInstance instance = definition.createProcessInstance(); 

long id = instance.getId(); 

instance.signal();

jbpmContext.save(instance); 

jbpmContext.close();

return ""+id; 

}

 

3)- Task List

 

public List<?> listAllTasks(String User)

{

JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();

jbpmContext.setActorId(User);

System.out.println(jbpmContext.getActorId());

List<?> list =  jbpmContext.getTaskList(User);

System.out.println(list.size());

return list;

}

 

4)- Signalling a Process.

 

public String signalProcess(String processDefinitionName, String id) { 

    JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); 

 

    long processId = Long.parseLong(id); 

    ProcessInstance instance = jbpmContext.loadProcessInstance(processId); 

    instance.signal();

    jbpmContext.save(instance); 

    jbpmContext.close();

    return "process signalled" ;

}

 

5)- Completing task

 

 

public String completeProcess(String processDefinitionName, String id, String User) { 

        JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();

long processId = Long.parseLong(id); 

        ProcessInstance instance = jbpmContext.loadProcessInstance(processId); 

        TaskInstance taskInstance = (TaskInstance) 

          instance.getTaskMgmtInstance().getTaskInstances().iterator().next();

        System.out.println(taskInstance);

        System.out.println(taskInstance.getActorId());

        taskInstance.end();

        jbpmContext.save(instance); 

        jbpmContext.close();

 

        return "process signalled" ;

    }

 

note : Task can also be completed using a transition. Something like this : taskInstance.end(transition);

 

 

The return values are being saved in the session here.

 

These methods can be called from the servlet by matching the front-end parameter values.

 

Regds

I had this requirement where jBPM process was supposed to be triggered at specified time. I would like to share it here.

 

1)-  2 methods for testing it :

 


public void deploy()

{


ProcessEngine processEngine = configuration.buildProcessEngine();


RepositoryService repositoryService = processEngine.getRepositoryService();


repositoryService.createDeployment().addResourceFromClasspath("test.jpdl.xml").deploy();

}




public void startProcess(String key)

{


ProcessEngine processEngine = configuration.buildProcessEngine();


ExecutionService executionService = processEngine.getExecutionService();


executionService.startProcessInstanceByKey(key);




}

 

2)- Creating a job that needs to be executed at the specified time :

 


public void execute(JobExecutionContext arg0) throws JobExecutionException {





Methods methods =  new Methods();


methods.startProcess("testS");


System.out.println("*******************STARTED******************");

}

 

3)- Creating a scheduler and a Trigger that would trigger the method in step 2.

 


public void startupScheduler() throws SchedulerException, ParseException

{


SchedulerFactory schedulerFactory = new StdSchedulerFactory();


Scheduler scheduler = schedulerFactory.getScheduler();


scheduler.start();





JobDetail jobDetail = new JobDetail("startProcessJob", Scheduler.DEFAULT_GROUP, StartProcessJob.class);





CronTrigger ct=new CronTrigger("cronTrigger","group2","00 49 13 22 * ?");


scheduler.scheduleJob(jobDetail, ct);

}

 

note 1: Here, the 3rd parameter in jobDetail is the class StartProcessJob implementing interface Job, that contains overridden method execute that is in step 2.

note 2: Last parameter in CronTrigger represents the date-time that is matched with system time for triggering it. (secs,mins,hours,date,month,year).

 

4)- Finally calling process deploy() and then calling method startupScheduler().

 

Hope this is helpful.

 

regds.