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 GetGeoIP operation from GeoIPService SOAP Web service (http://www.webservicex.net/geoipservice.asmx) to look up countries by IP addresses.
Create an external Java class as a jar file
In Eclipse, create a new Maven Project.
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>
Create a package org.activiti.
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 void execute(DelegateExecution execution) throws Exception {
String var = (String) execution.getVariable("ip");
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://www.webservicex.net/";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("example", serverURI);
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("GetGeoIP", "example");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("IPAddress", "example");
soapBodyElem1.addTextNode(var);
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI + "GetGeoIP");
soapMessage.saveChanges();
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
String url = "http://www.webservicex.net/geoipservice.asmx";
SOAPMessage soapResponse = soapConnection.call(soapMessage, url);
String result = "";
NodeList nodeList = soapResponse.getSOAPBody().getElementsByTagName("CountryName");
if (nodeList.getLength() > 0)
result = nodeList.item(0).getTextContent();
else
result = "Invalid IP address";
soapConnection.close();
execution.setVariable("result", result);
}
}
Right click on your project and select Run As→Maven Install to build a jar file.
Copy a jar file in the folder target to apache-tomcat-7.0.12\webapps\activiti-explorer\WEB-INF\lib
Restart your tomcat.
Design the "Get IP Country" process model
In Activiti, login as admin with kermit/kermit
Go to Processes→Model workspace→New model.
Enter new model Name: Get IP Country. Click Create.
Firstly, enter the following process properties:
Process identifier: getIPCountry
Name: Get IP Country
Design a process model by drag and drop model elements, you should have something similar to this picture:
On the start event, specify the following properties:
Initiator: initiator
Form properties:
id: ip, Name: IP Address, Type: string
On the Get IP Country activity, specify the property Class: org.activiti.MySOAPDelegate
On the Result activity, specify the following properties:
Assignments→Assignee: ${initiator}
Form properties:
id: result, Name: Result, Type: string
On the Reenter IP activity, specify the following properties:
Assignments→Assignee: ${initiator}
Documentation: Invalid IP address, please reenter.
Form properties:
id: ip, Name: IP Address, Type: string
On the exclusive gateway, make sure that the property Exclusive is checked.
On the sequence flow from the exclusive gateway to Result, specify the property Flow condition: ${result != 'Invalid IP address'}
On the sequence flow from the exclusive gateway to Reenter IP, specify the property Flow condition: ${result == 'Invalid IP address'}
Click on Save the model icon, then click on Save and close editor.
In Processes→Model workspace, selected your Get IP Country process model, then go to Model action and select Deploy.
Executing a process model
Go to Processes→Deployed process definitions, select your Get IP Country process model, then click on Start process.
Fill the ip address (for example, 8.8.8.8), then click Start process.
Go to
Processes→My instances to observe the status of your executing process. The current task should be
Result if you entered 8.8.8.8 as an ip address.
Select
Tasks→Inbox to see the result. It should be United States if you entered 8.8.8.8 as an ip address. Then click the
Complete task button.
Try to start the process again with invalid ip address (for example, 999.999.999.999). Observe the status of your executing process. The current task should be
Reenter IP
Select
Tasks→Inbox to reenter the ip address. Enter an ip address then click the
Complete task. If you entered invalid ip address again, you will need to reenter it until it's valid.
Exercices
-
Create a process model with a java service task to access your REST resource. Please do these steps before designing your process model:
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.";
}
}
Run the modified REST service by right clicking on the RESTDistributor.java file, select Run As → Java Application.
-
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.
Restart your tomcat.
Back to top