一、Activiti介绍与搭建开发环境
二、运行官方例子
三、编写第一个Activiti程序
3.1 流程部署
代码
@Test
public void test2() {
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = engine.getRepositoryService();
repositoryService.createDeployment().addClasspathResource("flow/test1.bpmn20.xml").deploy();
}
涉及到的表
act_ge_bytearray
act_re_deployment
act_re_procdef
3.2 流程发起
代码
@Test
public void test3() {
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = engine.getRuntimeService();
ProcessInstance instance = runtimeService.startProcessInstanceByKey("myTest1");
}
涉及到的表
act_hi_actinst
act_hi_procinst
act_hi_taskinst
act_ru_execution
act_ru_task
3.3 进行查询任务且进行审批
@Test
public void test4() {
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = engine.getRuntimeService();
ProcessInstance instance = runtimeService.startProcessInstanceByKey("myTest1");
TaskService taskService = engine.getTaskService();
Task task = taskService.createTaskQuery().processInstanceId(instance.getId()).singleResult();
System.out.println("当前流程节点:" + task.getName());
taskService.complete(task.getId());
task = taskService.createTaskQuery().processInstanceId(instance.getId()).singleResult();
System.out.println("当前流程节点:" + task.getName());
taskService.complete(task.getId());
task = taskService.createTaskQuery().processInstanceId(instance.getId()).singleResult();
System.out.println("当前流程节点:" + task);
engine.close();
System.exit(0);
}
四、API
4.1 数据查询
@Test
public void test1() {
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
IdentityService identityService = engine.getIdentityService();
for (int i = 0; i < 10; i++) {
Group group = identityService.newGroup(String.valueOf(i));
group.setName("group_"+i);
group.setType("TYPE_"+ i);
identityService.saveGroup(group);
}
engine.close();
System.exit(0);
}
package com.demo;
import java.util.Iterator;
import java.util.List;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.identity.Group;
import org.junit.Test;
public class TestActiviti02 {
@Test
public void test1() {
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
IdentityService identityService = engine.getIdentityService();
for (int i = 0; i < 10; i++) {
Group group = identityService.newGroup(String.valueOf(i));
group.setName("group_"+i);
group.setType("TYPE_"+ i);
identityService.saveGroup(group);
}
engine.close();
System.exit(0);
}
@Test
public void test2() {
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
IdentityService identityService = engine.getIdentityService();
List<Group> groups = identityService.createGroupQuery().list();
for (Group group : groups) {
System.out.println(group.getId() + "---" + group.getName() + "---" + group.getType());
}
}
@Test
public void test3() {
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
IdentityService identityService = engine.getIdentityService();
List<Group> groups = identityService.createGroupQuery().listPage(0,2);
for (Group group : groups) {
System.out.println(group.getId() + "---" + group.getName() + "---" + group.getType());
}
}
@Test
public void test4() {
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
IdentityService identityService = engine.getIdentityService();
long count = identityService.createGroupQuery().count();
System.out.println(count);
}
@Test
public void test5() {
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
IdentityService identityService = engine.getIdentityService();
List<Group> groups = identityService.createGroupQuery().orderByGroupName().desc().list();
for (Group group : groups) {
System.out.println(group.getId());
}
List<Group> list = identityService.createGroupQuery().orderByGroupId().desc().orderByGroupName().asc().list();
for (Group group : list) {
System.out.println(group.getId());
}
}
@Test
public void test6() {
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
IdentityService identityService = engine.getIdentityService();
Group group = identityService.createGroupQuery().groupName("group_0").singleResult();
System.out.println(group.getId());
}
@Test
public void test7() {
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
IdentityService identityService = engine.getIdentityService();
List<Group> list = identityService.createGroupQuery().groupType("TYPE_2").list();
for (Group group : list) {
System.out.println(group.getName());
}
}
@Test
public void test8() {
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
IdentityService identityService = engine.getIdentityService();
List<Group> list = identityService.createGroupQuery().groupType("TYPE_2").groupName("group_2").list();
for (Group group : list) {
System.out.println(group.getName());
}
}
@Test
public void test9() {
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
IdentityService identityService = engine.getIdentityService();
List<Group> list = identityService.createNativeGroupQuery()
.sql("select * from act_id_group where name_ = #{name}")
.parameter("name", "group_2")
.list();
for (Group group : list) {
System.out.println(group.getId());
}
}
}
4.2 流程部署
package com.demo;
import java.io.IOException;
import java.io.InputStream;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.junit.Test;
public class TestActiviti03 {
public void test1() {
}
public void test2() {
}
@Test
public void test3() {
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
RepositoryService rs = engine.getRepositoryService();
DeploymentBuilder builder = rs.createDeployment();
builder.addClasspathResource("flow/error.bpmn20.xml");
builder.disableSchemaValidation();
builder.deploy();
}
@Test
public void test4() {
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
RepositoryService rs = engine.getRepositoryService();
DeploymentBuilder builder = rs.createDeployment();
builder.addClasspathResource("flow/process_error.bpmn20.xml");
builder.disableBpmnValidation();
builder.deploy();
}
@Test
public void test5() throws IOException {
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
RepositoryService rs = engine.getRepositoryService();
DeploymentBuilder builder = rs.createDeployment();
builder.addClasspathResource("flow/a.txt");
Deployment deploy = builder.deploy();
InputStream is = rs.getResourceAsStream(deploy.getId(), "flow/a.txt");
int count = is.available();
byte[] contents = new byte[count];
is.read(contents);
String result = new String(contents);
System.out.println(result);
}
@Test
public void test6() throws IOException {
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
RepositoryService rs = engine.getRepositoryService();
DeploymentBuilder builder = rs.createDeployment();
builder.addClasspathResource("flow/test1.bpmn20.xml");
Deployment deploy = builder.deploy();
ProcessDefinition def = rs.createProcessDefinitionQuery()
.deploymentId(deploy.getId()).singleResult();
InputStream is = rs.getProcessModel(def.getId());
int count = is.available();
byte[] contents = new byte[count];
is.read(contents);
String result = new String(contents);
System.out.println(result);
}
@Test
public void test7() {
}
@Test
public void test8() {
}
}