首页 > 代码库 > 流程任务-服务任务(Java Service Task)

流程任务-服务任务(Java Service Task)

服务任务用于请求流程外任务或自动执行任务,有3种实现:Java Service Task/Web Service /Task和shellTask,

当流程到达该任务时,执行相应的java类或webservice或shell命令。

1.Java Service Task

  1.1使用activiti:class指定java类,该类必须实现JavaDelegate,activiti每次都会创建新的实例。

public class MyJavaDelegate implements JavaDelegate{    public void execute(DelegateExecution execution) throws Exception {        System.out.println(" 实现  JavaDelegate 的  JavaSeviceTask: " + this);    }}

流程文件全名限定delete类

<serviceTask id="servicetask1" name="Service Task 1"    <!--MyJavaDelegate必须是JavaDelegate实现类-->    activiti:class="org.crazyit.activiti.MyJavaDelegate"></serviceTask>

  1.2使用delegateExpression指定java类,该类必须实现JavaDelegate和Serializable

  a.创建java service task类

public class MyJavaDelegate implements JavaDelegate, Serializable {    public void execute(DelegateExecution execution) throws Exception {        System.out.println(" 实现  JavaDelegate 的  JavaSeviceTask: " + this);    }}

  b.流程文件juel表达式

<serviceTask id="servicetask1" name="Service Task"    activiti:delegateExpression="${myDelegate}"></serviceTask>

  c.实例的创建由提供者提供,需将实例设置到流程参数中:

  // 部署流程文件    
  repositoryService.createDeployment().addClasspathResource("bpmn/JUELClass.bpmn").deploy();
  Map<String, Object> vars = new HashMap<String, Object>(); vars.put("myDelegate", new MyJavaDelegate()); // 启动流程 ProcessInstance pi = runtimeService.startProcessInstanceByKey("process1", vars);

  1.3 使用activiti:expression 调用普通的java bean

    a.创建java bean

public class MyJavaBean implements Serializable {        public String getName() {        return "bestmyself"    }    public void print(Execution exe) {        System.out.println("使用Java Bean的print方法:" + exe.getId());    }}

  b.流程文件结合juel调用对象方法

<serviceTask id="servicetask1" name="Service Task"    <!--执行mybean的print方法-->    activiti:expression="${myBean.print(execution)}"></serviceTask>    <serviceTask id="servicetask2" name="Service Task"    <!--执行mybean.getName()方法-->    activiti:expression="${execution.setVariable(‘myName‘, myBean.name)}"></serviceTask>

  c.设置java bean 实例到流程中

  

    Map<String, Object> vars = new HashMap<String, Object>();    vars.put("myBean", new MyJavaBean());    // 启动流程    ProcessInstance pi = runtimeService.startProcessInstanceByKey("process1", vars);    // 进行任务参数查询    System.out.println(  runtimeService.getVariable(pi.getId(), "myName"));

 

流程任务-服务任务(Java Service Task)