如何在SpringBean中获取当前的Activiti ProcessInstance?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您也可以通过这种方式在服务任务中使用 spring bean:
我总是使用此配置在春天的环境中。希望有帮助!
列维
You can use spring beans in service tasks this way also:
I always use this configuration in spring environment. Hope it helps!
Levi
ProcessEngine
taskService.createTaskQuery()
task.getProcessInstanceId()
ProcessEngine
taskService.createTaskQuery()
task.getProcessInstanceId()