zoukankan      html  css  js  c++  java
  • 集成Seam,Spring和jBPM指南(译)

    这篇指南描述了一种为了使Spring和jBPM(当然还有Seam)能使用同一个Hibernate SessionFactory而将Seam,Spring和jBPM集成的方法。

    首先您要确保使用的是2.1.0版本的Seam,因为2.0.1版本的Seam和SpringTransaction会有些麻烦。

    相关的配置部分如下:

    1.在你的Spring bean配置中,像平常一样定义你的Hibernate SessionFactory并且设置如下这些属性

    <bean id="hibernateSessionFactory" class="">

            
    <!-- The hibernate properties  -->
            
    <property name="hibernateProperties">
                    
    <props>                               

                            
    <prop key="hibernate.hbm2ddl.auto">update</prop>
                            
    <!-- set to create-drop to NOT maintain state between two executions of the app -->
                            
                            
    <prop key="hibernate.transaction.flush_before_completion">
                                            true
                            
    </prop>
                            
    <prop key="hibernate.connection.release_mode">
                                            after_transaction
                            
    </prop>

                    
    </props>
            
    </property>

    <!-- this property must be set to false so we can use independent sessions -->
            
    <property name="useTransactionAwareDataSource">
                    
    <value>false</value>
            
    </property>

            
    <property name="mappingResources">
                    
    <list>

                    
    <!-- here you have to list all the *hbm.xml files for jBPM -->
                    
    <!-- see the default hibernate.cfg.xml file from jBPM -->



    </bean>
     2.为了集成Seam和Spring,我们需要两个bean
    <bean id="sessionFactory"
                    class="org.jboss.seam.ioc.spring.SeamManagedSessionFactoryBean">
                    
    <property name="sessionName" value="hibernateSession" />
    </bean>       
    <bean id="localTransactionManager"
                    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                    
    <property name="sessionFactory" ref="hibernateSessionFactory" />
    </bean>
    以上就是针对Spring的配置

    现在我们需要配置components.xml文件

    <!-- use the power of Spring transactions -->
    <spring:spring-transaction platform-transaction-manager-name="localTransactionManager"/>
              
    <persistence:managed-hibernate-session name="hibernateSession" auto-create="true"
            session-factory="#{hibernateSessionFactory}"/>         
       
    <component class="org.jboss.seam.bpm.Jbpm">
            
    <property name="processDefinitions">processdefinition.jpdl.xml</property>
    </component>
    为了在jBPM中使用hibernateSession,我从jBPM中扩展了一个子类DbPersistenceService:
    package your.namespace.jbpm.integration;

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;

    import org.jboss.seam.Component;
    import org.jboss.seam.contexts.Contexts;

    import org.jbpm.svc.Service;

    /**
     * @author Frank Bitzer
     * 
     * EL4J - The Extension Library for Java.
     *
     
    */
    public class DbPersistenceServiceFactory extends
                    org.jbpm.persistence.db.DbPersistenceServiceFactory {

            
    private static final long serialVersionUID = 997L;

            
            SessionFactory sessionFactory;
            
            
    /**
             * {@inheritDoc}
             
    */
            
    public Service openService() {
                
                    
    //create instance of own service implementation
                return new your.namespace.jbpm.integration.DbPersistenceService(this);
              }
                   
            
    /**
             * Retrieve Hibernate sessionFactory
             
    */
            @Override
            
    public synchronized SessionFactory getSessionFactory() {
               
                    
    if (sessionFactory==null) {
                    
                             
    if(Contexts.isApplicationContextActive()){
                                     
                                     
    //access seam component holding session
                                     Session session = (Session)
                                             Component.getInstance("hibernateSession");
                                    
                                     
    //and extract sessionFactory
                                    sessionFactory = session.getSessionFactory();
                                    
                             }
                            
                }
                
                
    return sessionFactory;
            }
            
            
    /**
             * Set sessionFactory
             
    */
            @Override
            
    public void setSessionFactory(SessionFactory sessionFactory) {
                
    this.sessionFactory = sessionFactory;
              }
                   
    }

    package your.namespace.jbpm.integration;

    import org.hibernate.Session;
    import org.jbpm.JbpmContext;
    import org.jbpm.persistence.db.DbPersistenceServiceFactory;
    import org.jbpm.svc.Services;
    import org.springframework.orm.hibernate3.SessionFactoryUtils;

    /**
     * @author Frank Bitzer
     * 
     * EL4J - The Extension Library for Java.
     *
     
    */
    public class DbPersistenceService extends
                    org.jbpm.persistence.db.DbPersistenceService {

            
    private static final long serialVersionUID = 996L;

            
    public DbPersistenceService(
                            DbPersistenceServiceFactory persistenceServiceFactory) {
                    
    this(persistenceServiceFactory, getCurrentServices());
            }

            
    static Services getCurrentServices() {
                    Services services = null;
                    JbpmContext currentJbpmContext = JbpmContext.getCurrentJbpmContext();
                    
    if (currentJbpmContext != null) {
                            services = currentJbpmContext.getServices();
                    }
                    
    return services;
            }

            DbPersistenceService(DbPersistenceServiceFactory persistenceServiceFactory,
                            Services services) {

                    
    super(persistenceServiceFactory);

                    
    this.persistenceServiceFactory = persistenceServiceFactory;
                    
    this.isTransactionEnabled = persistenceServiceFactory
                                    .isTransactionEnabled();
                    
    this.isCurrentSessionEnabled = persistenceServiceFactory
                                    .isCurrentSessionEnabled();
                    
    this.services = services;

            }
            
            
    /**
             * Use Hibernate sessionFactory to retrieve a Session instance.
             
    */
            
    public Session getSession() {

                    
    if ((session == null&& (getSessionFactory() != null)) {

                            session = getSessionFactory().openSession();

                            mustSessionBeClosed = true;
                            mustSessionBeFlushed = true;
                            mustConnectionBeClosed = false;

                            isTransactionEnabled = !SessionFactoryUtils.isSessionTransactional(
                                            session, getSessionFactory());

                            
    if (isTransactionEnabled) {

                                    beginTransaction();
                            }

                    }
                    
    return session;
            }

    }     
    为了结束工作,只需要像下面一样简单的在jbpm.cfg.xml中使用刚刚定义的DbPersistenceService

    <jbpm-context>
        
    <service name="persistence">
                    
    <factory>
                            
    <bean class="your.namespace.jbpm.integration.DbPersistenceServiceFactory">
                                    
    <field name="isTransactionEnabled">
                                        
    <false/>
                                    
    </field>
                            
    </bean>
                    
    </factory>
            
    </service>


    </jbpm-context>

    Also make sure your Spring WebApplicationContext is initialized before the startup of Seam. This can be achieved by placing the
    org.jboss.seam.servlet.SeamListener behind the listener for Spring in your web.xml.
  • 相关阅读:
    批量计算(batch computing)、流式计算(stream computing)、交互计算(interactive computing)、图计算(graph computing)
    ETL相关 ELT
    添加AD验证(域身份验证)到现有网站
    android开发导包升级到androidx踩坑记录【转载】
    Android Support v4v7v13和AndroidX理解【转载】
    架构师的成长之路初片~linux-基本防护措施
    架构师的成长之路初片~nginx优化篇
    架构师的成长之路初片~linux-监控脚本
    架构师的成长之路初片~Virtual-自定义容器
    架构师的成长之路初片~Virtual-容器和镜像常用的管理命令
  • 原文地址:https://www.cnblogs.com/rgbw/p/1563233.html
Copyright © 2011-2022 走看看