大多數項目都集成Spring網上大多數教程也是Activiti集成Spring的方法集成到項目中。java
一直也沒有找到如何拋離Spring使用Activiti。node
一開始覺得集成Activit須要大量的工做,實際上很是簡單。web
只須要把Activiti的Jar包和Mybatis的Jar包導入到項目中。sql
設置自啓動Servlet在web.xml中修改以下數據庫
<servlet> <servlet-name>initActiviti</servlet-name> <servlet-class>com.pansoft.jbsf.servlet.InitProcessEngine</servlet-class> <load-on-startup>3</load-on-startup> </servlet> <servlet-mapping> <servlet-name>initActiviti</servlet-name> <url-pattern>/initActiviti</url-pattern> </servlet-mapping>
InitProcessEngine代碼以下apache
package com.****.jbsf.servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngineConfiguration; import org.activiti.engine.ProcessEngines; import org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration; import com.jf.plugin.activerecord.DbKit; import com.****.jbsf.plugin.ActivitiPlugin; import com.****.jbsf.util.activiti.ActivitiTransactionFactory; public class InitProcessEngine extends HttpServlet { private static final long serialVersionUID = 715456159702221404L; public void init(ServletConfig config) throws ServletException { StandaloneProcessEngineConfiguration conf = (StandaloneProcessEngineConfiguration) ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration(); // conf.setDatabaseSchema("root"); conf.setDataSource(DbKit.getDataSource()).setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE).setDbHistoryUsed(true); conf.setTransactionFactory(new ActivitiTransactionFactory()); ActivitiPlugin.processEngine = conf.buildProcessEngine(); //自啓動完成 //部署流程定義 ProcessEngine pe = ProcessEngines.getDefaultProcessEngine(); pe.getRepositoryService() .createDeployment() .name("督察催辦") .addClasspathResource("/com/pansoft/jbsf/oa/bpmn/Urge.bpmn") .addClasspathResource("/com/pansoft/jbsf/oa/bpmn/Urge.png") .deploy(); } }
啓動以上代碼以後,數據庫表將自動創建json
DbKit.getDataSource()爲項目中獲取DataSource對象canvas
其中new ActivitiTransactionFactory()的ActivitiTransactionFactory事務管理代碼爲session
package com.****.jbsf.util.activiti; import java.sql.Connection; import java.util.Properties; import javax.sql.DataSource; import org.apache.ibatis.session.TransactionIsolationLevel; import org.apache.ibatis.transaction.Transaction; import org.apache.ibatis.transaction.TransactionFactory; public class ActivitiTransactionFactory implements TransactionFactory { @Override public void setProperties(Properties props) { } @Override public Transaction newTransaction(Connection conn) { return new ActivitiTransaction(conn); } @Override public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) { return new ActivitiTransaction(ds, level, autoCommit); } }
其中ActivitiTransaction對象代碼爲app
package com.****.jbsf.util.activiti; import java.sql.Connection; import java.sql.SQLException; import javax.sql.DataSource; import org.apache.ibatis.session.TransactionIsolationLevel; import org.apache.ibatis.transaction.Transaction; import com.jf.plugin.activerecord.DbKit; public class ActivitiTransaction implements Transaction { protected Connection connection; protected DataSource dataSource; protected TransactionIsolationLevel level; protected boolean autoCommmit; public ActivitiTransaction(DataSource ds, TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit) { dataSource = ds; level = desiredLevel; autoCommmit = desiredAutoCommit; } public ActivitiTransaction(Connection connection) { this.connection = connection; } @Override public Connection getConnection() throws SQLException { if (connection == null) { openConnection(); } return connection; } @Override public void commit() throws SQLException { } @Override public void rollback() throws SQLException { } @Override public void close() throws SQLException { if(connection!=null){ DbKit.close(connection); } } protected void openConnection() throws SQLException { connection = DbKit.getConnection(); if (level != null) { connection.setTransactionIsolation(level.getLevel()); } } }
openConnection對象獲取系統內獲取Conn對象方法.
須要注意的是,Oracle不一樣用戶對應不一樣數據庫表
若是用戶不一樣對應不一樣的Schema.初始化的時候經過
conf.setDatabaseSchema("username");
切換schema.
使用Jfinal的話,能夠不實用servlet的方式加載.
將activiti做爲一個插件的形式集成進去.
寫一個activitiPlugin.整體代碼與servlet的類似.
代碼比較多,貼出來的代碼中還有,"流程轉換爲模型"和"建立新模型"還有"加載流程定義"的代碼.
具體代碼以下:
package com.****.jbsf.plugin; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamReader; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import com.jf.plugin.IPlugin; import com.jf.plugin.activerecord.DbKit; import org.activiti.bpmn.converter.BpmnXMLConverter; import org.activiti.bpmn.model.BpmnModel; import org.activiti.editor.constants.ModelDataJsonConstants; import org.activiti.editor.language.json.converter.BpmnJsonConverter; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngineConfiguration; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration; import org.activiti.engine.repository.Model; import org.activiti.engine.repository.ProcessDefinition; import org.apache.commons.lang3.StringUtils; import com.****.util.activiti.ActivitiTransactionFactory; public class ActivitiPlugin implements IPlugin{ private static ProcessEngine processEngine = null; private static ProcessEngineConfiguration processEngineConfiguration = null; private boolean isStarted = false; @Override public boolean start(){ try { createProcessEngine(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; } @Override public boolean stop() { ProcessEngines.destroy(); isStarted = false; return true; } private Boolean createProcessEngine() throws Exception{ if (isStarted) { return true; } StandaloneProcessEngineConfiguration conf = (StandaloneProcessEngineConfiguration) ProcessEngineConfiguration .createStandaloneProcessEngineConfiguration(); // conf.setDatabaseSchema("cwbase35_9999"); conf.setDataSource(DbKit.getDataSource()) .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE) .setDbHistoryUsed(true); // conf.setTransactionsExternallyManaged(true); // 使用託管事務工廠 conf.setTransactionFactory(new ActivitiTransactionFactory()); ActivitiPlugin.processEngine = conf.buildProcessEngine(); isStarted = true; //開啓流程引擎 System.out.println("啓動流程引擎......."); /** * 部署流程定義 * 之後能夠拿出去 * */ // ProcessEngine pe = ProcessEngines.getDefaultProcessEngine(); // pe.getRepositoryService() // .createDeployment() // .name("督察催辦") // .addClasspathResource("/com/****/jbsf/oa/bpmn/Urge.bpmn") // .addClasspathResource("/com/****/jbsf/oa/bpmn/Urge.png") // .deploy(); // convertToModel(ActivitiPlugin.processEngine,"Urge:4:17504"); // createModel(ActivitiPlugin.processEngine); return isStarted; } // 開啓流程服務引擎 public static ProcessEngine buildProcessEngine() { if (processEngine == null) if (processEngineConfiguration != null) { processEngine = processEngineConfiguration.buildProcessEngine(); } return processEngine; } /** * 建立新模型 * @throws UnsupportedEncodingException * */ public void createModel(ProcessEngine pe) throws UnsupportedEncodingException{ RepositoryService repositoryService = pe.getRepositoryService(); ObjectMapper objectMapper = new ObjectMapper(); ObjectNode editorNode = objectMapper.createObjectNode(); editorNode.put("id", "canvas"); editorNode.put("resourceId", "canvas"); ObjectNode stencilSetNode = objectMapper.createObjectNode(); stencilSetNode.put("namespace", "http://b3mn.org/stencilset/bpmn2.0#"); editorNode.put("stencilset", stencilSetNode); Model modelData = repositoryService.newModel(); ObjectNode modelObjectNode = objectMapper.createObjectNode(); modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, "模型名稱"); modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1); String description = StringUtils.defaultString("模型描述信息"); modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description); modelData.setMetaInfo(modelObjectNode.toString()); modelData.setName("模型名稱"); modelData.setKey(StringUtils.defaultString("Urge")); repositoryService.saveModel(modelData); repositoryService.addModelEditorSource(modelData.getId(), editorNode.toString().getBytes("utf-8")); } /** * 流程定義轉模型 * */ public void convertToModel(ProcessEngine pe,String processDefinitionId) throws Exception{ RepositoryService repositoryService = pe.getRepositoryService(); ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery() .processDefinitionId(processDefinitionId).singleResult(); InputStream bpmnStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getResourceName()); XMLInputFactory xif = XMLInputFactory.newInstance(); InputStreamReader in = new InputStreamReader(bpmnStream, "UTF-8"); XMLStreamReader xtr = xif.createXMLStreamReader(in); BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr); BpmnJsonConverter converter = new BpmnJsonConverter(); com.fasterxml.jackson.databind.node.ObjectNode modelNode = converter.convertToJson(bpmnModel); Model modelData = repositoryService.newModel(); modelData.setKey(processDefinition.getKey()); modelData.setName(processDefinition.getResourceName()); modelData.setCategory(processDefinition.getDeploymentId()); ObjectNode modelObjectNode = new ObjectMapper().createObjectNode(); modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, processDefinition.getName()); modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1); modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, processDefinition.getDescription()); modelData.setMetaInfo(modelObjectNode.toString()); repositoryService.saveModel(modelData); repositoryService.addModelEditorSource(modelData.getId(), modelNode.toString().getBytes("utf-8")); } }
而後在jfinal插件中註冊即可
代碼以下
@Override public void configPlugin(Plugins me) { **************省略代碼********** ActivitiPlugin ap = new ActivitiPlugin(); me.add(ap); _MappingKit.mapping(arp);//註冊全部model-bean **************省略代碼********** }