我的Activiti例子

Spring 与Activiti的入门整合 [url]http://www.iteye.com/topic/1134046[/url]
Activiti 5.12用户指南之Activiti的Spring集成 [url]http://www.iteye.com/topic/1130389[/url]
Activiti 5.15 用户手册 [url]http://www.mossle.com/docs/activiti/[/url]
默认Explorer应用使用的内存数据库已经包含了示例流程,用户和群组信息。
下面是示例中可以使用的用户:
账号 密码 角色
kermit kermit admin
gonzo gonzo manager
fozzie fozzie user
--------------------------------------------------


package com;

import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.editor.constants.ModelDataJsonConstants;
import org.activiti.editor.language.json.converter.BpmnJsonConverter;
import org.activiti.engine.*;
import org.activiti.engine.form.StartFormData;
import org.activiti.engine.form.TaskFormData;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.Model;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ObjectNode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created with IntelliJ IDEA.
* User: pandy
* Date: 13-9-16
* Time: 上午9:36
* To change this template use File | Settings | File Templates.
*/
@Controller
@RequestMapping("/a")
public class TestController {
@Resource(name = "taskService")
private TaskService taskService;

@Resource(name = "runtimeService")
private RuntimeService runtimeService;

@Resource(name = "repositoryService")
private RepositoryService repositoryService;

@Resource(name = "formService")
private FormService formService;

@Resource(name = "historyService")
private HistoryService historyService;


/**
* 获得一些查询列表
*
* @return
*/
@RequestMapping("/list")
public ModelAndView list() {
ModelAndView view = new ModelAndView("list");
List<Task> tasks = taskService.createTaskQuery().list();
view.addObject("tasks", tasks);
view.addObject("message", "This is a message.");

List<Model> modelList = repositoryService.createModelQuery().list();
view.addObject("modelList", modelList);

List<Deployment> deploymentList = repositoryService.createDeploymentQuery().list();
view.addObject("deploymentList", deploymentList);

List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery().list();
view.addObject("processDefinitionList", processDefinitionList);

return view;
}

/**
* 创建一个modeler
*
* @return
*/
@RequestMapping("/create")
public ModelAndView create() {

Date d = new Date();
String str = "Pandy_" + d.getTime() + "";

try {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode editorNode = objectMapper.createObjectNode();
editorNode.put("id", "canvas");
editorNode.put("resourceId", "canvas");
ObjectNode stencilSetNode = objectMapper.createObjectNode();
stencilSetNode.put("namespace", "http://b3mn.org/stencilset/bpmn2.0#");
editorNode.put("stencilset", stencilSetNode);
Model modelData = repositoryService.newModel();

ObjectNode modelObjectNode = objectMapper.createObjectNode();
modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, str);
modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);
modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, str);
modelData.setMetaInfo(modelObjectNode.toString());
modelData.setName(str);
modelData.setKey(str);

repositoryService.saveModel(modelData);
repositoryService.addModelEditorSource(modelData.getId(), editorNode.toString().getBytes("utf-8"));


} catch (Exception e) {
e.printStackTrace();
}


String url = "redirect:/a/list.do";
return new ModelAndView(url);
}

/**
* 删除一个已经部署的实例
*
* @param id
* @return
*/
@RequestMapping("/deleteModeler")
public ModelAndView deleteModeler(@RequestParam("id") String id) {
System.out.println("id=" + id);
try {
repositoryService.deleteModel(id);
} catch (Exception e) {
e.printStackTrace();
}

String url = "redirect:/a/list.do";
return new ModelAndView(url);
}


/**
* 部署一个Modeler
*
* @param id
* @return
*/
@RequestMapping("/deployModel")
public ModelAndView deployModel(@RequestParam("id") String id) {
System.out.println("id=" + id);
Deployment deployment = null;
try {
Model modelData = repositoryService.getModel(id);
ObjectNode modelNode = (ObjectNode) new ObjectMapper().readTree(
repositoryService.getModelEditorSource(modelData.getId())
);

byte[] bpmnBytes = null;

BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);
bpmnBytes = new BpmnXMLConverter().convertToXML(model);

String processName = modelData.getName() + ".bpmn20.xml";

deployment = repositoryService.createDeployment()
.name(modelData.getName())
.addString(processName, new String(bpmnBytes))
.deploy();

} catch (Exception e) {
e.printStackTrace();
}


String url = "redirect:/a/list.do";
return new ModelAndView(url);
}


/**
* 删除一个已经部署的实例
*
* @param id
* @return
*/
@RequestMapping("/deleteDeploy")
public ModelAndView deleteDeploy(@RequestParam("id") String id) {
System.out.println("id=" + id);
try {
repositoryService.deleteDeployment(id,false);
} catch (Exception e) {
e.printStackTrace();
}

String url = "redirect:/a/list.do";
return new ModelAndView(url);
}

