SpringBoot 整合Activiti(一)

最近在学习Activiti工作流,学习过程整理下,方便以后回顾。

我这里用的Activiti版本是5.22,考虑后面应该会做在线绘制流程图,而6.0版本网上信息过少。。。(如下配置6.0也可试用)

开发工具使用Eclipse,Eclipse集成Activiti可参考:https://www.cnblogs.com/mingforyou/p/5347561.html

项目目录结构如下:

SpringBoot 整合Activiti(一)_第1张图片

1、pom.xml配置SpringBoot和Activiti5.22


	4.0.0
	workFlow
	workFlow
	0.0.1-SNAPSHOT
	
		UTF-8
		1.8
	

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.8.RELEASE
	
	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.activiti
			activiti-spring-boot-starter-basic
			5.22.0
		
		
		
			org.springframework.boot
			spring-boot-starter-data-jpa
		
		
			org.springframework.boot
			spring-boot-starter-thymeleaf
		
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			mysql
			mysql-connector-java
			runtime
		
		
			org.springframework.boot
			spring-boot-starter-jdbc
		
		
			org.springframework.boot
			spring-boot-starter-tomcat
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
			org.springframework.boot
			spring-boot-devtools
			true
		
	
	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	
 
2、数据源配置(application.properties):

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/activiti?characterEncoding=utf8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

server.context-path=/workFlow
server.session.timeout=10

spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8
//端口号
server.port=8081
3、绘制流程图如下:
SpringBoot 整合Activiti(一)_第2张图片

右键以xml编辑器方式打开流程图:

SpringBoot 整合Activiti(一)_第3张图片

文件内容:



  
    
    
    
    
    
    
    
  
  
    
      
        
      
      
        
      
      
        
      
      
        
      
      
        
        
      
      
        
        
      
      
        
        
      
    
  

新创建的流程一定记得修改流程图id如下图,如若流程图id存在重复,则启动报错:

SpringBoot 整合Activiti(一)_第4张图片

4、java代码:

SpringBoot会把常用的服务自动配置注入,使用时直接装配使用即可:

@SpringBootApplication
public class App {
	
    public static void main(String[] args) throws Exception {
        SpringApplication.run(App.class, args);
    }
}
@RestController
@RequestMapping("/demo")
public class DemoController {
	@Autowired  
	private RepositoryService repositoryService; 
	@Autowired  
	private RuntimeService runtimeService;  
	@Autowired  
	private TaskService taskService;  
	
	@RequestMapping("/firstDemo")
	public void firstDemo() {

		//根据bpmn文件部署流程
		Deployment deployment = repositoryService.createDeployment().addClasspathResource("demo2.bpmn").deploy();
		//获取流程定义
		ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
		//启动流程定义,返回流程实例
		ProcessInstance pi = runtimeService.startProcessInstanceById(processDefinition.getId());
		String processId = pi.getId();
		System.out.println("流程创建成功,当前流程实例ID:"+processId);
		
		Task task=taskService.createTaskQuery().processInstanceId(processId).singleResult();
		System.out.println("第一次执行前,任务名称:"+task.getName());
		taskService.complete(task.getId());

		task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
		System.out.println("第二次执行前,任务名称:"+task.getName());
		taskService.complete(task.getId());

		task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
		System.out.println("task为null,任务执行完毕:"+task);
	}
解决流程图汉字乱码问题:

/**
 * 解决乱码问题
 * @author Administrator
 *
 */
@Component
public class ShareniuProcessEngineConfigurationConfigurer implements ProcessEngineConfigurationConfigurer  {
	public void configure(SpringProcessEngineConfiguration processEngineConfiguration) {
		processEngineConfiguration.setActivityFontName("宋体");
		processEngineConfiguration.setLabelFontName("宋体");
		processEngineConfiguration.setAnnotationFontName("宋体");
		System.out.println("ShareniuProcessEngineConfigurationConfigurer#############");
		System.out.println(processEngineConfiguration.getActivityFontName());
	}
}

5、执行结果:

访问http://localhost:8081/workFlow/demo/firstDemo,控制台即可得到如下结果:

SpringBoot 整合Activiti(一)_第5张图片
后面会继续更新Activiti相关内容。

你可能感兴趣的:(Activiti)