Table of Contents

Web service invocation with Flowable java service task!!!!

We will see how to invoke SOAP Web service with Floawble java service task. You should practice the previous tutorial to be clear with the java service task. We use the NumberToWord operation from NumberConvertion SOAP Web service to translate numbers to words (http://www.dataaccess.com/webservicesserver/NumberConversion.wso)

Create an external Java class as a jar file

  1. In Eclipse, create a new Maven Project.
    1. Check Create a simple project→Next
    2. Add Group Id and Artifact Id: org.flowable → Finish
  2. In your project, edit the file pom.xml by adding:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>org.flowable</groupId>
      <artifactId>org.flowable</artifactId>
      <version>0.0.1-SNAPSHOT</version>
       <dependencies>
       <dependency>
        <groupId>org.flowable</groupId>
        <artifactId>flowable-engine</artifactId>
        <version>6.7.2</version>
        <scope>provided</scope>
    </dependency>
     
    <dependency>
        <groupId>com.sun.xml.messaging.saaj</groupId>
        <artifactId>saaj-impl</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>jakarta.xml.soap</groupId>
        <artifactId>jakarta.xml.soap-api</artifactId>
        <version>3.0.2</version>
    </dependency>
    <dependency>
        <groupId>com.sun.activation</groupId>
        <artifactId>jakarta.activation</artifactId>
        <version>2.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.jvnet.staxex</groupId>
        <artifactId>stax-ex</artifactId>
        <version>2.1.0</version>
    </dependency>
                </dependencies>
     
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.5.2</version>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>shade</goal>
                </goals>
                <configuration>
                  <createDependencyReducedPom>false</createDependencyReducedPom>
                  <artifactSet>
                    <includes>
                      <include>jakarta.xml.soap:jakarta.xml.soap-api</include>
                      <include>com.sun.xml.messaging.saaj:saaj-impl</include>
                      <include>com.sun.activation:jakarta.activation</include>
                      <include>org.jvnet.staxex:stax-ex</include>
                    </includes>
                  </artifactSet>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>
  3. Create a package org.flowable.
  4. In the package org.flowable, create a class MySOAPDelegate with the following code:

    package org.flowable;
     
    import org.flowable.engine.delegate.DelegateExecution;
    import org.flowable.engine.delegate.JavaDelegate;
    import org.w3c.dom.NodeList;
     
    import jakarta.xml.soap.MessageFactory;
    import jakarta.xml.soap.SOAPBody;
    import jakarta.xml.soap.SOAPConnection;
    import jakarta.xml.soap.SOAPConnectionFactory;
    import jakarta.xml.soap.SOAPElement;
    import jakarta.xml.soap.SOAPEnvelope;
    import jakarta.xml.soap.SOAPException;
    import jakarta.xml.soap.SOAPMessage;
    import jakarta.xml.soap.SOAPPart;
     
    public class MySOAPDelegate implements JavaDelegate {
     
    	public static boolean isNumeric(String strNum) {
    		if (strNum == null) {
    			return false;
    		}
    		try {
    			Double.parseDouble(strNum);
    		} catch (NumberFormatException nfe) {
    			return false;
    		}
    		return true;
    	}
     
    	public void execute(DelegateExecution execution) {
     
    		String var = (String) execution.getVariable("input");
    		String result = "";
     
    		if (!isNumeric(var)) {
    			result = "Invalid Number";
    		} else {
    			MessageFactory messageFactory;
    			try {
    				messageFactory = MessageFactory.newInstance();
     
    				SOAPMessage soapMessage = messageFactory.createMessage();
    				SOAPPart soapPart = soapMessage.getSOAPPart();
     
    				String serverURI = "http://www.dataaccess.com/webservicesserver/";
     
    				// SOAP Envelope
    				SOAPEnvelope envelope = soapPart.getEnvelope();
    				envelope.addNamespaceDeclaration("ex", serverURI);
     
    				// SOAP Body
    				SOAPBody soapBody = envelope.getBody();
    				SOAPElement soapBodyElem = soapBody.addChildElement("NumberToWords", "ex");
    				SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("ubiNum", "ex");
    				soapBodyElem1.addTextNode(var);
     
    				soapMessage.saveChanges();
     
    				// Create SOAP Connection
    				SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
    				SOAPConnection soapConnection = soapConnectionFactory.createConnection();
     
    				// Send SOAP Message to SOAP Server
    				String url = "https://www.dataaccess.com/webservicesserver/NumberConversion.wso";
    				SOAPMessage soapResponse = soapConnection.call(soapMessage, url);
     
    				NodeList nodeList = soapResponse.getSOAPBody().getElementsByTagName("m:NumberToWordsResult");
    				String test = soapResponse.getSOAPBody().getTextContent();
    				if (nodeList.getLength() > 0)
    					result = nodeList.item(0).getTextContent();
    				else
    					result = "Invalid Number";
     
    				soapConnection.close();
    			} catch (SOAPException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
     
    		execution.setVariable("input", result);
     
    	}
    }
  5. Right click on your project and select Run As→Maven Install to build a jar file.
  6. Copy a jar file in the folder target to TOMCAT_HOME\webapps\flowable-ui\WEB-INF\lib
  7. Restart your tomcat.

Design the "Get Number to Word" process model

  1. In Activiti, login as admin with admin/test
  2. Go to Modeler App→Processes→Create Process.
  3. Enter new model Model name: Get Number to Word, Model key:get_number_to_word. Click Create new model.
  4. Design a process model by drag and drop model elements, you should have something similar to this picture:

    ?1000
  5. On the start event, specify the following properties:
    1. Initiator: initiator
    2. Form reference→New form, with Form name: Initial number, and Form key: initial_number, then click on Create form.
      1. Design the form by drag and drop the component, by add new Text, then on click on edit icon after hover on, and update the Label: Input, then click Close.
      2. Click on the Save icon, then Save and close editor.
  6. On the Process data activity, specify the property Class: org.flowable.MySOAPDelegate
  7. On the Result activity, specify the following properties:
    1. Assignments→Assignment: Assigned to process initiator
    2. Form reference→New form, with Form name: Output result, and Form key: output_result, then click on Create form.
      1. Add Text, then change the label: Output, check Override id?, then id: input.
  8. On the Reply activity, specify the following properties:
    1. Assignments→Assignment: Assigned to process initiator
    2. Form reference then select the initial_number form, then click on Save.
  9. On the exclusive gateway, make sure that the property Exclusive is checked.
  10. On the sequence flow from the exclusive gateway to Result, specify the property Flow condition: ${input != 'Invalid Number'}
  11. On the sequence flow from the exclusive gateway to Retry, specify the property Flow condition: ${input == 'Invalid Number'}
  12. Click on Save the model icon, then click on Save and close editor.
  13. In Apps→Create App (or select the exiting one then click on App Editor):
    1. App definition name:Service connection
    2. App definition key:service_connection
    3. Then Create new app definition
  14. Clik on Edit included models, select your Displaying Upper Case process model, then go to Save and check published, and finally Save and close editor.

Executing a process model

  1. Go to Service connection→Processes→Start a process, select your newly created process model.
  2. Select Get Number to word.
  3. Enter a number in the Input (for example, 78), then click on Start process.
  4. Go to Processes→Inbox to observe the status of your executing process. The current task should be Result if you entered 78.


  5. Select Tasks→Inbox to see the result. It should be seventy eight if you entered 78 as input. Then click the Complete task button.


  6. Try to start the process again with an invalid number (for example, abc). Observe the status of your executing process. The current task should be Retry


  7. Select Tasks→Inbox to reenter the input number. Enter an input then click the Complete task. If you entered an invalid input again, you will need to reenter it until it's valid.

Exercices

  1. Can you invoke the sayHello operation from WS_HelloWorld service from first web service tutorial
  2. Create a process model with a java service task to access your REST resource. Please do these steps before designing your process model:
    1. Suppose that we have already done the tutorial about Getting a target resource based on the client request. Open the class UserResource.java file. Modify the code as following:

      package forthREST;
       
      import org.restlet.resource.Get;
      import org.restlet.resource.ServerResource;
       
      public class UserResource extends ServerResource {  	
      	@Get  
      	public String toString() {
      		String uid = (String) getRequestAttributes().get("uid");
      		if (uid.equals("123")){
      			return "<users>" +
      					"<user>" +
      					"<id>"+uid+"</id>" +
      					"<name>John</name>" +
      					"</user>" +
      					"</users>";
      		}
      		return "No information found.";  
       
      	}  
      }
    2. Run the modified REST service by right clicking on the RESTDistributor.java file, select Run As → Java Application.
    3. Open http://localhost:8182/users/123 by your web browser. You should get the following:

      <users><user><id>123</id><name>John</name></user></users>
    4. Create a new Maven Project for an external Java class to access your REST resource, build it as a jar file and copy it to TOMCAT_HOME\webapps\flowable-ui\WEB-INF\lib.
    5. Restart your tomcat.