Activiti5學習之【HelloWorld】

相信看完第一節的朋友都已經把Activiti需要的環境搭建起來了吧,那麼咱們來幹一件程序員學習一門新的技術都喜歡乾的事情 HelloWorld!! 本文將會講到如果和創建activiti運行時所需要的數據庫 以及如何繪製流程圖、如何發佈一個流程、如何啓動一個流程、和如何結束一個流程。 

要想寫這個HelloWorld還是需要費點勁的,因爲這個不像別的HelloWorld一樣直接寫一些代碼就行 這個還涉及到了數據庫的操作。所以咱們第一步就是創建Acitviti運行時所需要的數據庫。

創建數據庫

創建Acitviti運行時需要的數據庫有好幾種方法,比如運行Acitviti項目然後讓其自動創建。但本人不太喜歡這種方式,我還是喜歡你給我sql語句我自己去執行 用着比較自然……那麼我們當前之急就是去找所需要的sql腳本哈,在咱們下載的activiti的jar中activiti-engine-5.8.jar 這個包中就有我們要的東西。首先加壓這個包 然後進到 org/activiti/db/create目錄下 大家可以看到各種sql 那麼大家根據自己使用的數據庫來選擇相應的腳本  本人使用的mysql 所以我選擇如圖中的四個腳本。

首先創建數據庫activiti(名稱隨便) 然後到這個數據庫中直接上述四個創建表的腳本,執行完畢後。數據庫的工作就完成了哈,簡單吧!

創建項目(J2EE)導入jar包


編寫配置文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                           http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 配置數據源 -->
  <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/activiti" />
    <property name="username" value="root" />
    <property name="password" value="1234" />
  </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="transactionManager" ref="transactionManager" />
    <property name="databaseSchemaUpdate" value="true" />
    <property name="jobExecutorActivate" value="false" />
  </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="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" />
  
  <!--配置流程中工具類-->
  <bean id="commonUtil" class="activiti.process.util.CommonUtil">
  </bean>
</beans>


繪製流程圖

創建一個流程圖(如圖)點擊next以後 filename 自己隨便輸入 這裏面我輸入 HelloWorld.activiti 然後直接Finsh.


創建完後會發現它自動把流程圖放到main.resources.diagrams 包下面了,如果不想放在這下面可以直接移動過去。本人移動到了activiti.process.def包下面,這樣比較符合編碼規範。

接下來就是打開創建的流程圖,開始繪製了。這個涉及到JBPM的東西了,本文就不詳述。



按照上面繪製完後,就該編寫相應的代碼了。既然是HelloWorld那麼咱們就在走到HelloWorld流程的時候就輸出一個HelloWorld吧。

package activiti.process;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;

public class ProcessHelloWorld implements JavaDelegate {

	@Override
	public void execute(DelegateExecution arg0) throws Exception {
		System.out.println("HelloWorld");
	}

}

然後給HelloWorld這個節點指定相應的執行相應的代碼,需要打開流程圖HelloWorld.activiti 然後點擊HelloWorld節點 在下面的點開Main config選項卡里面指定Service class 爲剛纔寫的流程代碼。這樣當流程走到這一步的時候就會執行這個類裏面的execute方法。


接下來咱們需要做的就是發佈流程以及啓動流程了。

編寫ProcessUtil封裝一些方法,方便調用。

package activiti.process;

import java.util.HashMap;

/**
 * 與流程有關的工具類
 * 
 * 
 */
public class ProcessUtil {
	/**
	 * 發佈流程的方法
	 */
	public static void deploy() {
		RepositoryService service = (RepositoryService) CommonUtil.getBean("repositoryService");
		service.createDeployment().addClasspathResource("activiti/process/def/HelloWorld.bpmn20.xml")
				.addClasspathResource("activiti/process/def/HelloWorld.png").deploy();
	}

	/**
	 * 啓動流程
	 */
	public static String start() {
		RuntimeService service = (RuntimeService) CommonUtil.getBean("runtimeService");
		ProcessInstance instance = service.startProcessInstanceByKey("HelloWorld");

		return instance.getProcessInstanceId();
	}
}
CommonUtil代碼

public class CommonUtil implements ApplicationContextAware {
	private static ApplicationContext springFactory = null;

	public static Object getBean(String name) {
		return springFactory.getBean(name);
	}

	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		springFactory = applicationContext;
	}
}



編寫相應的servlet,方便咱們的操作。

public class ProcessAction extends HttpServlet {
	private static final long serialVersionUID = 1L;


	public ProcessAction() {
		super();
	}


	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cmd = request.getParameter("cmd");
		String page = null;
		if ("deploy".equals(cmd)) {
			ProcessUtil.deploy();
			request.setAttribute("result", "流程發佈成功");
			page = "success.jsp";
		} else if ("start".equals(cmd)) {
			String id = ProcessUtil.start();
			request.setAttribute("result", "流程啓動成功,流程ID:" + id);
			page = "success.jsp";
		} 
		request.getRequestDispatcher(page).forward(request, response);
	}
}
相應的頁面index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>流程測試</title>
</head>
<body>
	<form action="process" method="post">
		<input type="radio" name="cmd" value="deploy">發佈流程</input> 
		<input type="radio" name="cmd" value="start">啓動流程</input> 
		<input type="radio" name="cmd" value="view">查看當前啓動用戶流程</input> 
		<input type="submit" value="提交">
	</form>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>成功</title>
</head>
<body>
	處理結果:<%=request.getAttribute("result") %>
	<a href="index.jsp">返回</a>
</body>
</html>

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
	classpath*:*.xml
    </param-value>
  </context-param>
  <listener>
    <listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
  </listener>
  <servlet>
    <servlet-name>ProcessAction</servlet-name>
    <servlet-class>com.hollycrm.servlet.ProcessAction</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ProcessAction</servlet-name>
    <url-pattern>/process</url-pattern>
  </servlet-mapping>
</web-app>
至此編碼工作已經全部完成, 那麼接下來就是驗證下大家的代碼了哈……

發佈應用進入index.jsp,首先點擊發布流程。然後再點擊啓動流程。現在大家看看是不是控制檯輸出了HelloWorld! 因爲ServiceTask

爲自動任務不需要人工干預,所以會自動執行。啓動流程以後直接進入到了HellorWorld執行完後流程也就結束了。

本文只是演示如何運行起來Activiti的HelloWorld,萬事開頭難 有了開頭那麼接下來學起來也就比較容易了。後面的文章我將會與大家一起學習Activiti的一些核心方法,敬請期待!





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章