Ø 部署流程定义(方式一):将设计的流程图发布到数据库中
public void run3() {
// 创建流程部署建造对象
DeploymentBuilder deployment = processEngine.getRepositoryService()
.createDeployment();
// 读取两个流程定义文件(png、pbmn)
deployment.addClasspathResource("qingjia.png");
deployment.addClasspathResource("qingjia.bpmn");
// 部署流程定义,返回值是
Deployment deploy = deployment.deploy();
System.out.println(deploy.getId());
}
Ø 部署流程定义(方式二):加载zip压缩文件
public void run4() throws Exception {
// 创建流程部署对象
DeploymentBuilder deploymentBuilder = processEngine
.getRepositoryService().createDeployment();
// 创建压缩文件输入流
// 类路径下(不常用)
/*
* ZipInputStream zip = new ZipInputStream(this.getClass()
* .getClassLoader().getResourceAsStream("qingjia.zip"));
*/
// 磁盘路径
ZipInputStream zip = new ZipInputStream(new FileInputStream(new File(
"D:/qingjia.zip")));
// 创建流程部署对象
deploymentBuilder.addZipInputStream(zip);
// 设置流程部署名称
deploymentBuilder.name("请假");
Deployment deploy = deploymentBuilder.deploy();
}
public void run5() {
// 获取流程部署查询对象
DeploymentQuery deploymentQuery = processEngine.getRepositoryService()
.createDeploymentQuery();
// 设置查询条件
deploymentQuery.deploymentName("请假");
// 结果进行排序
deploymentQuery.orderByDeploymentId().desc();
// 进行查询
// 分页查询
List
List
for (Deployment deployment : list) {
System.out.println(deployment.getId());
}
}
public void run6() {
// 创建流程定义查询对象
ProcessDefinitionQuery query = processEngine.getRepositoryService()
.createProcessDefinitionQuery();
List
.list();
for (ProcessDefinition processDefinition : list) {
System.out.println(processDefinition.getId());
}
}
public void run12(){
//方式一:根据流程定义ID启动
//定义定义id
String id = "myProcess:3:204";
//获取流程实例对象
//ProcessInstance instance = processEngine.getRuntimeService().startProcessInstanceById(id);
//System.out.println(instance.getId()+" "+instance.getBusinessKey());
//方式二 ,默认最新版本
//定义流程定义key
String key = "myProcess";
ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey(key);
System.out.println(pi.getId());
}
public void run13(){
//获取流程实例查询对象
ExecutionQuery query = processEngine.getRuntimeService().createExecutionQuery();
List
for (Execution execution : list) {
System.out.println(execution.getId()+":"+execution.getParentId());
}
}
public void run15(){
//获取任务查询对象
TaskQuery query = processEngine.getTaskService().createTaskQuery();
//设置查询条件 根据负责人
query.taskAssignee("张健");
List
for (Task task : list) {
System.out.println(task.getId()+":" + task.getAssignee());
}
}
public void run16(){
String taskId = "504";
processEngine.getTaskService().complete(taskId);
}
public void run10() throws IOException {
//定义部署 id
String id = "201";
//获取文件名称
List
.getDeploymentResourceNames("201");
for (String name : names) {
System.out.println(name);
//获取输入流
InputStream in = processEngine.getRepositoryService().getResourceAsStream(id, name);
FileUtils.copyInputStreamToFile(in, new File("e:\\"+name));
}
}
public void run11() throws IOException{
//定义流程id
String id = "myProcess:3:204";
//获取定义输入流
InputStream in = processEngine.getRepositoryService().getProcessDiagram(id);
FileUtils.copyInputStreamToFile(in, new File("e:\\abc.png"));
}
public void run8() {
// processEngine.getRepositoryService().deleteDeployment("1",true);级联删除
processEngine.getRepositoryService().deleteDeployment("1");
}
public void run9() {
//流程定义不能单独存在,通过删除部署表达到删除流程定义表的目的
// 定义了流程部署id
String id = "101";
processEngine.getRepositoryService().deleteDeployment(id);
}
public void run14(){
//设置要删除实例id
String id = "401";
//删除原因
String deleteReason = "不请假了";
processEngine.getRuntimeService().deleteProcessInstance(id, deleteReason);
ExecutionQuery query = processEngine.getRuntimeService().createExecutionQuery();
List
for (Execution execution : list) {
System.out.println(execution.getId());
}
}
几个Service对象:
Ø RepositoryService-----操作部署表、流程定义表、二进制表
Ø RuntimeService-----操作流程实例表、任务表
Ø TaskService-----操作任务表
Ø HistoryService-----操作历史表
Ø IdentityService-----操作用户身份相关的表
几个Query对象:
Ø DeploymentQuery---查询部署信息
Ø ProcessDefinitionQuery----查询流程定义信息
Ø ProcessInstanceQuery-----查询流程实例信息
Ø TaskQuery-----查询任务信息
几个接口(和表有对应关系):
Ø Deployment---部署信息
Ø ProcessDefinition----流程定义信息
Ø ProcessInstance-----流程实例信息
Ø Task-----任务信息
public void run1(){
HistoricProcessInstanceQuery query = processEngine.getHistoryService().createHistoricProcessInstanceQuery();
List
for (HistoricProcessInstance hi : list) {
System.out.println(hi.getId()+":"+hi.getProcessDefinitionId());
}
}
public void run2(){
HistoricTaskInstanceQuery query = processEngine.getHistoryService().createHistoricTaskInstanceQuery();
query.orderByProcessInstanceId().asc();
query.orderByHistoricTaskInstanceStartTime().asc();
List
for (HistoricTaskInstance hi : list) {
System.out.println("历史任务id:"+hi.getId()+" 流程实例id:"+hi.getProcessInstanceId());
}
}
public void run3(){
//历史活动查询对象
HistoricActivityInstanceQuery query = processEngine.getHistoryService().createHistoricActivityInstanceQuery();
query.orderByProcessInstanceId().asc();
query.orderByHistoricActivityInstanceStartTime().asc();
List
for (HistoricActivityInstance hi : list) {
System.out.println("历史活动id:"+hi.getId()+",历史活动名称:"+hi.getActivityName()+",历史流程实例id:"+hi.getProcessInstanceId());
}
}
Ø 方式一:当启动流程实例时设置流程变量
public void run1() {
// 设置流程定义key
String processDefinitionKey = "qj";
// 创建map集合
Map
variables.put("key1", "Tom");
variables.put("key2", 23);
ProcessInstance pi = engine.getRuntimeService()
.startProcessInstanceByKey(processDefinitionKey, variables);
}
Ø 方式二:当办理任务时设置流程变量
public void run2() {
//设置流程任务id
String taskId = "906";
Map
variables.put("Tkey1", "办理任务");
variables.put("Tkey2", "TaskService");
//执行任务
engine.getTaskService().complete(taskId, variables );
}
前两种方式会影响流程的推进。
Ø 方式三:使用RuntimeService的set方法设置流程变量
public void run3() {
//设置一个变量
String executionId = "901";
//engine.getRuntimeService().setVariable(executionId, "RuntimeService", "setVariable");
//设置多个变量
Map
variables.put("Runtime1", "设置多个");
variables.put("Runtime2", "setVariables");
engine.getRuntimeService().setVariables(executionId, variables );
}
方式四:使用TaskService的set方法设置流程变量
public void run4() {
String taskId = "1004";
//单个
//engine.getTaskService().setVariable(taskId, variableName, value);
//多个
Map
variables.put("Task1", "设置多个");
variables.put("Task2", "setVariables");
engine.getTaskService().setVariables(taskId, variables);
}
注意:如果需要存储自定义的实体类,需要实现序列化接口。存储的原则,怎么存怎么取。
Ø 方式一:使用RuntimeService的get方法获取流程变量
public void run1(){
String executionId = "901";
//获取指定一个
//Object value = engine.getRuntimeService().getVariable(executionId, "Tkey1");
//System.out.println(value);
//获取指定多个
//Collection
//variableNames.add("key1");
//variableNames.add("key2");
//Map
//System.out.println(map);
//获取全部
Map
System.out.println(map);
}
Ø 方式二:使用TaskService的get方法获取流程变量
public void run2(){
String taskId = "1004";
//engine.getTaskService().getVariable(taskId, variableName);
Map
System.out.println(map);
}
Ø 方式三:在bpmn文件中使用表达式获取
Ø 查询组任务
public void run1() {
TaskQuery query = engine.getTaskService().createTaskQuery();
String candidateUser = "张三";
String candidateUser2 = "李四";
//过滤候选人组任务
query.taskCandidateUser(candidateUser2 );
List
for (Task task : list) {
System.out.println(task.getId()+" "+task.getName());
}
}
Ø 拾取组任务(将组任务变为个人任务)
public void run2() {
String taskId = "1802";
String userId = "1805";
//String userId = "1803";
engine.getTaskService().claim(taskId, userId);
}
Ø 退回组任务(将个人任务变为组任务)
public void run3() {
String taskId = "1802";
String userId = null;
//engine.getTaskService().claim(taskId, userId);
engine.getTaskService().setAssignee(taskId, userId);
}
Ø 指定任务负责人
public void run4() {
String taskId = "1802";
String userId = "1805";
//String userId = "1803";
engine.getTaskService().setAssignee(taskId, userId);
}
Ø 创建组
public void run1(){
//创建组对象
Group group = new GroupEntity();
group.setId("bx");
group.setName("管理部");
//将组保存在数据库中 act_id_group
engine.getIdentityService().saveGroup(group);
}
Ø 创建用户
public void run2(){
User user1 = new UserEntity();
user1.setId("01");
user1.setFirstName("张三");
User user2 = new UserEntity();
user2.setId("02");
user2.setFirstName("李四");
engine.getIdentityService().saveUser(user1);
engine.getIdentityService().saveUser(user2);
}
Ø 将用户加入到组中
public void run3(){
String userId = "01";
String groupId = "bx";
engine.getIdentityService().createMembership(userId, groupId);
}
监听流程实例的开始和结束事件
Ø 根据框架的要求编写一个监听器类,实现ExecutionListener接口
public class MyExecutionBeginListener implements ExecutionListener {
@Override
/**
* 参数是流程执行对象的代理对象
*/
public void notify(DelegateExecution execution) throws Exception {
//添加流程变量
Map
variables.put("id", "007");
variables.put("name", "周星驰");
execution.setVariables(variables);
//获取流程执行对象的属性
execution.getId();
execution.getEventName();
//获取流程变量
Map
System.out.println("begin...开始监听器执行了");
}
}
Ø 注册自定义的监听器
Ø 点空白处 ,两个类型 begin和end
监听任务的创建和办理事件
Ø 根据框架的要求编写一个监听器类,实现TaskListener接口
public class MyTaskOneListener implements TaskListener {
@Override
public void notify(DelegateTask task) {
String id = task.getId();
String name = task.getName();
task.setAssignee("周杰伦");
//设置流程变量
Map
variables.put("id", "007");
variables.put("name", "周润发");
task.setVariables(variables );
System.out.println("任务一监听器执行了。。。");
}
}
Ø 提供spring配置文件,配置一个spring框架提供的流程引擎配置对象
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource">property>
bean>
<bean id="processEngineConfiguration"
class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource">property>
<property name="transactionManager" ref="transactionManager">property>
<property name="databaseSchemaUpdate" value="true">property>
bean>
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration">property>
bean>
Ø 读取spring配置文件,创建一个spring工厂,从工厂中获取一个流程引擎对象
public class Demo {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("act_spr.xml");
ProcessEngine processEngine = (ProcessEngine) app.getBean("processEngine");
DeploymentQuery query = processEngine.getRepositoryService().createDeploymentQuery();
List
for (Deployment deployment : list) {
System.out.println(deployment.getId()+":"+deployment.getName());
}
}
}