Handling Gateways

In this tutorial, we will focus on two strategies for working with gateways:

  • Using implicit variables
  • Using a java delegate

Using implicit variable

To explore the use of gateways, let’s extend the Travel Plan Process to make it a little bit cleaver.

part2-process-with-gateways

On this version of the process, the hotel booking task is skipped for same-day trips. The fork exclusive gateway (the first one) has two outputs. The “more than 1-day trip” flow is executed if the travel has more than 1 day. Otherwise the “same day trip” flow is executed.

The easiest way to configure this gateway is by using the implicit variable processInstance.

To do so, we can set the “more than 1-day trip” flow as follows:

  • Condition type: Expression
  • Expression: ${processInstance.travelPlan.endDate > processInstance.travelPlan.startDate}

The variable processInstance is automatically set by the AgileKip Reference Architecture and represents an instance of the process-binding entity (in our example TravelPlanProcess). As we showed in the previous tutorial Getting Started, the process-binding entity has a reference to the domain entity (TravelPlan). Finally, the TravelPlan entity has two date fields (startDate and endDate). The condition expression evaluates to true whether the end date is after the start date.

part2-gateway-flow-more-than-one-day-trip

A similar configuration can be done in the “same day trip” flow:

  • Condition type: Expression
  • Expression: ${! (processInstance.travelPlan.endDate > processInstance.travelPlan.startDate) }

part2-gateway-flow-same-day-trip

Note: You can checkout the tag part2-bpmn-with-simple-gateways from the TravelPlan repository on Github to get the BPMN file src/main/resources/TravelPlanProcess.bpmn used in this step. git checkout part2-bpmn-with-simple-gateways.

Using a java delegate

As we said before, using conditional expressions with the implicit variable processInstance is the easiest way to configure gateways. However, there are many situations for which these expressions are not suitable. For example for complex expressions or for conditinal expressions that requires a more sofisticated algorithm. In this scenarios, we can use java delegates to configure gateways.

Let’s update the Travel Plan Process to use this strategy:

part2-process-with-delegate-gateways

In this version of the process, we included a service task just before the fork gateway. The aim of this service task is to calculate whether the trip has more than one day and set the result of this calculation as a variable in the process engine so it can be used later in the process.

The service task is configured as follow:

  • Task type: Service Task
  • Implementation: Delegate Expression
  • Delegate Expression: ${calculateSameDayTripDelegate}

part2-process-with-delegate-gateways-delegate-configuration

Now it is necessary to create a java class named CalculateSameDayTripDelegate that implements the interface JavaDelegate and mark it with the @Component Spring annotation. An implementation of this delagate is shown below:

package com.mycompany.myapp.delegate;

import com.mycompany.myapp.service.dto.TravelPlanProcessDTO;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.springframework.stereotype.Component;

@Component
public class CalculateSameDayTripDelegate implements JavaDelegate {

    @Override
    public void execute(DelegateExecution delegateExecution) throws Exception {
        TravelPlanProcessDTO travelPlanProcess = (TravelPlanProcessDTO) delegateExecution.getVariable("processInstance");
        Boolean moreThan1DayTrip = travelPlanProcess.getTravelPlan().getEndDate().isAfter(travelPlanProcess.getTravelPlan().getStartDate());
        delegateExecution.setVariable("moreThan1DayTrip", moreThan1DayTrip);
    }
}

Now that the delegate makes things simpler, we can just use the moreThan1DayTrip variable on the gateway flows configuration:

For the more than 1 day trip flow:

  • Condition type: Expression
  • Expression: ${moreThan1DayTrip}

part2-process-with-delegate-gateways-flow1-configuration

For the same day trip flow:

  • Condition type: Expression
  • Expression: ${!moreThan1DayTrip}

part2-process-with-delegate-gateways-flow2-configuration

Note: You can checkout the tag part2-bpmn-with-delegate-and-gateway from the TravelPlan repository on Github to get the BPMN file src/main/resources/TravelPlanProcess.bpmn and the delegate CalculateSameDayTripDelegate used in this step. git checkout part2-bpmn-with-delegate-and-gateway.

Summary

In this tutorial we showed two strategies to handle gateways: (a) Using the processInstance implicit variable, and (b) Using a delegate. Although using the implicit variable is simpler, there are situation for which the use of such strategy is not suitable. For these more complicated situations, you can use a delegate just before the gateway to set a variable to be used in the flows configuration.