/**
* 启动已经部署工作流
*
* @return
*/
@RequestMapping("/start")
public ModelAndView start(@RequestParam("id") String defId) {
Map<String, Object> variables = new HashMap<String, Object>();
/* variables.put("employeeName", "Kermit");
variables.put("numberOfDays", new Integer(4));
variables.put("vacationMotivation", "I'm really tired!");*/
//启动
System.out.println("启动");

//ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess", variables);
ProcessInstance processInstance = runtimeService.startProcessInstanceById(defId, variables);
//StartFormData formData = formService.getStartFormData(processInstance.getProcessDefinitionId());

String url = "redirect:/a/list.do";
return new ModelAndView(url);
}

/**
* 完成一个节点
*
* @param id
* @return
*/
@RequestMapping("/complete")
public ModelAndView complete(@RequestParam("id") String id) {
//List<Task> list = taskService.createTaskQuery().taskDefinitionKey(id).list();
List<Task> list = taskService.createTaskQuery().taskId(id).list();
if (list != null && !list.isEmpty()) {
for (Task task : list) {


TaskFormData formData = formService.getTaskFormData(task.getId());
taskService.complete(task.getId());
}
}

String url = "redirect:/a/list.do";
return new ModelAndView(url);
}

/**
* 导出流程
*
* @param fileName
* @param bpmnBytes
*/
public void exportFile(String fileName, byte[] bpmnBytes) {
fileName = "/mnt/D/work_documents/workspace/ActivitiTemp/src/main/resources/diagrams/" + fileName;
System.out.println(fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(fileName, true);
fos.write(bpmnBytes);
fos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException iex) {
}
}


}


/**
* 启动已经部署工作流
*
* @return
*/
@RequestMapping("/start1")
public ModelAndView start1() {
Map<String, Object> variables = new HashMap<String, Object>();
/* variables.put("employeeName", "Kermit");
variables.put("numberOfDays", new Integer(4));
variables.put("vacationMotivation", "I'm really tired!");*/
//启动
System.out.println("启动");
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess", variables);
StartFormData formData = formService.getStartFormData(processInstance.getProcessDefinitionId());

String url = "redirect:/a/list.do";
return new ModelAndView(url);
}

/**
* 启动已经部署工作流
*
* @return
*/
@RequestMapping("/start2")
public ModelAndView start2() {
Map<String, Object> variables = new HashMap<String, Object>();
/* variables.put("employeeName", "Kermit");
variables.put("numberOfDays", new Integer(4));
variables.put("vacationMotivation", "I'm really tired!");*/
//启动
System.out.println("启动");
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess", variables);

String url = "redirect:/a/list.do";
return new ModelAndView(url);
}

/**
* 启动已经部署Form工作流
*
* @return
*/
@RequestMapping("/start3")
public ModelAndView start3() {
Map<String, Object> variables = new HashMap<String, Object>();
/* variables.put("employeeName", "Kermit");
variables.put("numberOfDays", new Integer(4));
variables.put("vacationMotivation", "I'm really tired!");*/
//启动
System.out.println("启动");
Map<String, String> properties = new HashMap<String, String>();
ProcessInstance processInstance = formService.submitStartFormData("xxxxx", properties);

String url = "redirect:/a/list.do";
return new ModelAndView(url);
}

/**
* 这只是一个测试的方法
*
* @return
*/
@RequestMapping("/mytest")
public ModelAndView mytest() {
System.out.println("开始执行工作流的方法");

//发布
System.out.println("发布");
repositoryService.createDeployment()
.addClasspathResource("bpmn/MyProcess.bpmn")
.deploy();

Map<String, Object> variables = new HashMap<String, Object>();
/* variables.put("employeeName", "Kermit");
variables.put("numberOfDays", new Integer(4));
variables.put("vacationMotivation", "I'm really tired!");*/


//启动
System.out.println("启动");
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess", variables);

//遍历任务
System.out.println("遍历所有");
//List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("management").list();
//List<Task> tasks = taskService.createTaskQuery().taskCandidateUser("test").list();
List<Task> tasks = taskService.createTaskQuery().taskAssignee("test").list();
for (Task task : tasks) {
System.out.println("#Task available: " + task.getId() + ":" + task.getName());


Map<String, Object> taskVariables = new HashMap<String, Object>();
//taskService.complete(task.getId(), taskVariables);
System.out.println("complete#Task: " + task.getId() + ":" + task.getName());
}

//查看任务
System.out.println("遍历所有");
tasks = taskService.createTaskQuery().list();
for (Task task : tasks) {
System.out.println("@Task available: " + task.getId() + ":" + task.getName());
}

String url = "redirect:/a/list.do";
return new ModelAndView(url);
}
}



<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%
String path=request.getContextPath();
%>
<html>
<head>
<title></title>
<style>
table,tr,th,td{
border: 1px solid #000000;
}
</style>
</head>
<body>
<br>模型列表: 创建模型: <a href="<%=path%>/a/create.do">Create</a>
<table border="0">
<tr>
<th>ID</th>
<th>Key</th>
<th>Name</th>
<th>编辑</th>
<th>部署</th>
<th>删除</th>
</tr>
<c:forEach items="${modelList}" var="t">
<tr>
<td>${t.id}</td>
<td>${t.key}</td>
<td>${t.name}</td>
<td><a target="_blank" href="<%=path%>/modeler/service/editor?id=${t.id}">编辑</a></td>
<td><a target="_self" href="<%=path%>/a/deployModel.do?id=${t.id}">部署</a></td>
<td><a target="_self" href="<%=path%>/a/deleteModeler.do?id=${t.id}">删除</a></td>
</tr>
</c:forEach>
</table>

