最近项目开发中发现,利用注解的方式替代原有的xml配置文件搭建SpringMVC,较流程和步骤来说,着实方便许多,整理笔记如下:
目录
一,整合solr
二,整合Activity
1,在IDEAL中创建一个名为personal-system的项目,依赖文件:
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.20.RELEASE
com.spring.person
person-desc
0.0.1-SNAPSHOT
person-desc
Demo project for Spring Boot
1.8
2.1.1.RELEASE
org.springframework.data
spring-data-solr
${spring.data.solr.version}
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
commons-lang
commons-lang
2.6
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.3
mysql
mysql-connector-java
5.1.44
com.alibaba
druid
1.1.0
org.apache.shiro
shiro-core
1.2.2
org.apache.shiro
shiro-spring
1.2.2
org.apache.shiro
shiro-ehcache
1.2.2
org.springframework.boot
spring-boot-starter-thymeleaf
org.aspectj
aspectjweaver
net.sourceforge.nekohtml
nekohtml
1.9.22
org.springframework.boot
spring-boot-devtools
true
redis.clients
jedis
2.9.0
com.dyuproject.protostuff
protostuff-core
1.0.8
com.dyuproject.protostuff
protostuff-runtime
1.0.8
org.nutz
nutz
1.b.51
net.sf.json-lib
json-lib
2.3
jdk15
org.springframework.data
spring-data-solr
org.apache.solr
solr-solrj
6.6.2
org.springframework.boot
spring-boot-starter-amqp
org.activiti
activiti-spring-boot-starter-basic
5.22.0
org.activiti
activiti-spring-boot-starter-jpa
5.22.0
io.springfox
springfox-swagger2
2.6.1
io.springfox
springfox-swagger-ui
2.6.1
org.springframework.boot
spring-boot-maven-plugin
true
2.下载Tomcat和solr,配置solr服务器(网上有配置步骤,百度即可,需要提醒的是:下载相匹配的版本)
其中用到的数据库连接包和中文分词包,需要的小伙伴可以评论留言邮箱地址,配置成功如下图:
3,在项目中注入SolrClient,即可进行相应的操作
1,idea中安装插件,actiBPM用来绘制流程图,和eclipse不同的是,IDEA以.xml文件生成图片,如果不能生成,需要启用JBossjBPM插件
2,绘制流程以后,重启项目,Activity会自动在数据库中生成25张表
3,创建任务,启动任务,执行任务
package com.spring.activity;
import org.activiti.engine.EngineServices;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.task.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.support.HttpRequestHandlerServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@RequestMapping(value = "createActiviti")
@Controller
public class InitActivityData {
private static final Logger logger = LoggerFactory.getLogger(InitActivityData.class);
@Autowired
private EngineServices processEngine;
@Autowired
private RepositoryService repositoryService;
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
//部署流程
@RequestMapping("/buildFlow")
@ResponseBody
public void deployProcess(){
logger.info("部署流程");
RepositoryService repositoryService = processEngine.getRepositoryService();
DeploymentBuilder builder = repositoryService.createDeployment();
builder.addClasspathResource("askforleave.bpmn");
builder.deploy();
}
@RequestMapping("/startAskForLeave")
@ResponseBody
public String firstDemo() {
logger.info("启动请假流程");
RuntimeService runtimeService = processEngine.getRuntimeService();
runtimeService.startProcessInstanceByKey("askforleave");
return "请假流程创建成功";
}
@RequestMapping("/queryFlow")
@ResponseBody
public void queryTask(){
logger.info("查询流程");
TaskService taskService = processEngine.getTaskService();
//根据assignee(节点接受人)查询任务
String assignee = "student";
List tasks = taskService.createTaskQuery().taskAssignee(assignee).list();
int size = tasks.size();
for (int i = 0; i < size; i++) {
Task task = tasks.get(i);
}
//首次运行的时候这个没有输出,因为第一次运行的时候扫描act_ru_task的表里面是空的,但第一次运行完成之后里面会添加一条记录,之后每次运行里面都会添加一条记录
for (Task task : tasks) {
System.out.println("taskId=" +"流程任务节点信息ID:"+ task.getId() +
",taskName:" +"流程任务节点名称ID:" +task.getName() +
",assignee:" + "流程任务节点接受人:"+task.getAssignee() +
",createTime:" +"流程任务节点创建时间:"+ task.getCreateTime());
}
}
@RequestMapping("/examineFlow")
@ResponseBody
public void examineFlow(HttpServletRequest request){
logger.info("处理流程");
String taskId = request.getParameter("taskId");
TaskService taskService = processEngine.getTaskService();
taskService.complete(taskId);
}
}
4,一般企业为了追踪流程信息,不仅要列表展示历史信息,以图说明显得更形象,笔者借鉴他人的思路,整理了一套方法如下
Controller:
@RequestMapping("getImages")
public void getCode(HttpServletRequest req, HttpServletResponse res){
logger.info("流程跟踪信息");
String processId = req.getParameter("processId");
InputStream in = null;
byte[] b=new byte[1024];
try {
ServletOutputStream sos = res.getOutputStream();
in = userTaskService.imgs(processId);
//文件名
String src3="askforleave.png";
res.setHeader("Content-Disposition","attachment;filename="+URLEncoder
.encode(src3,"UTF-8"));
int i=in.read(b, 0, b.length);
while(i!=-1){
sos.write(b, 0, i);
i=in.read(b,0,b.length);
}
in.close();
sos.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Service:
@Override
public InputStream imgs(String processId) {
String taskId = processId;//这里的任务id应该从前台传过来,可以直接在数据库里查一个放在这里先试试
//processInstanceId
String processInstanceId = taskService.createTaskQuery().taskId(taskId).singleResult().getProcessInstanceId();
//获取历史流程实例
HistoricProcessInstance processInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
//获取流程图
BpmnModel bpmnModel = repositoryService.getBpmnModel(processInstance.getProcessDefinitionId());
ProcessEngineConfiguration processEngineConfiguration = processEngine.getProcessEngineConfiguration();
Context.setProcessEngineConfiguration((ProcessEngineConfigurationImpl) processEngineConfiguration);
ProcessDiagramGenerator diagramGenerator = processEngineConfiguration.getProcessDiagramGenerator();
ProcessDefinitionEntity definitionEntity = (ProcessDefinitionEntity)repositoryService.getProcessDefinition(processInstance.getProcessDefinitionId());
List highLightedActivitList = historyService.createHistoricActivityInstanceQuery().processInstanceId(processInstanceId).list();
//高亮环节id集合
List highLightedActivitis = new ArrayList();
//高亮线路id集合
List highLightedFlows = getHighLightedFlows(definitionEntity,highLightedActivitList);
for(HistoricActivityInstance tempActivity : highLightedActivitList){
String activityId = tempActivity.getActivityId();
highLightedActivitis.add(activityId);
}
//中文显示的是口口口,设置字体就好了
InputStream imageStream = diagramGenerator.generateDiagram(bpmnModel, "png", highLightedActivitis,highLightedFlows,"宋体","宋体","宋体",null,1.0);
//单独返回流程图,不高亮显示
return imageStream;
}
/**
* 获取需要高亮的线
* @param processDefinitionEntity
* @param historicActivityInstances
* @return
*/
private List getHighLightedFlows(
ProcessDefinitionEntity processDefinitionEntity,
List historicActivityInstances) {
List highFlows = new ArrayList();// 用以保存高亮的线flowId
for (int i = 0; i < historicActivityInstances.size() - 1; i++) {// 对历史流程节点进行遍历
ActivityImpl activityImpl = processDefinitionEntity
.findActivity(historicActivityInstances.get(i)
.getActivityId());// 得到节点定义的详细信息
List sameStartTimeNodes = new ArrayList();// 用以保存后需开始时间相同的节点
ActivityImpl sameActivityImpl1 = processDefinitionEntity
.findActivity(historicActivityInstances.get(i + 1)
.getActivityId());
// 将后面第一个节点放在时间相同节点的集合里
sameStartTimeNodes.add(sameActivityImpl1);
for (int j = i + 1; j < historicActivityInstances.size() - 1; j++) {
HistoricActivityInstance activityImpl1 = historicActivityInstances
.get(j);// 后续第一个节点
HistoricActivityInstance activityImpl2 = historicActivityInstances
.get(j + 1);// 后续第二个节点
if (activityImpl1.getStartTime().equals(
activityImpl2.getStartTime())) {
// 如果第一个节点和第二个节点开始时间相同保存
ActivityImpl sameActivityImpl2 = processDefinitionEntity
.findActivity(activityImpl2.getActivityId());
sameStartTimeNodes.add(sameActivityImpl2);
} else {
// 有不相同跳出循环
break;
}
}
List pvmTransitions = activityImpl
.getOutgoingTransitions();// 取出节点的所有出去的线
for (PvmTransition pvmTransition : pvmTransitions) {
// 对所有的线进行遍历
ActivityImpl pvmActivityImpl = (ActivityImpl) pvmTransition
.getDestination();
// 如果取出的线的目标节点存在时间相同的节点里,保存该线的id,进行高亮显示
if (sameStartTimeNodes.contains(pvmActivityImpl)) {
highFlows.add(pvmTransition.getId());
}
}
}
return highFlows;
}
5,结果如下:
(由于时间仓促,下一篇介绍Shiro和RabbitMQ的集成)