osworkflow+spring+jdbc实现

最近研究osworkflow, 觉得这个工作流框架很轻便灵活,但是只有osworkflow+spring+hibernate的实现,我在想,如果一个大型的工作流系统,数据非常多,岂不是不能用hibernate了?,于是我就照着Hibernate的实现做了一个 Jdbc的的spring实现,反正jdbc可以解决任何数据优化问题,

package com.opensymphony.workflow.spi.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.support.JdbcUtils;

public class SpringJDBCWorkflowStore extends JDBCWorkflowStore 
implements InitializingBean {

	@Override
	public void afterPropertiesSet() throws Exception {
        entryTable = "OS_WFENTRY";
        entryId = "ID";
        entryName = "NAME";
        entryState = "STATE";
        historyTable = "OS_HISTORYSTEP";
        currentTable = "OS_CURRENTSTEP";
        currentPrevTable = "OS_CURRENTSTEP_PREV";
        historyPrevTable = "OS_HISTORYSTEP_PREV";
        stepId = "ID";
        stepEntryId = "ENTRY_ID";
        stepStepId = "STEP_ID";
        stepActionId = "ACTION_ID";
        stepOwner = "OWNER";
        stepCaller = "CALLER";
        stepStartDate = "START_DATE";
        stepFinishDate = "FINISH_DATE";
        stepDueDate = "DUE_DATE";
        stepStatus = "STATUS";
        stepPreviousId = "PREVIOUS_ID";
	}
	
    
    public void setDataSource(DataSource dataSource){
    	this.ds = dataSource;
    }
    
    public DataSource getDataSource(){
    	return this.ds;
    }
    
    @Override
    protected Connection getConnection() throws SQLException {
    	return DataSourceUtils.getConnection(this.ds);
    }
    
    @Override
    protected void cleanup(Connection connection, Statement statement,
ResultSet result) {
    	JdbcUtils.closeStatement(statement);
    	JdbcUtils.closeResultSet(result);
    	DataSourceUtils.releaseConnection(connection, this.ds);
    	
    }
    
    public void setEntrySequence(String entrySequence){
    	this.entrySequence = entrySequence;
    }
    
    public void setStepSequence(String stepSequence){
    	this.stepSequence = stepSequence;
    }

}

  

数据库的连接是基于JdbcTemplate的,好处就是可以很方便的和spring的事务框架整合(备注:如果想要用spring的事务管理,必须使用JdbcTemplate,原因是Spring的事务管理的数据库连接是从ConnHolder中取的,JdbcTemplate的数据库连接也是从ConnHolder中取的),这样使得osworkflow带入事务管理特性

 

Spring配置文件

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <description>数据源spring配置</description>
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" singleton="true" lazy-init="default" autowire="default" dependency-check="default">
    <property name="driverClassName">
      <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
      <value>jdbc:mysql://localhost:3306/test?autoReconnect=true</value>
    </property>
    <property name="username">
      <value>root</value>
    </property>
    <property name="password">
      <value>123</value>
    </property>
  </bean> 
	
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>	
    
	<bean id="workflowStore" class="com.opensymphony.workflow.spi.jdbc.SpringJDBCWorkflowStore">
		<property name="dataSource" ref="dataSource"></property>
		<property name="entrySequence">
			<value>select count(*) + 1 from os_wfentry</value>
		</property>
		<property name="stepSequence">
			<value>select sum(c1) + 1 from (select 1 as tb,  count(id)  as c1 from os_currentstep union select 2 as tb, count(id) as c1 from os_historystep) as TabelaFinal</value>
		</property>
	</bean>
		
	<bean id="workflowTypeResolver" class="com.opensymphony.workflow.util.SpringTypeResolver"/>
	
	<bean id="workflowFactory" class="com.opensymphony.workflow.spi.hibernate.SpringWorkflowFactory" init-method="init">
        <property name="resource"><value>workflows.xml</value></property>
        <property name="reload"><value>false</value></property>
	</bean>
	
	<bean id="osworkflowConfiguration" class="com.opensymphony.workflow.config.SpringConfiguration">
		<property name="store"><ref local="workflowStore"/></property>
		<property name="factory"><ref local="workflowFactory"/></property>		
	</bean>
	
	<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager"><ref local="transactionManager"/></property>
		<property name="transactionAttributes">
			<props>
				<prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
			</props>
		</property>
	</bean>

	<bean id="workflow" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="singleton">
			<value>false</value>
		</property>
		<property name="proxyInterfaces">
			<value>com.opensymphony.workflow.Workflow</value>
		</property>
		<property name="interceptorNames">
			<list>
				<value>transactionInterceptor</value>
			</list>
		</property>
		<property name="target"  ref="workflowTarget"></property>
	</bean>

	<bean id="workflowTarget" class="com.opensymphony.workflow.basic.BasicWorkflow" singleton="false">
		<property name="configuration"><ref local="osworkflowConfiguration"/></property>	
		<constructor-arg>
			<value>test2</value>
		</constructor-arg>
	</bean>
</beans>

  

 

附件里面的例子是基于Iteye里面某位同学的例子改的,只不过我整合了spring和事务,谢谢这位同学的贡献,lib没有传上来,因为有点大,需要spring的lib和osworkflow的lib,这些都可以在网上下载到

你可能感兴趣的:(spring,jdbc,OSWorkflow)