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

发布于 2024-11-19 06:39:08 字数 3817 浏览 5 评论 0原文

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

我的工作流程中有一个 ServiceTask 解析为 Spring 托管 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 包装异常(带有状态模板):委托表达式 ${taskBean} 未解析为接口 org.activiti.engine.impl.pvm.delegate.ActivityBehavior 或接口 org.activiti.engine.delegate.JavaDelegate 的实现

Any/All 的实现回复表示赞赏! 提前致谢。

I am trying to get a workflow working using Activiti 5.5 using a Spring managed process engine and I'm having some trouble.

I have a ServiceTask in my workflow that resolves to a Spring Managed bean. It looks like this:

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

I am not starting the process via code, the process is either started via the activti-rest api or a form. How can I get the context in which this task is executing from inside the bean, so that I might be able to add a process variable that could be referenced in a later task, such as an email. I tried looking at the spring examples that come with Activiti 5.5 and I do not see how my example is any different from the examples. I am implementing the JavaDelegate interface the same wat the spring example shows.

Here is my code:

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.");
        }
    }
}

}

Here is the spring config (boring parts left out for brevity):

<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>

When I run the worflow, I get an error:

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

Any/All replies appreciated!
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半岛未凉 2024-11-26 06:39:08

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

  1. 您应该创建一个 spring 托管 bean(不需要实现 JavaDelegate),
  2. 在 ServiceTask 中使用此配置: activiti:express="${taskBean.someMethod()}"

我总是使用此配置在春天的环境中。希望有帮助!

列维

You can use spring beans in service tasks this way also:

  1. You should make a spring managed bean (implements JavaDelegate not needed)
  2. use this configuration in ServiceTask: activiti:expression="${taskBean.someMethod()}"

I always use this configuration in spring environment. Hope it helps!

Levi

温柔戏命师 2024-11-26 06:39:08
  1. 获取 ProcessEngine
  2. 获取任务服务
  3. 创建一个查询来查找流程实例,taskService.createTaskQuery()
  4. 最后,task.getProcessInstanceId()
  1. Get the ProcessEngine
  2. Get the task service
  3. Create a query to find the process instance, taskService.createTaskQuery()
  4. Finally, task.getProcessInstanceId()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文