Activiti User Guide -- Activit 用户指南 Part03

Chapter 4. Spring integration

第四章 Spring集成

Table of Contents

ProcessEngineFactoryBean

Transactions

Expressions

Automatic resource deployment

Unit testing

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

当你不通过Spring来使用Activiti时,我们会提供非常漂亮的集成功能,在本章中我们会对它们进行解析。

ProcessEngineFactoryBean

ProcessEngineFactoryBean

The starting point of the integration is the class org.activiti.engine.impl.cfg.spring.ProcessEngineFactoryBean. That bean implementsProcessEngine and takes care of building the process engine whenever Spring instantiates it.

首先我们要集成的类是org.activiti.engine.impl.cfg.spring.ProcessEngineFactoryBean。该bean实现了ProcessEngine,并且在Spring实例化时负责构建流程引擎。

<bean id="processEngine" class="org.activiti.engine.impl.cfg.spring.ProcessEngineFactoryBean">
  ...
</bean>
  Properties that you can configure on the ProcessEngineFactoryBean:

  ProcessEngineFactoryBean可以配置的属性包含了:

Transactions

事务

 

We'll explain the SpringTransactionIntegrationTest here 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.

下面我们一步一步的解释SpringTransactionIntegrationTest 。下面列出了我们在示例中所使用的spring的配置文件(存放在SpringTransactionIntegrationTest-context.xml文件中)。所列出的部分包含了dataSource, transactionManager, processEngine Activiti 引擎中的services

<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="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
    <property name="dataBaseName" value="h2" />
    <property name="dataSource" ref="dataSource" />
    <property name="transactionManager" ref="transactionManager" />
    <property name="dbSchemaStrategy" value="check-version" />
  </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

...
  <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 we're using a classpath XML resource to configure our Spring application context:

第一个application context则是通过任意Spring方式来构建。在本示例中我们使用类路径下的XML资源文件的方式来创建我们的Spring application context

 

