1. 程式人生 > >Activiti環境配置、專案搭建、與Spring整合、簡單示例

Activiti環境配置、專案搭建、與Spring整合、簡單示例

 概念

示例專案下載

環境搭建:

    FBI WARNING 兩個視訊,螢幕錄影專家錄製,非病毒,本站願意承擔責任,下載完檔案請先核對MD5。

    1.Eclipse配置Tomcat和部署WEB應用.exe(MD5:81c1dfb994ff5b2094ce0dac244da598)

    2.Activiti專案搭建與Spring整合配置.exe(MD5:3debeec0a00dc061c782ca0cdcc97f7d)

專案搭建:

    Maven專案POM

    <!-- Jar版本管理 -->

    <properties>

        <springframework>4.0.2.RELEASE</springframework>

        <hibernate>4.3.4.Final</hibernate>

        <mysql.connector>5.1.29</mysql.connector>

        <c3p0>0.9.1.2</c3p0>

        <log4j>1.2.17</log4j>

        <activiti>5.15.1</activiti>

        <jstl>1.2</jstl>

    </properties>

 

 

    <dependencies>

        <!-- Spring web mvc -->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-webmvc</artifactId>

            <version>${springframework}</version>

        </dependency>

        <!-- Spring framework 4中的orm包已經單獨拿出來了,所以要和hibernate整合,需要此包 -->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-orm</artifactId>

            <version>${springframework}</version>

        </dependency>

        <!-- 提供JMS,Freemarker,Quartz整合服務 -->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-context-support</artifactId>

            <version>${springframework}</version>

        </dependency>

        <!-- Hibernate -->

        <dependency>

            <groupId>org.hibernate</groupId>

            <artifactId>hibernate-core</artifactId>

            <version>${hibernate}</version>

        </dependency>

        <!-- MySQL驅動 -->

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>${mysql.connector}</version>

        </dependency>

        <!-- 連線池 -->

        <dependency>

            <groupId>c3p0</groupId>

            <artifactId>c3p0</artifactId>

            <version>${c3p0}</version>

        </dependency>

        <!-- log4j -->

        <dependency>

            <groupId>log4j</groupId>

            <artifactId>log4j</artifactId>

            <version>${log4j}</version>

        </dependency>

        <!-- jstl -->

        <dependency>

            <groupId>jstl</groupId>

            <artifactId>jstl</artifactId>

            <version>${jstl}</version>

        </dependency>

        <!-- activiti依賴 -->

        <dependency>

            <groupId>org.activiti</groupId>

            <artifactId>activiti-engine</artifactId>

            <version>${activiti}</version>

        </dependency>

        <!-- activiti 與 Spring 整合 -->

        <dependency>

            <groupId>org.activiti</groupId>

            <artifactId>activiti-spring</artifactId>

            <version>${activiti}</version>

        </dependency>

 

        <!-- junit -->

        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

            <version>3.8.1</version>

            <scope>test</scope>

        </dependency>

    </dependencies>


與Spring整合:

    activiti.cfg.xml   

<beans xmlns="http://www.springframework.org/schema/beans" 

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- Activiti處理引擎的配置,交由Spring管理 -->

  <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">

          <!-- 配置資料來源,和系統使用同一個資料來源 -->

          <property name="dataSource" ref="dataSource" />

          <property name="databaseSchemaUpdate" value="true" />

          <property name="jobExecutorActivate" value="false" />

          <!-- 統一的事務管理 -->

          <property name="transactionManager" ref="transactionManager" />

          <property name="activityFontName"  value="宋體" />

          <property name="labelFontName"  value="宋體" />

  </bean>

  <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">

      <property name="processEngineConfiguration" ref="processEngineConfiguration" />

  </bean>

 

</beans>

activiti的API中,最終要的一個介面就是ProcessEngine,獲取到ProcessEngine的例項,就獲取了activiti體系中的所有api。重要的介面如下:

ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

 

RuntimeService runtimeService = processEngine.getRuntimeService();

RepositoryService repositoryService = processEngine.getRepositoryService();

TaskService taskService = processEngine.getTaskService();

ManagementService managementService = processEngine.getManagementService();

IdentityService identityService = processEngine.getIdentityService();

HistoryService historyService = processEngine.getHistoryService();

FormService formService = processEngine.getFormService();

    Activiti的服務框架如下

    

    ActivitiController程式碼

package org.xdemo.example.activiti.controller;

 

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.io.InputStream;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import javax.annotation.Resource;

