pille (1) [Avatar] Offline
#1
Hi,

Chapter 9.1 describes how a bean can be @Autowired.
However, when I give the Bean the scope="process" then I get a null pointer exception in the class that uses it. Is this feature still there? Or do you see what I do wrong?

Thanks!


________________MAIN________________________

public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:activiti-context.xml");

Initiator initiator = context.getBean(Initiator.class);
initiator.startProcess();

//RuntimeService runtimeService = context.getBean(RuntimeService.class);
//runtimeService.startProcessInstanceByKey("myProcess");

}

________________INITIATOR___________________

public class Initiator {

@StartProcess(processKey="myProcess")
public void startProcess() {}

}


________________MyProcess.bpmn________________________

<process id="myProcess" name="My process" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<serviceTask id="servicetask1" name="Service Task 1" activiti:expression="#{serviceTask1.execute()}"></serviceTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
<serviceTask id="servicetask2" name="Service Task 2" activiti:expression="#{serviceTask2.execute()}"></serviceTask>
<sequenceFlow id="flow3" sourceRef="servicetask1" targetRef="servicetask2"></sequenceFlow>
<endEvent id="terminateendevent1" name="TerminateEndEvent">
<terminateEventDefinition></terminateEventDefinition>
</endEvent>
<sequenceFlow id="flow4" sourceRef="servicetask2" targetRef="terminateendevent1"></sequenceFlow>
</process>

________________Service Task 1_________________

public class ServiceTask1{

public void execute() throws InterruptedException {
System.out.println("Service Task 1 - Start: " + DateTime.now().getSecondOfMinute());
Thread.sleep(3000);
System.out.println("Service Task 1 - End: " + DateTime.now().getSecondOfMinute());
}

}

________________Service Task 2 - Autowired Bean____________

public class ServiceTask2{

@Autowired
private DateTimeProvider dateTimeProvider;

public void execute(){
System.out.println( "Service Task 2: " + dateTimeProvider.getTime().getSecondOfMinute() );
}


}

________________DateTimeProvider____________

public class SystemDateTimeProvider implements DateTimeProvider {

public DateTime getTime() {
return DateTime.now();
}

}


________________activiti-context.xml____________

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" >
xmlnssmiliesi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:p="http://www.springframework.org/schema/p" xmlns:activiti="http://www.activiti.org/schema/spring/components"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.activiti.org/schema/spring/components
http://www.activiti.org/schema/spring/components/activiti.xsd">


<!-- -->
<!-- activate annotation config -->
<!-- -->
<context:annotation-config />
<context:component-scan base-package="de.phil" />
<tx:annotation-driven />
<activiti:annotation-driven process-engine="processEngine" />


<bean id="initiator" class="de.phil.Initiator" />

<bean id="serviceTask1" class="de.phil.servicetasks.ServiceTask1" />
<bean id="serviceTask2" class="de.phil.servicetasks.ServiceTask2" />

<bean id="dateTimeProvider" class="de.phil.impl.SystemDateTimeProvider" scope="process" />

<bean id="dataSource" >
class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>

<bean id="transactionManager" >
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="databaseType" value="h2" />
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseSchemaUpdate" value="true" />
<property name="jobExecutorActivate" value="false" />

<property name="deploymentResources" value="classpath*:*.bpmn" />
</bean>

<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<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="historyService" factory-bean="processEngine" >
factory-method="getHistoryService" />
<bean id="managementService" factory-bean="processEngine" >
factory-method="getManagementService" />


</beans>
tijs.rademakers (494) [Avatar] Offline
#2
Re: Process scoped Beans
Hi,

That's more a Spring question/issue than Activiti.
To my knowledge, when you define a bean in the Spring config you should configure its wiring there as well. Or you should not define the bean in the Spring config and use Annotations. In this example you've combined both approaches, and that doesn't work I think.

Best regards,

Tijs