ClassPathXmlApplicationContext applicationContext = 
    new ClassPathXmlApplicationContext("org/activiti/examples/spring/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:

接下来我们就可以获取相应的service bean并调用相应的方法。ProcessEngineFactoryBean添加了一个事务拦截器,为Activiti service中的方法实施Propagation.REQUIRED事务。因此我们像下面代码一样通过repositoryService来部署我们的流程:

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

 

The next section shows how invocation of the userBean. In this case, the Spring transaction will be around the userBean.hello() method and the Activiti service method invocation will join that same transaction.

下面的示例则是展示了如何调用userBean。在本示例中,userBean.hello()中则会织入Spring事务,并且Activiti所提供的业务方法也将加入相应的事务。

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流程所定义的表达式都可以“看”到Springbean。例如,SpringTransactionIntegrationTest  hello.bpmn20.xml将展示通过UEL方法表达式如何调用Springbean的方法:

<definitions id="definitions" ...>
  
  <process id="helloProcess">
  
    <startEvent id="start" />
    <sequenceFlow id="flow1" sourceRef="start" targetRef="print" />
    
    <serviceTask id="print" 
                 activiti:method-expr="#{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配置(上面已经描述)如下:

<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 configuration of the ProcessEngineFactoryBean, you can specify a set of resources. When the ProcessEngineFactoryBean is initialized, 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.

Spring集成时提供另外一个功能就是部署资源文件。在配置ProcessEngineFactoryBean时你可以指定一系列的资源文件。当ProcessEngineFactoryBean初始化时,这些资源文件将被扫描和部署。部署时可以使用通配符进行配置,并且防止重复部署。只有资源文件被改变时,系统才会自动重新部署到Activiti数据库中。

 

Here's an example

示例如下:

<beans ...>
  ...
  <bean id="processEngine" class="org.activiti.engine.impl.cfg.spring.ProcessEngineFactoryBean">
    ...
    <property name="deploymentResources" value="classpath*:/org/activiti/examples/spring/autodeploy.*.bpmn20.xml" />
  </bean>

</beans>

 

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测试可以轻松的进行业务流程的单元测试。下面的示例展现了如何利用基于Spring的单元测试进行业务流程的测试:

@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(用于上面示例中的自动注入)。

<bean id="activitiRule" class="org.activiti.engine.test.ActivitiRule">
  <property name="processEngine" ref="processEngine" />
</bean>   
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,Activity是Android系统中的四大组件之一,可以用于显示View。Activity是一个与用记交互的系统模块,几乎所有的Activity都是和用户进行交互的,但是如果这样就能说Activity主要是用来显示View就不太正确了。 在深入了解Activity之前,我们先要知道一下MVC设计模式,在JAVAEE 中MVC设计模式已经很经典了,而且分的也比较清晰了,但是在Android中,好多人对MVC在Android开发中的应用不是很清楚,下面我就先来介绍一下MVC在Android开发中的应用: M(Model 模型):Model是应用程序的主体部分,所有的业务逻辑都应该写在这里,在Android中Model层与JavaEE中的变化不大,如:对数据库的操作,对网络等的操作都放在该层(但不是说它们都放在同一个包中,可以分开放,但它们统称为Model层)。 V(View 视图):是应用程序中负责生成用户界面的部分,也是在整个MVC架构中用户唯一可以看到的一层,接收用户输入,显示处理结果;在Android应用中一般采用XML文件里德界面的描述,使用的时候可以非常方便的引入,当然也可以使用JavaScript+Html等方式作为View。 C(Controller控制层)android的控制层的重任就要落在众多的activity的肩上了,所以在这里就要建议大家不要在activity中写太多的代码,尽量能过activity交割Model业务逻辑层处理。 好了,在介绍过Android应用开发中的MVC架构后,我们就可以很明确的知道,在Android中Activity主要是用来做控制的,它可以选择要显示的View,也可以从View中获取数据然后把数据传给Model层进行处理,最后再来显示出处理结果。 介绍过Activity的主要作用后,那么我们就要详细说一下Activity了。 Activity生命周期图 Activity 的生命周期是被以下的函数控制的。 public class Activity extends ApplicationContext { protected void onCreate(Bundle icicle); protected void onStart(); protected void onRestart(); protected void onResume(); protected void onFreeze(Bundle outIcicle); protected void onPause(); protected void onStop(); protected void onDestroy(); } onCreate(Bundle) 函数是你进行初始化的地方,这个也是执行 setContentView(View)函数的地方,setContentView(View)函数可以传入一个由XML 编制的UI界面,可以使UI和具体实现完全分离。 onPause()函数是处理用户离开当前 Activity 的地方。更重要的是,任何在当前 Activity中的任何改变都要在这个函数中提交。 Activity有四种状态: 活动状态,当Activity处于Stack(栈)顶时,就是手机当前的现实屏幕,这是 Activity就 处于activity或者运行状态。 运行但是失去焦点,当Activity还处于运行状态时,但是屏幕是有另外一个Activity 处于文档处于焦点状态,当前的Activity就处于pause。 停止,当Activity被另一个Activity完全覆盖的时候,就被停止了,其实就是虽然在 运行,但是用户却看不见。 结束,当Activity处于pause或者stop时,系统可以结束 Activity,回收资源,这 是Activity就是处于结束状态了。 处于结束状态的是Activity,如果要使用户可见,只要重启才可以。 Activity的响应时间 当前Activity所在的线程为主线程,它的响应时间为5秒,如果在当前运行的Activity中进行耗时的操作且响应时间起过5秒,那么程序就会报ANR错误。所以,这也是不建议在Activity中写太多复杂代码的原因之一。 当然,有些代码只能写在Activity中,不然就运行不了(它们不是生命周期方法),比如你想要获得android系统或者硬件一的些信息,就必须在Activity中写出来,如果单独写一个工具类获得不了。
城市应急指挥系统是智慧城市建设的重要组成部分,旨在提高城市对突发事件的预防和处置能力。系统背景源于自然灾害和事故灾难频发,如汶川地震和日本大地震等,这些事件造成了巨大的人员伤亡和财产损失。随着城市化进程的加快,应急信息化建设面临信息资源分散、管理标准不统一等问题,需要通过统筹管理和技术创新来解决。 系统的设计思路是通过先进的技术手段,如物联网、射频识别、卫星定位等,构建一个具有强大信息感知和通信能力的网络和平台。这将促进不同部门和层次之间的信息共享、交流和整合,提高城市资源的利用效率,满足城市对各种信息的获取和使用需求。在“十二五”期间,应急信息化工作将依托这些技术,实现动态监控、风险管理、预警以及统一指挥调度。 应急指挥系统的建设目标是实现快速有效的应对各种突发事件,保障人民生命财产安全,减少社会危害和经济损失。系统将包括预测预警、模拟演练、辅助决策、态势分析等功能,以及应急值守、预案管理、GIS应用等基本应用。此外,还包括支撑平台的建设,如接警中心、视频会议、统一通信等基础设施。 系统的实施将涉及到应急网络建设、应急指挥、视频监控、卫星通信等多个方面。通过高度集成的系统,建立统一的信息接收和处理平台,实现多渠道接入和融合指挥调度。此外,还包括应急指挥中心基础平台建设、固定和移动应急指挥通信系统建设,以及应急队伍建设,确保能够迅速响应并有效处置各类突发事件。 项目的意义在于,它不仅是提升灾害监测预报水平和预警能力的重要科技支撑,也是实现预防和减轻重大灾害和事故损失的关键。通过实施城市应急指挥系统,可以加强社会管理和公共服务,构建和谐社会,为打造平安城市提供坚实的基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值