工作流Activiti的学习总结(五) activiti的API和虚拟工作流测试

activitiAPI
    
activiti引擎中,你可以通过多种方式获取ProcessEngine对象,从ProcessEngine中获取各种服务对象,从而访问工作流/BPM方法。ProcessEngine和她的各种服务均为线程安全性服务。在整个项目中每一种服务仅需要保持一个引用。

ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

 

 

 

RuntimeService runtimeService = processEngine.getRuntimeService();

 

RepositoryService repositoryService = processEngine.getRepositoryService();

 

TaskService taskService = processEngine.getTaskService();

 

ManagementService managementService = processEngine.getManagementService();

 

IdentityService identityService = processEngine.getIdentityService();

 

HistoryService historyService = processEngine.getHistoryService();

 

FormService formService = processEngine.getFormService();

工作流Activiti的学习总结(五) activiti的API和虚拟工作流测试
针对ProcessEngine的说明:
       ProcessEngine.getDefaultProcessEngine()
在第一个被调用的时候初始化并创建一个流程引擎,在以后的调用中返回同一个引用的流程引擎对象。针对流程引擎中初始化方法为ProcessEngine.init().销毁方法的方法为ProcessEngine.destroy().

ProcessEngineConfiguration.createProcessEngineConfigurationFromInputStream(inputStream).buildProcessEngine()创建工作流引擎,ProcessEngine见扫描所有的activiti.cfg.xml文件和activiti-context.xml文件。
异常策略:
   
activitiorg.activiti.engine.ActivitiException类为所有异常的基类。ActivitiException为非检查性异常。例如:
        **

        * Called when the task is successfully executed.

        * @param taskId the id of the task to complete, cannot be null.

        * @throws ActivitiException when no task exists with the given id.

        */

       void complete(String taskId);
其他异常类如下:
ActivitiWrongDbException:
     Thrown when the Activiti engine discovers a mismatch between the database schema version and the engine version(schema
版本和引擎版本不匹配报的错误).

ActivitiOptimisticLockingException:
 Thrown when an optimistic locking occurs in the datastore caused by concurrent access of the same data entry.

ActivitiClassLoadingException:
 Thrown when an class requested to load was not found or when error occurred while loading it (eg. JavaDelegates, TaskListeners, ...).
Activiti
的测试:
      activiti
的测试支持Junit3Junit4的单元测试。在junit3的单元测试中,org.activit.engine.test.ActivitiTestCase必须被继承。在setup()方法中,流程引擎要初始化加载classpath下默认的activi.cfg.xml资源文件。针对不同的配置文件可能需要重新getConfigurationResource()方法。在ActivitiTestCase中可以使用org.activiti.engine.test.Deployment注解方法。
        public class MyBusinessProcessTest extends ActivitiTestCase {

           

          @Deployment

          public void testSimpleProcess() {

            runtimeService.startProcessInstanceByKey("simpleProcess");

             

            Task task = taskService.createTaskQuery().singleResult();

            assertEquals("My Task", task.getName());

             

            taskService.complete(task.getId());

            assertEquals(0, runtimeService.createProcessInstanceQuery().count());

          }

        }      
junit4org.activiti.engine.test.ActivitiRule Rule被采用。通过ActivitiRulegetter方法获取相关的服务。在Junit4同样功能的单元测试如下:

public class MyBusinessProcessTest { @Rule public ActivitiRule activitiRule = new ActivitiRule(); @Test @Deployment public void ruleUsageExample() { RuntimeService runtimeService = activitiRule.getRuntimeService(); runtimeService.startProcessInstanceByKey("ruleUsage"); TaskService taskService = activitiRule.getTaskService(); Task task = taskService.createTaskQuery().singleResult(); assertEquals("My Task", task.getName()); taskService.complete(task.getId()); assertEquals(0, runtimeService.createProcessInstanceQuery().count()); } }

 

 

 

 

 

web环境中流程引擎的使用:
   
由于processEngine是一个线程安全性的对象可以容易在多个线程中被共享。在web容器启动加载创建工作流引擎,在容器销毁销毁工作流引擎。可以ServletContextListener实现相关的功能。
       
public class ProcessEnginesServletContextListener implements ServletContextListener {

           

          public void contextInitialized(ServletContextEvent servletContextEvent) {

            ProcessEngines.init();

          }

         

          public void contextDestroyed(ServletContextEvent servletContextEvent) {

            ProcessEngines.destroy();

          }

         

        }

Activiti提供模拟流程的API
  

PvmProcessDefinition processDefinition = new ProcessDefinitionBuilder() .createActivity("a") .initial() .behavior(new WaitState()) .transition("b") .endActivity() .createActivity("b") .behavior(new WaitState()) .transition("c") .endActivity() .createActivity("c") .behavior(new WaitState()) .endActivity() .buildProcessDefinition(); PvmProcessInstance processInstance = processDefinition.createProcessInstance(); processInstance.start(); PvmExecution activityInstance = processInstance.findExecution("a"); assertNotNull(activityInstance); activityInstance.signal(null, null); activityInstance = processInstance.findExecution("b"); assertNotNull(activityInstance); activityInstance.signal(null, null); activityInstance = processInstance.findExecution("c"); assertNotNull(activityInstance);

 

     

 

 activiti中表达式
activiti
使用UEL(Unified Expression Lanuage)标记解析配置文件中表达式。UELEE6特性。
activiti
Expressions支持两种方式:
1.Value expression:
activitiDelegateExecution是在上下文中是使用“execution”在表达式上下文被使用的。所有在使用值表达式是,所有变量和springbeans使用execution将被隐藏,在表达式将不被执行。使用方法如下:
${myVar}   ${myBean.myProperty}
2.Method expression:
使用方法访问的方式。常用方式如下:
${printer.print()}  ${myBean.addNewOrder(“orerName”)} 
 
备注:在activitiexpression支持解析的类型为原始类型,beanslists,arraysmaps

你可能感兴趣的:(Activiti)