cancel
Showing results for 
Search instead for 
Did you mean: 

@Autowired private RuntimeService runtimeService; is giving null

logicallimit
Confirmed Champ
Confirmed Champ
I am trying to build a spring web mvc application using Activiti 5.19.0.2.

When I try to call Activiti API from a java class  where I use @Autowired to get instance of activity engine, I am getting null. Am I missing something in my following code.

mvc-dispatcher-servlet.xml

<!– Activiti related beans–>
   <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
      <property name="dataSource" ref="dataSource" />
      <property name="transactionManager" ref="transactionManager" />
      <property name="databaseSchemaUpdate" value="true" />
      <property name="jobExecutorActivate" value="true" />
      <property name="enableDatabaseEventLogging" value="true" />
      <!– <property name="customFormTypes">
         <list>
            <bean class="org.activiti.explorer.form.UserFormType" />
            <bean class="org.activiti.explorer.form.ProcessDefinitionFormType" />
            <bean class="org.activiti.explorer.form.MonthFormType" />
         </list>
      </property> –>
      
   </bean>

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

   <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="formService" factory-bean="processEngine"
      factory-method="getFormService" />
   <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" />


And following is my java class code:


   @Autowired
   private RuntimeService runtimeService;
   
   @Autowired
   private RepositoryService repositoryService;
   
   public void startProcess(EmEntity emEntity){
      
      repositoryService.createDeployment().name("Em Test").addInputStream("em-test.bpmn20.xml", ReflectUtil.getResourceAsStream("diagrams/em-test.bpmn")).deploy();
      ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("em-test");
      runtimeService.setVariable(processInstance.getId(), "emitraEntity", emitraEntity);
   }


In above java code both "runtimeService" and "repositoryService" are null.
3 REPLIES 3

logicallimit
Confirmed Champ
Confirmed Champ
There is a typo in the last line code
<code>
runtimeService.setVariable(processInstance.getId(), "emitraEntity", emitraEntity);
</code>

It is actually
<code>
runtimeService.setVariable(processInstance.getId(), "emEntity", emEntity);
</code>

Problem still prevails.

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi,

Can you create jUnit test?
https://forums.activiti.org/content/sticky-how-write-unit-test

Regards
Martin

Thanks for the reply Martin. I was able to solve the problem.

Let me brief about it. There is a service class from where I call the class where Activiti related logic is put up.
<code>
@Service
@Transactional
public class EmServiceImpl implements EmService {

public EmServiceImpl() {
  System.out.println("EmServiceImpl");
}

@Autowired
private EmDAO emDAO;

@Autowired
private ActivitiRuntimeService activitiRuntimeService;

public long createEm(EmEntity emEntity) {
  long applicationNo = emDAO.insertEm(emEntity);
 
                activitiRuntimeService.startProcess(emEntity);

  return applicationNo;
}
}
</code>
<code>
@Service
@Transactional
public class ActivitiRuntimeService {

public ActivitiRuntimeService() {
  // TODO Auto-generated constructor stub
}

@Autowired
private RuntimeService runtimeService;

@Autowired
private RepositoryService repositoryService;

@Transactional
public void startProcess(EmEntity emEntity){
  Map<String, Object> variables = new HashMap<String, Object>();
  variables.put("emEntity", emEntity);

  repositoryService.createDeployment().name("Em Test").addInputStream("em-test.bpmn20.xml", ReflectUtil.getResourceAsStream("diagrams/em-test.bpmn")).deploy();
   
  @SuppressWarnings("unused")
  ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("em-test", variables);
 
}
}
</code>

The above code is corrected code. The problem was that in the service class "EmServiceImpl", I was also creating an object of my custom Activiti related class "ActivitiRuntimeService" through code by calling default constructor. And hence it was probably confusing Spring container and the object was ultimately not even initialized and was null. It was a very kiddish mistake indeed!

So, when we are autowiring to get automatic object, then need to check we are not creating explicit object also in the calling code.