Web service invocation with Activiti java service task!!!!

We will see how to invoke SOAP Web service with Activiti 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.
  2. In your project, edit the file pom.xml by adding:

        <dependency>
          <groupId>org.activiti</groupId>
          <artifactId>activiti-engine</artifactId>
          <version>5.22.0</version>
        </dependency>
  3. Create a package org.activiti.
  4. In the package org.activiti, create a class MySOAPDelegate with the following code:

    package org.activiti;
     
    import javax.xml.soap.MessageFactory;
    import javax.xml.soap.MimeHeaders;
    import javax.xml.soap.SOAPBody;
    import javax.xml.soap.SOAPConnection;
    import javax.xml.soap.SOAPConnectionFactory;
    import javax.xml.soap.SOAPElement;
    import javax.xml.soap.SOAPEnvelope;
    import javax.xml.soap.SOAPMessage;
    import javax.xml.soap.SOAPPart;
     
    import org.activiti.engine.delegate.DelegateExecution;
    import org.activiti.engine.delegate.JavaDelegate;
    import org.w3c.dom.NodeList;
     
    public class MySOAPDelegate implements JavaDelegate {
     
    	public static boolean isNumeric(String strNum) {
    	    if (strNum == null) {
    	        return false;
    	    }
    	    try {
    	        double d = Double.parseDouble(strNum);
    	    } catch (NumberFormatException nfe) {
    	        return false;
    	    }
    	    return true;
    	}
     
    	public void execute(DelegateExecution execution) throws Exception {
     
    		String var = (String) execution.getVariable("input");
    		String result = "";
     
    		if(!isNumeric(var)){
    			result = "Invalid Number";			
    		}
    		else{
    			MessageFactory 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 = "http://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();			
    		}
     
    		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 apache-tomcat-7.0.12\webapps\activiti-explorer\WEB-INF\lib
  7. Restart your tomcat.

Design the "Get Number to Word" process model

  1. In Activiti, login as admin with kermit/kermit
  2. Go to Processes→Model workspace→New model.
  3. Enter new model Name: Get Number to Word. Click Create.
  4. Firstly, enter the following process properties:
    1. Process identifier: getNumberToWord
    2. Name: Get Number to Word
  5. Design a process model by drag and drop model elements, you should have something similar to this picture:

    ?1000
  6. On the start event, specify the following properties:
    1. Initiator: initiator
    2. Form properties:
      1. id: input, Name: Number, Type: string
  7. On the Process data activity, specify the property Class: org.activiti.MySOAPDelegate
  8. On the Result activity, specify the following properties:
    1. Assignments→Assignee: ${initiator}
    2. Form properties:
      1. id: input, Name: Result, Type: string
  9. On the Retry activity, specify the following properties:
    1. Assignments→Assignee: ${initiator}
    2. Documentation: Invalid input, please reenter.
    3. Form properties:
      1. id: input, Name: Number, Type: string
  10. On the exclusive gateway, make sure that the property Exclusive is checked.
  11. On the sequence flow from the exclusive gateway to Result, specify the property Flow condition: ${input != 'Invalid Number'}
  12. On the sequence flow from the exclusive gateway to Retry, specify the property Flow condition: ${input == 'Invalid Number'}
  13. Click on Save the model icon, then click on Save and close editor.
  14. In Processes→Model workspace, selected your Get Number to Word process model, then go to Model action and select Deploy.

Executing a process model

  1. Go to Processes→Deployed process definitions, select your Get Number to Word process model, then click on Start process.
  2. Fill the input number (for example, 78), then click Start process.
  3. Go to Processes→My instances to observe the status of your executing process. The current task should be Result if you entered 78.


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


  5. 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


  6. 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 apache-tomcat-7.0.12\webapps\activiti-explorer\WEB-INF\lib.
    5. Restart your tomcat.
teaching_assistant/workflow/activiti_soap.txt · Last modified: 2021/01/20 09:03 by IT Courses (NNC)
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0