Write a simple Activiti Client

Ok. Now the process is completed. We will add a minimal API to start the Process and claim the Tasks generated. 

package com.mastertheboss.activiti.DemoActiviti;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
 
public class App 
{
    public static void main( String[] args )
    {
    	 final ProcessEngine processEngine = ProcessEngineConfiguration
                 .createStandaloneInMemProcessEngineConfiguration()
                 .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE)
                 .setJobExecutorActivate(true)
                 .setDatabaseSchemaUpdate("drop-create")
                 .buildProcessEngine();

         final RepositoryService repositoryService = processEngine
                 .getRepositoryService();
         repositoryService.createDeployment()
                 .addClasspathResource("DemoProcess.bpmn").deploy();
         final Map<String, Object> variables = new HashMap<String, Object>();

 
         final RuntimeService runtimeService = processEngine.getRuntimeService();
       
         ProcessInstance id = runtimeService.startProcessInstanceByKey("myProcess", variables);
         System.out.println("Started Process Id: "+id.getId());
         
         try {
             final TaskService taskService = processEngine.getTaskService();
             final List<Task> tasksSenior = taskService.createTaskQuery().taskAssignee("kermit").list();
             final List<Task> tasksJunior = taskService.createTaskQuery().taskAssignee("gonzo").list();
           
     
             if (tasksSenior.size() > 0) {
                  System.out.println("Found task for Senior " +tasksSenior.get(0).getName() );
                 taskService.claim(tasksSenior.get(0).getId(), "kermit");
 
                 System.out.println("Task " +tasksSenior.get(0).getName() +" terminated");
             }
             else if (tasksJunior.size() > 0) {
                 System.out.println("Found task for Junior " +tasksJunior.get(0).getName() );
                 taskService.claim(tasksJunior.get(0).getId(), "gonzo");

                System.out.println("Task " +tasksJunior.get(0).getName() +" terminated");
            }
             
         } catch (Exception e) {
        	 System.out.println(e.getMessage());
         } finally {

         }
          processEngine.close();
    }
}

As you can see, the central starting point is the ProcessEngine. From the ProcessEngine, you can obtain the various services that contain the workflow/BPM methods.

Typically, there will be only one ProcessEngine instance needed in a end-user application. Building a ProcessEngine is done through a ProcessEngineBuilder instance and is a costly operation which should be avoided. For that purpose, it is advised to store it in a static field or JNDI location (or something similar).
 
Once started the class, an instance of the ProcessInstance will be created. The TaskService API is then used to retrieve the list of Tasks which can be claimed by the users "kermit" and "gonzo". Depending on the Random number generated, the Task can be claimed by one of the two users, therefore a typical output of the execution would look like this:

This is a simple task!
Started Process Id: 5
Found task for Junior SimpleTask
Task SimpleTask terminated

You can download the source code for this article at: https://github.com/fmarchioni/mastertheboss/tree/master/activiti/standalone/DemoActiviti

0
0
0
s2smodern