一、先看一下spring配置文件中的配置内容:
1、首先设置数据源dataSource:
classpath:conf/jdbc.properties
1
2
3 ${driverClassName}
4
5
6 ${urlAddr}
7
8
9 ${username}
10
11
12 ${password}
13
14
15 ${maxActive}
16
17
18 ${maxIdle}
19
20
21 ${minIdle}
22
23
24 ${maxWait}
25
26
27 ${timeBetweenEvictionRunsMillis}
28
29
30 ${minEvictableIdleTimeMillis}
31
32
33 ${mysqlTestWhileIdle}
34
35
36
37
38 ${mysqlValidationQuery}
39
40
41 true
42
43
44 180
45
46
2、设置Osworkflow:
3、事务管理器:
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
class="org.springframework.transaction.support.TransactionTemplate">
4、首先看类ExtMySQLWorkflowStore,与最顶层接口继承关系是ExtMySQLWorkflowStore->MySQLWorkflowStore->JDBCWorkflowStore->WorkflowStore,除了自定义扩展的ExtMySQLWorkflowStore。其它都是osworkflow自带类或者接口,首先看ExtMySQLWorkflowStore类:
public class ExtMySQLWorkflowStore extendsMySQLWorkflowStore {//防止并发
private ThreadLocal threadlocal = new ThreadLocal();//当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变量的线程提供独立的变量副本,所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程所对应的副本
private Mapmap;//对应上面spring配置文件中workflowStore中的map,传递参数privateDataSource dataSource;//对应spring配置文件中的dataSource,设置数据源public void init() throwsStoreException {if(null!=map){
map.put("datasource",null);
}super.init(map);super.ds=dataSource;
}public Connection getConnection() throwsSQLException{
Connection con=threadlocal.get();if((null != con) && !con.isClosed()){
System.out.println("getConnection"+con.hashCode());returncon ;
}else{
con= super.ds.getConnection();
System.out.println("|-------------"+Thread.currentThread().getId());
setConnection(con);
System.out.println("super.ds.getConnection"+con.hashCode());returncon;
}
}public voidsetConnection(Connection connection) {
threadlocal.set(connection);//this.connection = connection;
}public MapgetMap() {returnmap;
}public void setMap(Mapmap) {this.map =map;
}publicDataSource getDataSource() {returndataSource;
}public voidsetDataSource(DataSource dataSource) {this.dataSource =dataSource;
}
@Overridepublic PropertySet getPropertySet(longentryId) {
HashMap args= new HashMap(1);
args.put("globalKey", "osff_" +entryId);
PropertySet ps= PropertySetManager.getInstance("memory", args);
ps.setString("col.globalKey","GLOBAL_KEY");
ps.setString("col.itemKey","ITEM_KEY");
ps.setString("col.itemType","ITEM_TYPE");
ps.setString("col.string","STRING_VALUE");
ps.setString("col.date","DATE_VALUE");
ps.setString("col.data","DATA_VALUE");
ps.setString("col.float","FLOAT_VALUE");
ps.setString("col.number","NUMBER_VALUE");returnps;
}
}
5、再看MySQLWorkflowStore:
public class MySQLWorkflowStore extendsJDBCWorkflowStore {//~ Instance fields
private String _stepSequenceIncrement = null;private String _stepSequenceRetrieve = null;//~ Methods
public void init(Map props) throwsStoreException {super.init(props);
_stepSequenceIncrement= (String) props.get("step.sequence.increment");
_stepSequenceRetrieve= (String) props.get("step.sequence.retrieve");
}protected long getNextStepSequence(Connection c) throwsSQLException {
PreparedStatement stmt= null;
ResultSet rset= null;try{
stmt=c.prepareStatement(_stepSequenceIncrement);
stmt.executeUpdate();
rset=stmt.executeQuery(_stepSequenceRetrieve);
rset.next();long id = rset.getLong(1);returnid;
}finally{
cleanup(null, stmt, rset);
}
}
}
6、再看JDBCWorkflowStore,因为代码比较长,所以一个片段一个片段讲:
public class JDBCWorkflowStore implements WorkflowStore
首先定义一些属性:
protectedDataSource ds;protectedString currentPrevTable;protectedString currentTable;protectedString entryId;protectedString entryName;protectedString entrySequence;protectedString entryState;protectedString entryTable;protectedString historyPrevTable;protectedString historyTable;protectedString stepActionId;protectedString stepCaller;protectedString stepDueDate;protectedString stepEntryId;protectedString stepFinishDate;protectedString stepId;protectedString stepOwner;protectedString stepPreviousId;protectedString stepSequence;protectedString stepStartDate;protectedString stepStatus;protectedString stepStepId;protected boolean closeConnWhenDone = false;
然后在init方法中初始化这些变量:
public void init(Map props) throwsStoreException {
entrySequence= getInitProperty(props, "entry.sequence", "SELECT nextVal('seq_os_wfentry')");
stepSequence= getInitProperty(props, "step.sequence", "SELECT nextVal('seq_os_currentsteps')");
entryTable= getInitProperty(props, "entry.table", "OS_WFENTRY");
entryId= getInitProperty(props, "entry.id", "ID");
entryName= getInitProperty(props, "entry.name", "NAME");
entryState= getInitProperty(props, "entry.state", "STATE");
historyTable= getInitProperty(props, "history.table", "OS_HISTORYSTEP");
currentTable= getInitProperty(props, "current.table", "OS_CURRENTSTEP");
currentPrevTable= getInitProperty(props, "currentPrev.table", "OS_CURRENTSTEP_PREV");
historyPrevTable= getInitProperty(props, "historyPrev.table", "OS_HISTORYSTEP_PREV");
stepId= getInitProperty(props, "step.id", "ID");
stepEntryId= getInitProperty(props, "step.entryId", "ENTRY_ID");
stepStepId= getInitProperty(props, "step.stepId", "STEP_ID");
stepActionId= getInitProperty(props, "step.actionId", "ACTION_ID");
stepOwner= getInitProperty(props, "step.owner", "OWNER");
stepCaller= getInitProperty(props, "step.caller", "CALLER");
stepStartDate= getInitProperty(props, "step.startDate", "START_DATE");
stepFinishDate= getInitProperty(props, "step.finishDate", "FINISH_DATE");
stepDueDate= getInitProperty(props, "step.dueDate", "DUE_DATE");
stepStatus= getInitProperty(props, "step.status", "STATUS");
stepPreviousId= getInitProperty(props, "step.previousId", "PREVIOUS_ID");
String jndi= (String) props.get("datasource");if (jndi != null) {try{
ds=(DataSource) lookup(jndi);if (ds == null) {
ds= (DataSource) newjavax.naming.InitialContext().lookup(jndi);
}
}catch(Exception e) {throw new StoreException("Error looking up DataSource at " +jndi, e);
}
}
}
其它主要还有一些方法就是对osworkflow表的操作。