Activiti + Spring开发环境搭建

Spring + Spring MVC + Activiti + MyBatis

开发环境

  • Eclipse Luna (4.4.2)
  • JDK 1.7.0_68
  • Maven 3.3.3
  • Tomcat 7.0
  • Activiti 5.17.0
  • 其他
    • eclipse插件: activiti designer(activiti流程设计工具)

使用maven搭建web工程

(略)

编写pom.xml


    4.0.0
    org.homeway
    activitiDemo
    war
    0.0.1-SNAPSHOT
    activitiDemo Maven Webapp
    http://maven.apache.org

    
        4.1.4.RELEASE
        3.2.8
        1.2.2
        4.12
        5.17.0
        2.2
        1.4.187
    


    
        
        
            junit
            junit
            ${junit.version}
            test
        
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-context-support
            ${spring.version}
        
        
            org.springframework
            spring-aop
            ${spring.version}
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
        
            org.springframework
            spring-core
            ${spring.version}
        
        
            org.springframework
            spring-orm
            ${spring.version}
        
        
            org.springframework
            spring-tx
            ${spring.version}
        
        
            org.springframework
            spring-test
            ${spring.version}
        
        
        
            org.apache.logging.log4j
            log4j-api
            ${log4j.version}
        
        
            org.apache.logging.log4j
            log4j-core
            ${log4j.version}
        
        
        
            org.mybatis
            mybatis
            ${mybatis.version}
        
        
            org.mybatis
            mybatis-spring
            ${mybatis-spring.version}
        
        
        
            org.activiti
            activiti-engine
            ${activiti.version}
        
        
            org.activiti
            activiti-spring
            ${activiti.version}
        
        
            org.activiti
            activiti-rest
            ${activiti.version}
        
        
        
            com.h2database
            h2
            ${h2database.version}
        
    


    
        activitiDemo
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.2
                
                    1.7
                    1.7
                
            
        
    

编写web.xml


    Servlet 3.0 Web Application

    
        contextConfigLocation
        classpath*:applicationContext-spring.xml
    

    
        org.springframework.web.context.ContextLoaderListener
    

    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceEncoding
            true
        
    
    
        encodingFilter
        /*
    

    
        dispatcher
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath*:spring-mvc.xml
        
        1
    
    
        dispatcher
        *.action
    


编写spring相关的配置文件

applicationContext-spring.xml




    
        
    

    

    

    
        
        
        
        
    

    
        
    

    
        
            
            
            
        
    

    
        
        
    

    

    


applicationContext-activiti.xml




    
        
        
        
        
        
    
    
        
    
    
    
    
    
    
    
    

applicationContext-mybatis.xml




    
        
        
        
    

    
        
        
    
    

spring-mvc.xml



    
    
        
    


mybatis-config.xml




    
        
    

db.properties

db.driver=org.h2.Driver
db.url=jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000
db.username=sa
db.password=

项目结构

Activiti + Spring开发环境搭建_第1张图片
demo.webapp.png

使用activiti designer绘制流程图

Eclipse安装activiti designer
help >> install new sofeware >> add 
name: Activiti BPMN 2.0 designer , location:  http://activiti.org/designer/update/
Activiti + Spring开发环境搭建_第2张图片
demo.process.test.png
  
    
    
    
    
    
    
    
    
      
        PT5S
      
    
    
    
    
  

使用junit测试

package org.homeway.activiti.demo.test;


import java.util.List;

import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-spring.xml")
public class WorkFlowTest {
    
    Logger logger = Logger.getLogger(this.getClass());
    
    @Autowired
    ProcessEngineFactoryBean processEngine;
    
    @Autowired
    private RepositoryService repositoryService;
    
    @Autowired
    private RuntimeService runtimeService;
    
    @Autowired
    private TaskService taskService;
    
    @Autowired
    private HistoryService historyService;
    
    @Autowired
    private IdentityService identityService;
    
    @Test
    public void testEvent() throws InterruptedException {
        repositoryService.createDeployment()
            .addClasspathResource("org/homeway/activiti/demo/workflow/hello.bpmn")
            .deploy();
        System.out.println("Number of process definitions: " + repositoryService.createProcessDefinitionQuery().count());
        
        identityService.setAuthenticatedUserId("Jeff Dean");
        
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("helloProcess");
        System.out.println(processInstance.getId());
        System.out.println(processInstance.getProcessDefinitionId());
        System.out.println(processInstance.getProcessDefinitionKey());
        
        List tasks = taskService.createTaskQuery().taskAssignee("kermit").list();
        for (Task task : tasks) {
            System.out.println(task.getName() + " : " + task.getAssignee());
            
            taskService.claim(task.getId(), "kermit");
        }
        
        tasks = taskService.createTaskQuery().taskAssignee("kermit").list(); 
        for (Task task : tasks) {
            
            taskService.complete(task.getId());
            System.out.println(task.getName() + " : " + task.getId() + " completed ");
        }
        
        Thread.sleep(10 * 1000);
        
        tasks = taskService.createTaskQuery().taskAssignee("stone").list(); 
        for (Task task : tasks) {
            System.out.println(task.getName() + " : " + task.getAssignee());
            taskService.claim(task.getId(), "stone");
        }
        
        tasks = taskService.createTaskQuery().taskAssignee("stone").list();
        for (Task task : tasks) {
            taskService.complete(task.getId());
            System.out.println(task.getName() + " : " + task.getId() + " completed ");
        }
        
        tasks = taskService.createTaskQuery().taskAssignee("kermit").list(); 
        for (Task task : tasks) {
            System.out.println(task.getName() + " : " + task.getAssignee());
            taskService.claim(task.getId(), "kermit");
        }
        
        
        HistoricProcessInstance hpInstance = 
                historyService.createHistoricProcessInstanceQuery()
                .processInstanceId(processInstance.getId()).singleResult();
        System.out.println("end time: " + hpInstance.getEndTime());
        
    }
}

你可能感兴趣的:(Activiti + Spring开发环境搭建)