<br>已经部署列表: 启动任务: <a href="<%=path%>/a/start1.do">Start1</a> <a href="<%=path%>/a/start2.do">Start2</a>
<table border="0">
<tr>
<th>定义ID</th>
<th>部署ID</th>
<th>Key</th>
<th>Name</th>
<th>启动</th>
<th>删除</th>
</tr>
<c:forEach items="${processDefinitionList}" var="d">
<tr>
<td>${d.id}</td>
<td>${d.deploymentId}</td>
<td>${d.key}</td>
<td>${d.name}</td>
<td><a href="<%=path%>/a/start.do?id=${d.id}">启动</a></td>
<td><a href="<%=path%>/a/deleteDeploy.do?id=${d.deploymentId}">删除</a></td>
</tr>
</c:forEach>
</table>


<br>任务列表:
<table border="0">
<tr>
<th>ID</th>
<th>Name</th>
<th>完成</th>
</tr>
<c:forEach items="${tasks}" var="t">
<tr>
<td>${t.id}</td>
<td>${t.name}</td>
<td><a href="<%=path%>/a/complete.do?id=${t.id}">批准</a></td>
</tr>
</c:forEach>
</table>

</body>
</html>



<?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"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd">
<context:component-scan base-package="com">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.0.196:3306/activiti_demo"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
<!--<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://192.168.0.196:3306/activiti_demo" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>-->

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource"/>
<property name="deploymentResources" value="classpath*:/bpmn/*.bpmn"/>
<property name="transactionManager" ref="transactionManager"/>
<property name="databaseSchemaUpdate" value="true"/>
<property name="jobExecutorActivate" value="false"/>
<property name="processDefinitionCacheLimit" value="10"/>
<property name="activityFontName" value="${diagram.activityFontName}"/>
<property name="labelFontName" value="${diagram.labelFontName}"/>
</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>



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.pandy</groupId>
<artifactId>activiti_demo</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>activiti_demo</name>
<url>http://maven.apache.org</url>

<properties>
<spring.version>3.1.2.RELEASE</spring.version>
<activiti.version>5.13</activiti.version>
<spring-data-jpa.version>1.3.4.RELEASE</spring-data-jpa.version>
<commons-dbcp.version>1.4</commons-dbcp.version>
<commons-io.version>2.4</commons-io.version>
</properties>


<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<!--<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.3</version>
</dependency>-->

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>


<!-- Activiti -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>${activiti.version}</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring-data-jpa.version}</version>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- WEB begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>


<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>

<!-- JDBC -->
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<!-- dbcp connection pool -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>${commons-dbcp.version}</version>
</dependency>
<!-- bonecp -->
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp</artifactId>
<version>0.8.0-rc1</version>
</dependency>


<!-- Modeler -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-modeler</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-explorer</artifactId>
<version>${activiti.version}</version>
<exclusions>
<exclusion>
<groupId>com.vaadin</groupId>
<artifactId>vaadin</artifactId>
</exclusion>
<exclusion>
<groupId>org.vaadin.addons</groupId>
<artifactId>dcharts-widget</artifactId>
</exclusion>
<exclusion>
<artifactId>activiti-simple-workflow</artifactId>
<groupId>org.activiti</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- 查看流程详细定义 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-diagram-rest</artifactId>
<version>${activiti.version}</version>
</dependency>


</dependencies>


<repositories>
<repository>
<id>Alfresco Maven Repository</id>
<url>https://maven.alfresco.com/nexus/content/groups/public/</url>
</repository>
<repository>
<id>buzzmedia</id>
<url>http://maven.thebuzzmedia.com</url> <!-- ImageScalr -->
</repository>
<repository>
<id>activiti</id>
<name>Activiti</name>
<url>https://maven.alfresco.com/nexus/content/repositories/activiti/</url>
</repository>
<repository>
<id>Alfresco thirdparty</id>
<url>https://maven.alfresco.com/nexus/content/repositories/thirdparty/</url>
</repository>
<repository>
<id>activiti-third-party</id>
<name>Activiti third party</name>
<url>https://maven.alfresco.com/nexus/content/repositories/activiti-thirdparty/</url>
</repository>
<repository>
<id>maven-restlet</id>
<name>Public online Restlet repository</name>
<url>http://maven.restlet.org</url>
</repository>
</repositories>


<build>
<finalName>activiti_demo</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.3.v20120416</version>
<configuration>
<webApp>
<contextPath>/activiti_demo</contextPath>
</webApp>
</configuration>
</plugin>
</plugins>
</build>
</project>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值