2011-07-09 30 views
1

我正在嘗試使用Spring管理的流程引擎使用Activiti 5.5工作,並且遇到了一些麻煩。如何在SpringBean中獲得當前的Activiti ProcessInstance?

我在我的工作流中有一個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."); 
     } 
    } 
} 

}

這裏是Spring配置(鏜部分留出了簡潔):

<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 (with status template): Delegate expression ${taskBean} did not resolve to an implementation of interface org.activiti.engine.impl.pvm.delegate.ActivityBehavior nor interface org.activiti.engine.delegate.JavaDelegate

全部回覆讚賞! 在此先感謝。

回答

1

您可以使用Spring bean的服務任務,這樣也:

  1. 你應該做一個Spring管理豆(實現JavaDelegate不需要)
  2. 使用此配置在ServiceTask:Activiti的:表達式=「$ {taskBean.someMethod()}「

我總是在spring環境中使用這個配置。希望能幫助到你!

Levi