废话不多说直接上代码。
1、添加maven依赖包
1 <dependency> 2 <groupId>org.flowable</groupId> 3 <artifactId>flowable-engine</artifactId> 4 <version>6.4.2</version> 5 </dependency>
2、集成核心代码
1package com.nutzfw.core.plugin.flowable; 2 3import com.nutzfw.core.plugin.flowable.transaction.NutzTransactionFactory; 4import com.nutzfw.core.plugin.flowable.util.FlowStrongUuidGenerator; 5import org.flowable.common.engine.impl.history.HistoryLevel; 6import org.flowable.engine.*; 7import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration; 8import org.nutz.ioc.loader.annotation.Inject; 9import org.nutz.ioc.loader.annotation.IocBean; 10import org.nutz.lang.Encoding; 11 12import javax.sql.DataSource; 13 14/** 15 * @author 黄川 huchuc@vip.qq.com 16 * @date: 2019/4/8 17 */ 18@IocBean 19public class FlowAbleContext { 20 21 @Inject 22 DataSource dataSource; 23 24 @Inject 25 ProcessEngine processEngine; 26 27 @IocBean 28 public ProcessEngine processEngine() { 29 ProcessEngineConfiguration engineConfiguration = new StandaloneProcessEngineConfiguration(); 30 engineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE); 31 engineConfiguration.setActivityFontName("宋体"); 32 engineConfiguration.setLabelFontName("宋体"); 33 engineConfiguration.setAnnotationFontName("宋体"); 34 engineConfiguration.setXmlEncoding(Encoding.UTF8); 35 engineConfiguration.setDataSource(dataSource); 36 engineConfiguration.setHistoryLevel(HistoryLevel.FULL); 37 engineConfiguration.setIdGenerator(new FlowStrongUuidGenerator()); 38 //设置外部管理的事务 39 engineConfiguration.setTransactionsExternallyManaged(true); 40 engineConfiguration.setTransactionFactory(new NutzTransactionFactory()); 41 return engineConfiguration.buildProcessEngine(); 42 } 43 44 @IocBean 45 public RepositoryService repositoryService() { 46 return processEngine.getRepositoryService(); 47 } 48 49 @IocBean 50 public RuntimeService runtimeService() { 51 return processEngine.getRuntimeService(); 52 } 53 54 @IocBean 55 public IdentityService identityService() { 56 return processEngine.getIdentityService(); 57 } 58 59 @IocBean 60 public TaskService taskService() { 61 return processEngine.getTaskService(); 62 } 63 64 @IocBean 65 public HistoryService historyService() { 66 return processEngine.getHistoryService(); 67 } 68 69 @IocBean 70 public ManagementService managementService() { 71 return processEngine.getManagementService(); 72 } 73 74 @IocBean 75 public FormService formService() { 76 return processEngine.getFormService(); 77 } 78 79 @IocBean 80 public DynamicBpmnService dynamicBpmnService() { 81 return processEngine.getDynamicBpmnService(); 82 } 83 84}
3、我们需要在Nutz线程启用事务的时候将flowable工作流的事务管理权限交给nutz的事务,而Nutz线程没有启用事务时还是由flowable工作流自己管理
1package com.nutzfw.core.plugin.flowable.transaction; 2 3import org.apache.ibatis.session.TransactionIsolationLevel; 4import org.apache.ibatis.transaction.Transaction; 5import org.apache.ibatis.transaction.TransactionFactory; 6 7import javax.sql.DataSource; 8import java.sql.Connection; 9import java.util.Properties; 10 11/** 12 * @author 黄川 huchuc@vip.qq.com 13 * @date: 2019/4/19 14 */ 15public class NutzTransactionFactory implements TransactionFactory { 16 17 18 @Override 19 public void setProperties(Properties props) { 20 21 } 22 23 @Override 24 public Transaction newTransaction(Connection conn) { 25 return new NutzTransaction(conn); 26 } 27 28 @Override 29 public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) { 30 return new NutzTransaction(ds, level, autoCommit); 31 } 32} 33 34 35package com.nutzfw.core.plugin.flowable.transaction; 36 37import lombok.extern.slf4j.Slf4j; 38import org.apache.ibatis.session.TransactionIsolationLevel; 39import org.apache.ibatis.transaction.Transaction; 40import org.apache.ibatis.transaction.TransactionException; 41import org.nutz.trans.Trans; 42 43import javax.sql.DataSource; 44import java.sql.Connection; 45import java.sql.SQLException; 46 47/** 48 * @author 黄川 huchuc@vip.qq.com 49 * @date: 2019/4/19 50 */ 51@Slf4j 52public class NutzTransaction implements Transaction { 53 54 55 private DataSource dataSource; 56 private TransactionIsolationLevel level; 57 private Connection connection; 58 private boolean autoCommmit; 59 60 public NutzTransaction(Connection connection) { 61 this.connection = connection; 62 } 63 64 public NutzTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommmit) { 65 this.dataSource = ds; 66 this.level = level; 67 this.autoCommmit = autoCommmit; 68 } 69 70 @Override 71 public Connection getConnection() throws SQLException { 72 if (Trans.isTransactionNone()) { 73 if (this.connection == null) { 74 openConnection(); 75 } 76 } else { 77 this.connection = Trans.get().getConnection(dataSource); 78 } 79 return this.connection; 80 } 81 82 @Override 83 public void commit() throws SQLException { 84 // Does nothing 85 } 86 87 @Override 88 public void rollback() throws SQLException { 89 // Does nothing 90 } 91 92 @Override 93 public void close() throws SQLException { 94 if (this.connection != null) { 95 if (Trans.isTransactionNone()) { 96 resetAutoCommit(); 97 if (log.isDebugEnabled()) { 98 log.debug("Closing JDBC Connection [" + this.connection + "]"); 99 } 100 this.connection.close(); 101 } 102 } 103 } 104 105 protected void openConnection() throws SQLException { 106 if (log.isDebugEnabled()) { 107 log.debug("Opening JDBC Connection"); 108 } 109 this.connection = dataSource.getConnection(); 110 if (level != null) { 111 this.connection.setTransactionIsolation(level.getLevel()); 112 } 113 setDesiredAutoCommit(autoCommmit); 114 } 115 116 protected void setDesiredAutoCommit(boolean desiredAutoCommit) { 117 try { 118 if (connection.getAutoCommit() != desiredAutoCommit) { 119 if (log.isDebugEnabled()) { 120 log.debug("Setting autocommit to " + desiredAutoCommit + " on JDBC Connection [" + connection + "]"); 121 } 122 connection.setAutoCommit(desiredAutoCommit); 123 } 124 } catch (SQLException e) { 125 // Only a very poorly implemented driver would fail here, 126 // and there's not much we can do about that. 127 throw new TransactionException("Error configuring AutoCommit. " 128 + "Your driver may not support getAutoCommit() or setAutoCommit(). " 129 + "Requested setting: " + desiredAutoCommit + ". Cause: " + e, e); 130 } 131 } 132 133 protected void resetAutoCommit() { 134 try { 135 if (!connection.getAutoCommit()) { 136 // MyBatis does not call commit/rollback on a connection if just selects were performed. 137 // Some databases start transactions with select statements 138 // and they mandate a commit/rollback before closing the connection. 139 // A workaround is setting the autocommit to true before closing the connection. 140 // Sybase throws an exception here. 141 if (log.isDebugEnabled()) { 142 log.debug("Resetting autocommit to true on JDBC Connection [" + connection + "]"); 143 } 144 connection.setAutoCommit(true); 145 } 146 } catch (SQLException e) { 147 if (log.isDebugEnabled()) { 148 log.debug("Error resetting autocommit to true " 149 + "before closing the connection. Cause: " + e); 150 } 151 } 152 } 153 154 @Override 155 public Integer getTimeout() throws SQLException { 156 return null; 157 } 158}