adding a lot of simple examples of the activiti gateway support.
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package org.springframework.integration.activiti;
|
||||
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* Simple bean that you want to use from Activiti.
|
||||
* You've configured it all from Spring and don't want to
|
||||
* have to rencode all the configuration inside of Activiti
|
||||
*/
|
||||
public class CustomerService {
|
||||
|
||||
private DataSource dataSource ;
|
||||
private SimpleJdbcTemplate jdbcTemplate;
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
this.jdbcTemplate = new SimpleJdbcTemplate ( dataSource);
|
||||
}
|
||||
|
||||
public void createCustomer( String firstName, String lastName , String email ){
|
||||
Map<String,Object> parms = new HashMap<String,Object>() ;
|
||||
parms.put( "fn", firstName ) ;
|
||||
parms.put( "ln", lastName);
|
||||
parms.put( "email", email );
|
||||
|
||||
this.jdbcTemplate.update(
|
||||
"INSERT INTO CUSTOMER( FIRST_NAME, LAST_NAME, EMAIL) VALUES(:fn,:ln,:email)" ,
|
||||
parms );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.springframework.integration.activiti.signup;
|
||||
|
||||
import org.activiti.engine.impl.bpmn.BpmnActivityBehavior;
|
||||
import org.activiti.pvm.activity.ActivityBehavior;
|
||||
import org.activiti.pvm.activity.ActivityExecution;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class CheckForm extends BpmnActivityBehavior implements ActivityBehavior {
|
||||
|
||||
public void execute(ActivityExecution activityExecution) throws Exception {
|
||||
System.out.println( getClass()+" : checking form: ");
|
||||
|
||||
boolean formOK = Math.random() > .7 ;
|
||||
|
||||
activityExecution.setVariable( "formOK", formOK);
|
||||
System.out.println( getClass()+" : form OK? " + formOK);
|
||||
|
||||
|
||||
performDefaultOutgoingBehavior( activityExecution );
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.springframework.integration.activiti.signup;
|
||||
|
||||
import org.activiti.engine.impl.bpmn.BpmnActivityBehavior;
|
||||
import org.activiti.pvm.activity.ActivityBehavior;
|
||||
import org.activiti.pvm.activity.ActivityExecution;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
||||
@Component
|
||||
public class SendEmail extends BpmnActivityBehavior implements ActivityBehavior {
|
||||
|
||||
public void execute(ActivityExecution activityExecution) throws Exception {
|
||||
System.out.println( getClass() + ": sending email ") ;
|
||||
|
||||
this.performDefaultOutgoingBehavior( activityExecution);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package org.springframework.integration.activiti.signup;
|
||||
|
||||
import org.activiti.engine.ProcessEngine;
|
||||
import org.activiti.engine.RepositoryService;
|
||||
import org.activiti.engine.RuntimeService;
|
||||
import org.activiti.engine.TaskService;
|
||||
import org.activiti.engine.task.Task;
|
||||
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
|
||||
@Component
|
||||
public class SignupClient {
|
||||
private TaskService taskService;
|
||||
private RuntimeService runtimeService;
|
||||
private RepositoryService repositoryService;
|
||||
|
||||
@Resource
|
||||
private ProcessEngine processEngine;
|
||||
|
||||
@PostConstruct
|
||||
public void start() throws Exception {
|
||||
this.taskService = this.processEngine.getTaskService();
|
||||
this.repositoryService = this.processEngine.getRepositoryService();
|
||||
this.runtimeService = this.processEngine.getRuntimeService();
|
||||
|
||||
repositoryService.createDeployment().addClasspathResource("processes/signup.bpmn20.xml").deploy();
|
||||
}
|
||||
|
||||
private List<Task> tasksForUser(String user, String name)
|
||||
throws Exception {
|
||||
return taskService.createTaskQuery().name(name).assignee(user).list();
|
||||
}
|
||||
|
||||
private boolean handleWorkForUser(String user, String taskName)
|
||||
throws Exception {
|
||||
|
||||
boolean workToBeDone = false;
|
||||
for (Task t : tasksForUser(user, taskName)) {
|
||||
workToBeDone = true ;
|
||||
System.out.println("starting " + t.getId() + " : " + t.getName());
|
||||
taskService.complete(t.getId());
|
||||
System.out.println("completed " + t.getId() + " : " + t.getName());
|
||||
}
|
||||
return workToBeDone ;
|
||||
}
|
||||
|
||||
public void startSignupProcess(long customerId) throws Exception {
|
||||
Map<String, Object> parms = new HashMap<String, Object>();
|
||||
parms.put("customerId", customerId);
|
||||
|
||||
runtimeService.startProcessInstanceByKey("signup", parms);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("/org/springframework/integration/activiti/SignupTest-context.xml");
|
||||
classPathXmlApplicationContext.start();
|
||||
|
||||
SignupClient signupClient = classPathXmlApplicationContext.getBean(SignupClient.class);
|
||||
signupClient.startSignupProcess(System.currentTimeMillis());
|
||||
signupClient.handleWorkForUser("customer", "sign-up");
|
||||
|
||||
while(signupClient.handleWorkForUser( "customer", "fix-errors"))
|
||||
;
|
||||
|
||||
signupClient.handleWorkForUser("customer", "confirm-email");
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:activiti="http://www.springframework.org/schema/integration/activiti"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/integration/activiti http://www.springframework.org/schema/integration/activiti/spring-integration-activiti-2.0.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
|
||||
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
|
||||
">
|
||||
|
||||
|
||||
<context:component-scan base-package="org.springframework.integration.activiti.signup"/>
|
||||
|
||||
<bean id="dataSource"
|
||||
class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
|
||||
<property name="targetDataSource">
|
||||
<bean class="org.apache.commons.dbcp.BasicDataSource"
|
||||
p:url="jdbc:h2:tcp://localhost/~/activiti_example"
|
||||
p:driverClassName="org.h2.Driver"
|
||||
p:username="sa"
|
||||
p:password=""
|
||||
/>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="transactionManager"
|
||||
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
|
||||
p:dataSource-ref="dataSource"/>
|
||||
|
||||
<bean id="processEngine"
|
||||
class="org.activiti.engine.impl.cfg.spring.ProcessEngineFactoryBean"
|
||||
p:dataSource-ref="dataSource"
|
||||
p:transactionManager-ref="transactionManager"
|
||||
p:dbSchemaStrategy="CHECK_VERSION"
|
||||
>
|
||||
<property name="deploymentResources"
|
||||
value="classpath:/processes/signup.bpmn20.xml"/>
|
||||
</bean>
|
||||
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
|
||||
">
|
||||
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
|
||||
<property name="targetDataSource">
|
||||
<bean class="org.apache.commons.dbcp.BasicDataSource"
|
||||
p:url="jdbc:h2:tcp://localhost/~/activiti_example"
|
||||
p:driverClassName="org.h2.Driver"
|
||||
p:username="sa"
|
||||
p:password=""
|
||||
/>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="customerService" class="org.springframework.integration.activiti.CustomerService">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
</bean>
|
||||
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,74 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<definitions id="definitions"
|
||||
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:activiti="http://activiti.org/bpmn-extensions"
|
||||
typeLanguage="http://www.w3.org/2001/XMLSchema"
|
||||
expressionLanguage="http://www.w3.org/1999/XPath"
|
||||
targetNamespace="http://www.activiti.org/bpmn2.0">
|
||||
|
||||
<process id="signup">
|
||||
<startEvent id="start"/>
|
||||
|
||||
<sequenceFlow sourceRef="start" targetRef="sign-up"/>
|
||||
|
||||
<userTask name = "sign-up" id="sign-up">
|
||||
<documentation>
|
||||
the potential customer must fill out the form online first
|
||||
</documentation>
|
||||
<humanPerformer>
|
||||
<resourceAssignmentExpression>
|
||||
<formalExpression>customer</formalExpression>
|
||||
</resourceAssignmentExpression>
|
||||
</humanPerformer>
|
||||
</userTask>
|
||||
|
||||
<sequenceFlow sourceRef="sign-up" targetRef="check-form"/>
|
||||
|
||||
<serviceTask id="check-form" activiti:class = "#{checkForm}"/>
|
||||
|
||||
<sequenceFlow sourceRef="check-form" targetRef="form-completed-decision-gateway"/>
|
||||
|
||||
<exclusiveGateway gatewayDirection="Diverging" id="form-completed-decision-gateway"/>
|
||||
|
||||
<sequenceFlow id="formOK" sourceRef="form-completed-decision-gateway" targetRef="send-confirmation-email">
|
||||
<conditionExpression xsi:type="tFormalExpression">${formOK == true}</conditionExpression>
|
||||
</sequenceFlow>
|
||||
|
||||
<sequenceFlow id="formNotOK" sourceRef="form-completed-decision-gateway" targetRef="fix-errors">
|
||||
<conditionExpression xsi:type="tFormalExpression">${formOK == false}</conditionExpression>
|
||||
</sequenceFlow>
|
||||
|
||||
<userTask name="fix-errors" id="fix-errors">
|
||||
<documentation>
|
||||
the potential customer must fill out the form online first
|
||||
</documentation>
|
||||
<humanPerformer>
|
||||
<resourceAssignmentExpression>
|
||||
<formalExpression>customer</formalExpression>
|
||||
</resourceAssignmentExpression>
|
||||
</humanPerformer>
|
||||
</userTask>
|
||||
|
||||
<sequenceFlow sourceRef="fix-errors" targetRef="check-form"/>
|
||||
|
||||
<serviceTask id="send-confirmation-email" activiti:class = "#{sendEmail}"/>
|
||||
|
||||
<sequenceFlow sourceRef="send-confirmation-email" targetRef="confirm-email"/>
|
||||
|
||||
<userTask name="confirm-email" id="confirm-email">
|
||||
<documentation>
|
||||
confirm the user's email
|
||||
</documentation>
|
||||
<humanPerformer>
|
||||
<resourceAssignmentExpression>
|
||||
<formalExpression>customer</formalExpression>
|
||||
</resourceAssignmentExpression>
|
||||
</humanPerformer>
|
||||
</userTask>
|
||||
|
||||
<sequenceFlow sourceRef="confirm-email" targetRef="end"/>
|
||||
|
||||
<endEvent id="end"/>
|
||||
</process>
|
||||
</definitions>
|
||||
Reference in New Issue
Block a user