Discover our knowledge. Read our blogs!

Learn more

We build all our solutions with WSO2 and we are proud that we are WSO2 Premier Certified Integration Partner and Value-Added Reseller.

Learn more

Business Process ManagementIf you had the opportunity to work with WSO2 BPS or WSO2 BPMN, you’re familiar with the concept of variables. Just like in other programming languages, variable is in charge to hold some data and the system can make calculations and decisions based on such values. In this article I’ll show you how to read properties from the config file and to include them in your BPMN process definition. This can be very handy as, once you want to change something, it’s not necessary to recompile the code, redeploy the .bar file, even you don’t have to restart the product.

First of all, you need to create a new file call myconfig.properties and place it into EI-HOME/wso2/business-process/conf folder, with such content: 

foo=bar
variable_name=value
another_variable=another-value

 

Next, you’ll need to create new Maven project with such dependencies in your pom.xml:

<dependencies> 
 <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>3.8.1</version>
     <scope>test</scope>
   </dependency>
       
<dependency>
    <groupId>org.activiti</groupId>
    <artifactId>activiti-engine</artifactId>
    <version>5.18.0</version>
</dependency>   
 </dependencies>

 

Make sure you have the proper folder structure like for each maven project.  

Now create two files, as follows: 

 

ConfigApp.java
import org.activiti.engine.delegate.DelegateExecution;

import org.activiti.engine.delegate.JavaDelegate;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Logger; 

public class ConfigApp implements JavaDelegate{     

      private static final Logger LOGGER = Logger.getLogger(ConfigApp.class.getName());
      HashMap<String, String> properties = new HashMap<String, String>();          

      @SuppressWarnings("rawtypes")
      public void execute(DelegateExecution execution) throws Exception {
           
            LOGGER.info("Config File Service Task");
            ConfigFile file = new ConfigFile();           

            this.properties = file.getProperties();                                                 

            Iterator entries = this.properties.entrySet().iterator();
            while (entries.hasNext()) {
                  HashMap.Entry entry = (HashMap.Entry) entries.next();
                String key = entry.getKey().toString();
                String value = entry.getValue().toString();                                  

                execution.setVariable(key, value);
            } 
       

      }
}

  

 

… and ConfigFile.java:
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;
import java.util.logging.Logger; 

public class ConfigFile { 

    private static Logger log = Logger.getLogger(ConfigFile.class.getName());
 
    HashMap<String, String> properties = new HashMap<String, String>();

     public ConfigFile() {
        this.readProperties();
    }

     public void readProperties(){

         Properties prop = new Properties();
        InputStream input = null; 

        try {
            Path currentRelativePath = Paths.get("");
            String absolutePath = currentRelativePath.toAbsolutePath().toString();
            log.info("Current relative path is: " + absolutePath);           

             input = new FileInputStream(absolutePath+"/conf/myconfig.properties");

             // load a properties file
            prop.load(input); 

            Enumeration<?> e = prop.propertyNames();
            while (e.hasMoreElements()) {
                String key = (String) e.nextElement();
                String value = prop.getProperty(key);
                properties.put(key, value);
            }

         } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

     }

    public HashMap<String, String> getProperties(){
       return this.properties;
    }

 }

 

Once you build this, place the .jar file into EI-HOME/lib and restart the product. That’s the first part. The artifact you’ve created you’ll call from BPMN in order to read and initialize all the variables from myconfig.properties.

Open WSO2 Developer Studio and start creating new process.

Right after the START EVENT add new Service Task and make sure you’re calling the right package / class you’ve made in the previous step:

<serviceTask id="servicetask1" name="Get Config Stuff" activiti:class="com.yenlo.bps.config.ConfigApp"></serviceTask>


From there you can add your own workflow stuff. In this step system will execute the service task and that task will invoke your code and pull the variables. 
 As soon as you change the content of the properties file, new process instances will use updated values.

I hope this blog was helpful. If you have any questions regarding this blog, please ask them in the comment section below.

New Call-to-action

Care to share?
   
Picture of Dusan Dević
Published January 3, 2019

Dusan Dević

Dusan is a passionate full-stack developer with more then 6 years of experience, especially with web development. He is very interested in IoT and modern technology in general. From 2015 he works as Integration Consultant at Yenlo, mostly with WSO2-IS, WSO2-DAS, Java and AngularJS.

Responses

Stay up to date with the latest articles