demo自帶的例子不符合現在項目的業務需求,摸索了下,總算做出個能跑的,感覺很多地方的用法還是有些不規範,不過好歹跑起來了。
開發環境:
JDK1.6
Activiti5.13
Eclipse
MYSQL5
簡單起見,這裏沒有做一個web的項目,只是一個Java程序。
業務場景說明:
預算填報員選取期間填寫預算表單,表單是與流程獨立的,因此在流程節點只記錄表單的ID,預算填報員填報完後,提交領導審批,審批人是通過前臺選擇,也就是流程運行中動態指定的,審批時顯示錶單不同於填報表單,審批結束後流程結束。
表單的屬性包括填報單位。
流程實例中記錄的變量有
period 填報期間
任務節點記錄的變量有
accountCode 賬號編碼
1. 畫流程圖
設置流程id
設置節點表單id
填寫預算節點表單id爲sheet1,預算審批節點的表單id爲sheet2
2. 編寫監聽,更新流程圖
流程運行時,究竟流向何人,是通過程序來確定,而不是固定的一個人或者一個角色組。
因此這裏使用添加監聽的方式來添加下一個節點的執行人。
只是做一個demo方便起見,我在流程執行實例中添加了一個變量nextAssignee,用來存儲流程的下個執行人,監聽的代碼如下:
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class GetNextAssigneeHandler implements TaskListener {
/**
*
*/
private static final long serialVersionUID = 3385759189120775989L;
@Override
public void notify(DelegateTask task) {
// 加載配置
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
ProcessEngine processEngine = (ProcessEngine) ctx
.getBean("processEngine");
RuntimeService runtimeService = processEngine.getRuntimeService();
// 獲取存儲在流程實例中的nextAssignee變量的值
String next = (String) runtimeService.getVariable(
task.getExecutionId(), "nextAssignee");
// 設置節點的下個執行人
task.setAssignee(next);
}
}
修改流程圖兩個節點的配置,添加監聽,兩個節點用的是同一個監聽
3. 流程測試例子
kermit爲預算填報員,是從前臺傳入的
fozzie爲預算審批員,也是從前臺傳入的
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.FormService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.form.FormProperty;
import org.activiti.engine.task.Task;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestFinancial {
/**
* @param args
*/
public static void main(String[] args) {
// 加載spring配置
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
RuntimeService runtimeService = (RuntimeService) ctx
.getBean("runtimeService");
ProcessEngine processEngine = (ProcessEngine) ctx
.getBean("processEngine");
FormService formService = processEngine.getFormService();
// 發佈流程
RepositoryService repositoryService = processEngine
.getRepositoryService();
// Deploy the process definition
repositoryService.createDeployment()
.addClasspathResource("diagrams/FinancialReport.bpmn").deploy();
TaskService taskService = (TaskService) ctx.getBean("taskService");
// 流程起始參數,會保存在流程實例的變量中
Map<String, Object> map = new HashMap<String, Object>();
map.put("period", "2013-06");
map.put("nextAssignee", "kermit");
// 開始流程
runtimeService.startProcessInstanceByKey("myProcess", map);
// query kermit's tasks;
List<Task> tasks = taskService.createTaskQuery().taskAssignee("kermit")
.processVariableValueEquals("period", "2013-06").list();
for (Task task : tasks) {
if ("usertask1".equals(task.getTaskDefinitionKey())) {
System.out.println("任務已經成功分配給了kermit");
// 設置節點的填報賬號,這個值應該是要從前臺獲得的
taskService.setVariableLocal(task.getId(), "accountCode",
"A1106");
// 從節點中獲取表單ID
Map<String, FormProperty> propMap = createMap(formService
.getTaskFormData(task.getId()).getFormProperties());
String sheetId = propMap.get("sheetId").getValue();
// 從流程實例中獲取填報期間
String period = (String) runtimeService.getVariable(
task.getProcessInstanceId(), "period");
// 從節點實例中獲取賬戶編碼
String accountCode = (String) taskService.getVariable(
task.getId(), "accountCode");
// 打印填報信息
System.out.println("填報期間:" + period);
System.out.println("填報表單:" + sheetId);
System.out.println("填報單位:" + accountCode);
// 設置下個節點審批人
runtimeService.setVariable(task.getExecutionId(),
"nextAssignee", "fozzie");
// 節點任務結束
taskService.complete(task.getId());
System.out.println("kermit填寫預算單任務已完成");
}
}
// query fozzie's tasks;
List<Task> ftasks = taskService.createTaskQuery()
.taskAssignee("fozzie")
.processVariableValueEquals("period", "2013-06").list();
for (Task task : ftasks) {
taskService.setVariableLocal(task.getId(), "accountCode", "A1100");
if ("usertask2".equals(task.getTaskDefinitionKey())) {
System.out.println("任務已經成功分配給了fozzie");
// 設置節點的填報賬號,這個值應該是要從前臺獲得的
taskService.setVariableLocal(task.getId(), "accountCode",
"A1100");
// 從節點中獲取表單ID
Map<String, FormProperty> propMap = createMap(formService
.getTaskFormData(task.getId()).getFormProperties());
String sheetId = propMap.get("sheetId").getValue();
// 從流程實例中獲取填報期間
String period = (String) runtimeService.getVariable(
task.getProcessInstanceId(), "period");
// 從節點實例中獲取賬戶編碼
String accountCode = (String) taskService.getVariable(
task.getId(), "accountCode");
// 打印填報信息
System.out.println("填報期間:" + period);
System.out.println("填報表單:" + sheetId);
System.out.println("填報單位:" + accountCode);
// 節點任務結束
taskService.complete(task.getId());
System.out.println("fozzie審批任務已完成");
}
}
}
private static Map<String, FormProperty> createMap(List<FormProperty> props) {
Map<String, FormProperty> re = new HashMap<String, FormProperty>();
for (FormProperty p : props) {
re.put(p.getId(), p);
}
return re;
}
}
測試結果:
附applicationContext.xml,數據庫使用MYSQL5
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test_activiti" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Activiti begin -->
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseSchemaUpdate" value="true" />
<property name="jobExecutorActivate" value="false" />
<property name="history" value="full" />
<property name="processDefinitionCacheLimit" value="10" />
</bean>
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>
<bean id="repositoryService" factory-bean="processEngine"
factory-method="getRepositoryService" />
<bean id="runtimeService" factory-bean="processEngine"
factory-method="getRuntimeService" />
<bean id="formService" factory-bean="processEngine"
factory-method="getFormService" />
<bean id="identityService" factory-bean="processEngine"
factory-method="getIdentityService" />
<bean id="taskService" factory-bean="processEngine"
factory-method="getTaskService" />
<bean id="historyService" factory-bean="processEngine"
factory-method="getHistoryService" />
<bean id="managementService" factory-bean="processEngine"
factory-method="getManagementService" />
<!-- Activiti end -->
</beans>
項目引用jar包就是activiti-explorer裏用的所有jar包+mysql的驅動
END