
Vamos a ver un ejemplo de como es esto en Activiti. Para obtener el valor de una variable mientras el proceso aún está en ejecución se puede conseguir con:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
runtimeService.getVariable(processInstance.getId(), "variable"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
historyService.createHistoricDetailQuery().variableUpdates().orderByVariableName().orderByTime().list(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL http://www.omg.org/spec/BPMN/2.0/20100501/BPMN20.xsd" targetNamespace="HelloWorldActiviti"> | |
<process id="variables" name="Variables" isExecutable="true"> | |
<startEvent id="start" /> | |
<sequenceFlow id="flow1" sourceRef="start" targetRef="descuentoTask" /> | |
<scriptTask id="descuentoTask" scriptFormat="groovy"> | |
<script> | |
output.descuento = new java.math.BigDecimal('0') | |
if (cliente.tipo == es.com.blogspot.elblogdepicodev.activiti.misc.Cliente.Tipo.VIP) { | |
output.descuento = new java.math.BigDecimal('10') | |
} | |
</script> | |
</scriptTask> | |
<sequenceFlow id="flow2" sourceRef="descuentoTask" targetRef="end" /> | |
<endEvent id="end" /> | |
</process> | |
</definitions> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package es.com.blogspot.elblogdepicodev.activiti; | |
import java.math.BigDecimal; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.activiti.engine.RuntimeService; | |
import org.activiti.engine.test.ActivitiRule; | |
import org.activiti.engine.test.Deployment; | |
import org.junit.Assert; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import es.com.blogspot.elblogdepicodev.activiti.misc.Cliente; | |
import es.com.blogspot.elblogdepicodev.activiti.misc.Cliente.Tipo; | |
@SuppressWarnings({ "rawtypes", "unchecked" }) | |
public class VariablesTest extends ActivitiAbstractTest { | |
@Rule | |
public ActivitiRule activitiRule = new ActivitiRule("activiti-h2.cfg.xml"); | |
@Test | |
@Deployment(resources = "bpmn/Variables.bpmn20.xml") | |
public void testNormal() { | |
Cliente cliente = new Cliente(Tipo.NORMAL); | |
Map output = new HashMap(); | |
Map variables = new HashMap(); | |
variables.put("cliente", cliente); | |
variables.put("output", output); | |
RuntimeService rs = activitiRule.getRuntimeService(); | |
rs.startProcessInstanceByKey("variables", variables); | |
Assert.assertEquals(new BigDecimal("0"), output.get("descuento")); | |
} | |
@Test | |
@Deployment(resources = "bpmn/Variables.bpmn20.xml") | |
public void testVIP() { | |
Cliente cliente = new Cliente(Tipo.VIP); | |
Map output = new HashMap(); | |
Map variables = new HashMap(); | |
variables.put("cliente", cliente); | |
variables.put("output", output); | |
RuntimeService rs = activitiRule.getRuntimeService(); | |
rs.startProcessInstanceByKey("variables", variables); | |
Assert.assertEquals(new BigDecimal("10"), output.get("descuento")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package es.com.blogspot.elblogdepicodev.activiti; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.activiti.engine.ProcessEngine; | |
import org.activiti.engine.ProcessEngineConfiguration; | |
import org.activiti.engine.ProcessEngines; | |
import org.activiti.engine.RepositoryService; | |
import org.activiti.engine.RuntimeService; | |
import org.activiti.engine.runtime.ProcessInstance; | |
import org.h2.tools.Server; | |
import es.com.blogspot.elblogdepicodev.activiti.misc.Cliente; | |
import es.com.blogspot.elblogdepicodev.activiti.misc.Cliente.Tipo; | |
public class Variables { | |
public static void main(String[] args) throws Exception { | |
Server server = null; | |
try { | |
server = Server.createTcpServer().start(); | |
ProcessEngines.init(); | |
ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-mysql.cfg.xml").buildProcessEngine(); | |
RuntimeService runtimeService = processEngine.getRuntimeService(); | |
RepositoryService repositoryService = processEngine.getRepositoryService(); | |
repositoryService.createDeployment().addClasspathResource("bpmn/Variables.bpmn20.xml").deploy(); | |
Cliente cliente = new Cliente(Tipo.VIP); | |
Map variables = new HashMap(); | |
Map output = new HashMap(); | |
variables.put("cliente", cliente); | |
variables.put("output", output); | |
ProcessInstance pi = runtimeService.startProcessInstanceByKey("variables", variables); | |
System.out.println("Descuento aplicado: " + output.get("descuento")); | |
} finally { | |
ProcessEngines.destroy(); | |
if (server != null) | |
server.stop(); | |
} | |
} | |
} |
Referencia:
Conceptos sobre procesos de negocio (BP, BPM, BPMS, ...)
Procesos de negocio con Activiti
Tratamiento de errores en procesos de negocio con Activiti
Reglas de negocio con Drools y Activiti
Código fuente del ejemplo Usando variables con Activiti