如何在SpringBean中获取当前的Activiti ProcessInstance?

时间:2011-07-09 19:25:22

标签: spring process workflow bpmn activiti

我正在尝试使用Spring托管流程引擎使用Activiti 5.5来工作,我遇到了一些麻烦。

我的工作流程中有一个ServiceTask解析为Spring Managed bean。它看起来像这样:

<serviceTask id="springTask1" name="BeanTest" activiti:delegateExpression="${taskBean}"></serviceTask>

我没有通过代码启动该过程,该过程可以通过activti-rest api或表单启动。如何从bean内部获取执行此任务的上下文,以便我可以添加可在以后的任务中引用的流程变量,例如电子邮件。我试着查看Activiti 5.5附带的Spring示例,但我看不出我的示例与示例有什么不同。我正在实现JavaDelegate接口,就像Spring的例子所示。

这是我的代码:

public class GetBeanTest implements JavaDelegate {

private ContactService contactService;

public GetBeanTest() {
    super();
}

public String getContactName(String contactName) throws Exception {
    String retVal= "unknown";
    if(contactService == null){
        System.out.println("Bean was null!");
    }else{
        System.out.println("Bean is valid!");
        List<Contact> contacts= contactService.getContacts();
        System.out.println("There are " + contacts.size() +" in the contact list.");
        for (Contact contact : contacts) {
            if(contact.getName().equalsIgnoreCase(contactName)){
                System.out.println("Found the contact! " + contactName );
                retVal= contact.getEmail();
            }
        }
    }
    return retVal;

}

public void setContactService(ContactService contactService) {
    this.contactService = contactService;
}

@Override
public void execute(DelegateExecution execution) throws Exception {
    System.out.println("+++++++++++++ in execute ++++++++++++++++");
    System.out.println("Event Name: " + execution.getEventName());
    System.out.println("ID: " + execution.getId());
    System.out.println("Process Instance ID: " + execution.getProcessInstanceId());
    Set<String> varNames= execution.getVariableNames();
    for (String string : varNames) {
        System.out.println("Varible Named " + string + " exists");
        if(string.equalsIgnoreCase("contactName")){
            String contactName= (String) execution.getVariable(string);
            getContactName(contactName);
        }else{
            System.out.println("unable to find contact name.");
        }
    }
}

}

这是弹簧配置(为简洁起见省略了无聊的部分):

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

<!--Dao Beans -->
<bean id="contactDao" class="org.psc.database.dao.jpa.ContactDaoImpl"/>

<!--  Service Beans -->

  <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" />
  <bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" />

<bean id="contactService" class="org.psc.service.impl.ContactServiceImpl">
    <property name="contactDao" ref="contactDao"/>
</bean>

<bean id="contact" class="org.psc.bpmn.tasks.Contact"/>
<bean id="taskBean" class="org.psc.bpmn.examples.GetBeanTest">
        <property name="contactService" ref="contactService"/>
</bean>

当我运行worflow时,出现错误:

  

06090000 Wrapped Exception(带有状态模板):委托表达式$ {taskBean}没有解析为接口org.activiti.engine.impl.pvm.delegate.ActivityBehavior的实现,也没有解析为org.activiti.engine.delegate.JavaDelegate接口

任何/所有回复都表示赞赏! 提前谢谢。

2 个答案:

答案 0 :(得分:1)

您也可以通过这种方式在服务任务中使用spring bean:

  1. 您应该创建一个spring托管bean(不需要实现JavaDelegate)
  2. 在ServiceTask中使用此配置:activiti:expression =“$ {taskBean.someMethod()}”
  3. 我总是在spring环境中使用这个配置。希望它有所帮助!

    列维

答案 1 :(得分:0)

  1. 获取ProcessEngine
  2. 获取任务服务
  3. 创建查询以查找流程实例taskService.createTaskQuery()
  4. 最后,task.getProcessInstanceId()