import javax.imageio.ImageIO;

import javax.servlet.http.HttpServletResponse;

 

import org.activiti.engine.ProcessEngine;

import org.activiti.engine.RepositoryService;

import org.activiti.engine.RuntimeService;

import org.activiti.engine.TaskService;

import org.activiti.engine.impl.cmd.GetDeploymentProcessDiagramCmd;

import org.activiti.engine.impl.interceptor.Command;

import org.activiti.engine.repository.ProcessDefinition;

import org.activiti.engine.runtime.ProcessInstance;

import org.activiti.engine.task.Task;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.ModelAndView;

 

@Controller

@RequestMapping("/process")

public class ActivitiController {

 

    @Resource

    ProcessEngine engine;

 

    /**

     * 列出所有流程模板

     */

    @RequestMapping(method = RequestMethod.GET)

    public ModelAndView list(ModelAndView mav) {

        mav.addObject("list", Util.list());

        mav.setViewName("process/template");

        return mav;

    }

 

    /**

     * 部署流程

     */

    @RequestMapping("deploy")

    public ModelAndView deploy(String processName, ModelAndView mav) {

 

        RepositoryService service = engine.getRepositoryService();

 

        if (null != processName)

            service.createDeployment()

                    .addClasspathResource("diagrams/" + processName).deploy();

 

        List<ProcessDefinition> list = service.createProcessDefinitionQuery()

                .list();

 

        mav.addObject("list", list);

        mav.setViewName("process/deployed");

        return mav;

    }

 

    /**

     * 已部署流程列表

     */

    @RequestMapping("deployed")

    public ModelAndView deployed(ModelAndView mav) {

 

        RepositoryService service = engine.getRepositoryService();

 

        List<ProcessDefinition> list = service.createProcessDefinitionQuery()

                .list();

 

        mav.addObject("list", list);

        mav.setViewName("process/deployed");

 

        return mav;

    }

 

    /**

     * 啟動一個流程例項

     */

    @SuppressWarnings("unchecked")

    @RequestMapping("start")

    public ModelAndView start(String id, ModelAndView mav) {

 

        RuntimeService service = engine.getRuntimeService();

 

        service.startProcessInstanceById(id);

 

        List<ProcessInstance> list = service.createProcessInstanceQuery()

                .list();

 

        mav.addObject("list", list);

        mav.setViewName("process/started");

 

        return mav;

    }

 

    /**

     * 所有已啟動流程例項

     */

    @RequestMapping("started")

    public ModelAndView started(ModelAndView mav) {

 

        RuntimeService service = engine.getRuntimeService();

 

        List<ProcessInstance> list = service.createProcessInstanceQuery()

                .list();

 

        mav.addObject("list", list);

        mav.setViewName("process/started");

 

        return mav;

    }

     

    /**

     * 進入任務列表 

     */

    @RequestMapping("task")

    public ModelAndView task(ModelAndView mav){

        TaskService service=engine.getTaskService();

        List<Task> list=service.createTaskQuery().list();

        mav.addObject("list", list);

        mav.setViewName("process/task");

        return mav;

    }

     

    /**

     *完成當前節點 

     */

    @RequestMapping("complete")

    public ModelAndView complete(ModelAndView mav,String id){

         

        TaskService service=engine.getTaskService();

         

        service.complete(id);

         

        return new ModelAndView("redirect:task");

    }

 

    /**

     * 所有已啟動流程例項

     * 

     * @throws IOException

     */

    @RequestMapping("graphics")

    public void graphics(String definitionId, String instanceId,

            String taskId, ModelAndView mav, HttpServletResponse response)

            throws IOException {

         

        response.setContentType("image/png");

        Command<InputStream> cmd = null;

 

        if (definitionId != null) {

            cmd = new GetDeploymentProcessDiagramCmd(definitionId);

        }

 

        if (instanceId != null) {

            cmd = new ProcessInstanceDiagramCmd(instanceId);

        }

 

        if (taskId != null) {

            Task task = engine.getTaskService().createTaskQuery().taskId(taskId).singleResult();

            cmd = new ProcessInstanceDiagramCmd(

                    task.getProcessInstanceId());

        }

 

        if (cmd != null) {

            InputStream is = engine.getManagementService().executeCommand(cmd);

            int len = 0;

            byte[] b = new byte[1024];

            while ((len = is.read(b, 0, 1024)) != -1) {

                response.getOutputStream().write(b, 0, len);

            }

        }

    }

}

示例: