網(wǎng)路冷眼@BlogJava

          熙熙攘攘一閑人 以冷靜的眼光觀察技術
          posts - 88, comments - 193, trackbacks - 0, articles - 28
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          Chapter 4. Spring integration(Spring集成)

          Table of Contents

          ProcessEngineFactoryBean
          Transactions(事務)
          Expressions(表達式)
          Automatic resource deployment(自動資源部署)
          Unit testing(單元測試)

          While you definitely can use Activiti without Spring, we've provided some very nice integration features that are explained in this chapter.

          盡管你肯定能夠不用使用Spring而使用Activiti,但是本章將解釋一些優(yōu)秀的Spring集成特性。

          ProcessEngineFactoryBean

          The ProcessEngine can be configured as a regular Spring bean. The starting point of the integration is the class org.activiti.spring.ProcessEngineFactoryBean. That bean takes a process engine configuration and creates the process engine. This means that the way and all configuration properties documented in the configuration section are exactly the same as for Spring:

          ProcessEngine能夠配置為一個普通的Spring bean。集成的開始點是類org.activiti.spring.ProcessEngineFactoryBean。那個bean提取一個流程并建立流程引擎。這意味著方法和所有在配置部分的歸檔的配置屬性,完全和Spring相同:

          <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
          ...
          </bean>
          <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
          <property name="processEngineConfiguration" ref="processEngineConfiguration" />
          </bean>
          

          Do note that the processEngineConfiguration bean now uses the org.activiti.spring.SpringProcessEngineConfiguration class.

          一定要注意:processEngineConfiguration bean現(xiàn)在使用org.activiti.spring.SpringProcessEngineConfiguration 類。

          Transactions(事務)

          We'll explain the SpringTransactionIntegrationTest found in the spring examples of the distribution step by step. Here is the spring configuration file that we use in this example (located in SpringTransactionIntegrationTest-context.xml). The quoted section contains the dataSource, transactionManager, processEngine and the Activiti Engine services.

          我們將一步一步地解釋在發(fā)行包中的Spring示例的SpringTransactionIntegrationTest 。這里是這個示例里所使用的配置文件(位置在SpringTransactionIntegrationTest-context.xml)。引號部分包含了數(shù)據(jù)源,事務管理器,流程引擎和Activiti引擎服務。

          <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:context="http://www.springframework.org/schema/context"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
          http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
          <bean id="dataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
          <property name="targetDataSource">
          <bean class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
          <property name="driverClass" value="org.h2.Driver" />
          <property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
          <property name="username" value="sa" />
          <property name="password" value="" />
          </bean>
          </property>
          </bean>
          <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <property name="dataSource" ref="dataSource" />
          </bean>
          <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
          <property name="databaseType" value="h2" />
          <property name="dataSource" ref="dataSource" />
          <property name="transactionManager" ref="transactionManager" />
          <property name="databaseSchemaUpdate" value="true" />
          <property name="jobExecutorActivate" value="false" />
          </bean>
          <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
          <property name="processEngineConfiguration" ref="processEngineConfiguration" />
          </bean>
          <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
          <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
          <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
          <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
          <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
          ...

          The remainder of that spring configuration file contains the beans and configuration that we'll use in this particular example:

          Spring配置文件的其余部分包含了bean和在這個示例所使用的配置:

          <beans>
          ...
          <tx:annotation-driven transaction-manager="transactionManager"/>
          <bean id="userBean" class="org.activiti.spring.test.UserBean">
          <property name="runtimeService" ref="runtimeService" />
          </bean>
          <bean id="printer" class="org.activiti.spring.test.Printer" />
          </beans>

          First the application context is created with any of the Spring ways to do that. In this example you could use a classpath XML resource to configure our Spring application context:

          首先,用任何Spring方式建立應用程序上下文。在本例,能夠使用一個classpath XML 資源來配置我們的Spring應用程序上下文:

          ClassPathXmlApplicationContext applicationContext =
          new ClassPathXmlApplicationContext("org/activiti/examples/spring/SpringTransactionIntegrationTest-context.xml");

          or since it is a test:

          或者它是一個測試:

          @ContextConfiguration("classpath:org/activiti/spring/test/transaction/SpringTransactionIntegrationTest-context.xml")

          Then we can get the service beans and invoke methods on them. The ProcessEngineFactoryBean will have added an extra interceptor to the services that applies Propagation.REQUIRED transaction semantics on the Activiti service methods. So we can use for example the repositoryService to deploy a process like this:

          然后我們能夠得到服務bean并調(diào)用它們之上的方法。將對一個應用繁殖的,在Activiti服務方法之上必要的事務語義服務增加一個額外的攔截器。所以我們能夠像下面來部署一個流程來使用 repositoryService:

          RepositoryService repositoryService = (RepositoryService) applicationContext.getBean("repositoryService");
          String deploymentId = repositoryService
          .createDeployment()
          .addClasspathResource("org/activiti/spring/test/hello.bpmn20.xml")
          .deploy()
          .getId();
          

          The other way around also works. In this case, the Spring transaction will be around the userBean.hello() method and the Activiti service method invocation will join that same transaction.

          其它方法也可工作。在這種情況下, Spring事務將包圍在方法附近。Activiti服務方法調(diào)用將加入同一事務。

          UserBean userBean = (UserBean) applicationContext.getBean("userBean");
          userBean.hello();

          The UserBean looks like this. Remember from above in the Spring bean configuration we injected the repositoryService into the userBean.

          UserBean看起來如此。記得嗎,在上面的Spring bean配置里,已將repositoryService注入到userBean。

          public class UserBean {
          /** injected by Spring */
          private RuntimeService runtimeService;
          @Transactional
          public void hello() {
          // here you can do transactional stuff in your domain model
          // and it will be combined in the same transaction as
          // the startProcessInstanceByKey to the Activiti RuntimeService
          runtimeService.startProcessInstanceByKey("helloProcess");
          }
          public void setRuntimeService(RuntimeService runtimeService) {
          this.runtimeService = runtimeService;
          }
          }

          Expressions(表達式)

          When using the ProcessEngineFactoryBean, by default, all expressions in the BPMN processes will also 'see' the Spring beans. For example, the SpringTransactionIntegrationTest hello.bpmn20.xml shows how a method on a Spring bean can be invoked using a UEL method expression:

          當使用ProcessEngineFactoryBean時, 缺省地,在BPMN流程的所有表達式也將看見Spring beans。例如,SpringTransactionIntegrationTest hello.bpmn20.xml展示了如何能用一個UEL方法表達式調(diào)用 在Spring bean上一個方法。

          <definitions id="definitions" ...>
          <process id="helloProcess">
          <startEvent id="start" />
          <sequenceFlow id="flow1" sourceRef="start" targetRef="print" />
          <serviceTask id="print" activiti:expression="#{printer.printMessage()}" />
          <sequenceFlow id="flow2" sourceRef="print" targetRef="end" />
          <endEvent id="end" />
          </process>
          </definitions>

          Where Printer looks like this:

          這里 Printer 看起來如下:

          public class Printer {
          public void printMessage() {
          System.out.println("hello world");
          }
          }

          And the Spring bean configuration (also shown above) looks like this:

          Spring bean配置(如上所示)看起來如下:

          <beans ...>
          ...
          <bean id="printer" class="org.activiti.examples.spring.Printer" />
          </beans>

          Automatic resource deployment(自動資源部署)

          Spring integration also has a special feature for deploying resources. In the process engine configuration, you can specify a set of resources. When the process engine is created, all those resources will be scanned and deployed. There is filtering in place that prevents duplicate deployments. Only when the resources actually have changed, will new deployments be deployed to the Activiti DB. This makes sense in a lot of use case, where the Spring container is rebooted often (eg testing).

          Spring集成也為部署資源提供一個特殊的特性。在流程引擎配置里,你能指定一系列資源。當建立資源引擎時,將掃描并部署所有這些資源。存在一個防止復制部署的過濾器。只有當資源實際上已經(jīng)發(fā)生變化時,才將新的部署才部署到Activiti 數(shù)據(jù)庫里。這個需要Spring容器需要經(jīng)常重新引導(比如測試)的地方才有意義。

          Here's an example

          這里有個示例

          <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
          ...
          <property name="deploymentResources" value="classpath*:/org/activiti/spring/test/autodeployment/autodeploy.*.bpmn20.xml" />
          </bean>
          <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
          <property name="processEngineConfiguration" ref="processEngineConfiguration" />
          </bean>

          Unit testing(單元測試)

          When integrating with Spring, business processes can be tested very easily using the standard Activiti testing facilities. Following example shows how a business process is tested in a typical Spring-based unit test:

          當和Spring集成時,采用標準的測試設施Activiti testing facilities讓業(yè)務流程的測試輕而易舉。下例展示在一個典型基于Spring的單元測試里面一個業(yè)務流程是如何測試的。

          @RunWith(SpringJUnit4ClassRunner.class)
          @ContextConfiguration("classpath:org/activiti/spring/test/junit4/springTypicalUsageTest-context.xml")
          public class MyBusinessProcessTest {
          @Autowired
          private RuntimeService runtimeService;
          @Autowired
          private TaskService taskService;
          @Autowired
          @Rule
          public ActivitiRule activitiSpringRule;
          @Test
          @Deployment
          public void simpleProcessTest() {
          runtimeService.startProcessInstanceByKey("simpleProcess");
          Task task = taskService.createTaskQuery().singleResult();
          assertEquals("My Task", task.getName());
          taskService.complete(task.getId());
          assertEquals(0, runtimeService.createProcessInstanceQuery().count());
          }
          }
          

          Note that for this to work, you need to define a org.activiti.engine.test.ActivitiRule bean in the Spring configuration (which is injected by auto-wiring in the example above).

          注意:為了讓這個能夠工作,需要在Spring配置里面 定義一個org.activiti.engine.test.ActivitiRule bean(在上例里通過auto-wiring 注入)。

          <bean id="activitiRule" class="org.activiti.engine.test.ActivitiRule">
          <property name="processEngine" ref="processEngine" />
          </bean>
          

          評論

          # re: Activiti User Guide(Activiti用戶指南)-Chapter 4. Spring integration(Spring集成)[未登錄]  回復  更多評論   

          2010-12-29 15:46 by 三木
          博文寫的不錯,能把你的js鼠標特效去掉啊

          # re: Activiti User Guide(Activiti用戶指南)-Chapter 4. Spring integration(Spring集成)  回復  更多評論   

          2011-01-01 23:26 by 網(wǎng)路冷眼@BlogJava
          @三木
          謝謝光臨!去掉JS特效讓我好好考慮
          主站蜘蛛池模板: 札达县| 松阳县| 东光县| 丰顺县| 克拉玛依市| 韶关市| 宜兰县| 文水县| 星子县| 福安市| 礼泉县| 江津市| 全南县| 通河县| 华亭县| 松潘县| 伊金霍洛旗| 建瓯市| 措美县| 弋阳县| 集安市| 柳州市| 交城县| 兴业县| 贡山| 鸡东县| 郓城县| 铜川市| 山阴县| 肇源县| 玉山县| 东阿县| 黄冈市| 安仁县| 宣汉县| 中方县| 阳东县| 汪清县| 晋州市| 紫金县| 右玉县|