cancel
Showing results for 
Search instead for 
Did you mean: 

How do I get the current ProcessInstance in SpringBean?

mgriffith
Champ in-the-making
Champ in-the-making
Hello all,

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

<serviceTask id="springTask1" name="BeanTest" activiti:expression="${taskBean.execute(contactName)}"></serviceTask>

I am not starting the process via code, but 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 that the task invokes, so that I might be able to add a process variable that could be referenced in a later task, such as an email.

My process engine is Spring enabled.   Here's a sample of what my managed bean looks like that is being invoked from the expression:

package org.psc.bpmn.examples;

import java.util.List;

import org.activiti.engine.RuntimeService;
import org.psc.bpmn.tasks.Contact;
import org.psc.service.ContactService;

public class GetBeanTest {
   
   public GetBeanTest() {
      super();
   }

   private ContactService contactService;
   private RuntimeService runtimeService;
   
   public void execute(String contactName) throws Exception {
      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 );
                                        // How do I get the current process so I can put some new process variable into it?
                                        // runtimeService.?????
            }
         }
      }

   }

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

I hope my question is clear. 

Thanks in advance for any replies.
4 REPLIES 4

mgriffith
Champ in-the-making
Champ in-the-making
Looking at the spring examples that ship with activiti, I think all I need to do is implement JavaDelegate and change my ServiceTask from an expression (which was working) to a DelegateExpression in the model.

I did that, and implemented JavaDelegate.  When I execute the workflow, I get an error

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

When the task is fired.  I cannot see how this is any different from the example provided.

My project source is attached.  My bean looks like this:

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

mgriffith
Champ in-the-making
Champ in-the-making
Bump.  Any suggestions?

jbarrez
Star Contributor
Star Contributor
So if I understand you correctly, you have a working JavaDelegate, and want the process instance, right?

If you are using Spring, you don't need to implement any class, you can just do myclass.someMethod(execution), which will pass the current execution, from which you can obtain the current process instance.

mgriffith
Champ in-the-making
Champ in-the-making
Thanks for the reply. I didn't realize I could just reference the execution in an expression.