Creating Workflow in Compute Grid

  GigaSpaces 5.X

Documentation Home
Quick Start Guide
Release Notes

Previous release

  Search Here
Searching GigaSpaces Platform 5.X Documentation

                                               

Summary: Demonstrates a simple application which uses a Space Based Architecture (SBA), to process a workflow.

Workflow Example

This example demonstrates a simple application which uses a Space Based Architecture (SBA), to process a workflow. The workflow is determined by setting states in an Entry. The workflow itself is based on an enumerated Java class, with states of NEW, OPEN, PENDING and CLOSED. Generic worker instances are configured to process Entry types that match a specific template corresponding to a workflow state. This document is organized as follows:

This example will provide an introduction to the semantics possible with a SBA, and can be extended as needed.

Example Overview

The example described in this section can be found in the <GigaSpaces ROOT>\ServiceGrid\examples\workflow folder. The Workflow example architecture is depicted below:

+--------+                          +-----------+
|        |-- writes  -->            |           |                                    +--------+
|        |     OrderWorkFlow.NEW    |           | --  take OrderWorkFlow.NEW -->     |  New   |
|        |                          |           | <-- write OrderWorkFlow.PENDING -- | Worker |
|        |                          |           |                                    +--------+
|        |                          |           |                                    +--------+
| Master |                          | GigaSpace | --  take OrderWorkFlow.PENDING --> | Pending|
|        |                          |           | <-- write OrderWorkFlow.OPEN --    | Worker |
|        |                          |           |                                    +--------+
|        |                          |           |                                    +--------+
|        |                          |           | -- take OrderWorkFlow.OPEN -->     |  Open  |
|        |                          |           | <-- write OrderWorkFlow.CLOSED --  | Worker |
|        |<-- takes                 |           |                                    +--------+
|        |     OrderWorkFlow.CLOSED |           | 
+--------+                          +-----------+

The Master is contained in the source file example.OrderMaster, and uses the example.OrderEntry to write and take Entries to or from the space.

  • The "New Worker" is configured to process the example.OrderEntry with an example.OrderWorkFlowEnum set to example.OrderWorkFlow.NEW.
  • The "Pending Worker" is configured to process the example.OrderEntry with an example.OrderWorkFlow set to example.OrderWorkFlow.PENDING.
  • The "Open Worker" is configured to process the example.OrderEntry with an example.OrderWorkFlow set to example.OrderWorkFlow.OPEN.
  • The "Closed Worker" is configured to process the example.OrderEntry with an example.OrderWorkFlow set to example.OrderWorkFlow.CLOSED.
  • Each worker is configured in the deployment descriptor to process the correct Entry using the following configuration (only 2 are shown, but the pattern is obvious):

New Worker:

<Configuration>
    <Component Name="com.gigaspaces.grid.worker">
        <Parameter Name="temmplateClass"
                     Value="new example.OrderEntry(example.OrderWorkFlow.NEW)"/>
     </Component>
</Configuration>

Pending Checked Worker:

<Configuration>
    <Component Name="com.gigaspaces.grid.worker">
         <Parameter Name="temmplateClass"
                     Value="new example.OrderEntry(example.OrderWorkFlow.PENDING)"/>
    </Component>
</Configuration>

Each worker additionally has an association to the gigaspaces.xml deployment descriptor that is obtained from the global Association declaration.

Example Source Files

Source code is located in the <product-install-root>/ServiceGrid/examples/workflow directory.

File Name Location Description
OrderCLI.java /src/example Provides an interactive command line interface for running the example.
OrderEntry.java /src/example The OrderEntry which gets processed by the com.gigaspaces.grid.worker.GigaSpaceStationImpl class, gets its status changed. Once its status is OrderWorkFlow.CLOSED, it generates a random price.
OrderWorkFlow.java /src/example Enumerated workflow states.  
OrderMaster.java /src/example An example of a master (in the master-worker pattern) which writes and reads tasks, in this case writes an OrderEntry and takes the OrderEntry.
workflow.xml /src/config The deployment descriptor for the example, contains the declaration of the 4 Worker services.

Building the Example

Building the example is straight forward, you must have Ant and JDK 1.5. If these requirements are met, do the following:

$ cd <product-install-root>/ServiceGrid/examples/workflow
$ ant
The build process creates an executable jar file which contains classpath manifest settings, which load required jars specific to the machine you have build the example on. This is provided for ease of use.

Running the Example

The example below works with Java 5.0. It will not work with Java 1.4.

How to run the example:

  1. Start the Service Grid:
    $ cd <product-install-root>/ServiceGrid/bin
    $ gs-all
  2. Deploy the example:
    • Using the Service Grid CLI:
      $ cd <product-install-root>/ServiceGrid/bin
      $ gs deploy ../examples/workflow/config/workflow.xml
    • Using the Service Grid UI :
      $ cd <product-install-root>/ServiceGrid/bin
      $ gs-ui

    1. Go to the deployment wizard, select An application, as defined by a deployment descriptor, press Next.
    2. Press the Browse... button for the Select Deployment Descriptor text field.
    3. Traverse to the ServiceGrid/examples/workflow/config directory.
    4. Select workflow.xml.
    5. Press Next...
    6. Press Deploy.
    7. Click the Deploy button to start the deployment process.
      Once the Service has been deployed successfully the Deployment Wizard will display the following:
    8. The Service Grid Admin UI displays the deployment tree of the services and their dependencies.
  3. Run the client:
    $ java -jar lib/workflow.jar 
    Using Lookup argument : jini://*//*/GigaSpace?groups=gigaspaces-5.1EE
    Proceed? [y]/n/q 
    Discovering compute grid ...
    Submitting Order ...
    Waiting for result ...
    Order result is : $6891
    Submit another ? [y]/n/q? 
    Submitting Order ...
    Waiting for result ...
    Order result is : $5266
    Submit another ? [y]/n/q? 
    Submitting Order ...
    Waiting for result ...
    Order result is : $2305
    Submit another ? [y]/n/q? 
    Submitting Order ...
    Waiting for result ...
    Order result is : $43
    Submit another ? [y]/n/q? q
    $

Custom Worker

You might want to build a custom worker that consumes the tasks and executes these.

  • In the example below, a POJO-based worker and its Deployment Descriptor are deployed as a service.
  • The service interface as part of the Deployment Descriptor is org.jini.rio.resources.servicecore.Service, and there is no need for the worker class to implement an interface.
  • Enhanced versions of such a worker might use transactions to provide a higher level of consistency.
  • The MyOrderWorkFlow.setSpace() method will be called by the Service Grid once the Worker service is deployed, injecting the space proxy. This is implemented via the required Association Type, as part of the Deployment Descriptor.

Custom Worker - Worker Code

package example;
import net.jini.space.*;
import com.j_spaces.core.*;

public class MyOrderWorkFlow  {
	static IJSpace space = null;
	public MyOrderWorkFlow  (){}
	public void setSpace(IJSpace space)
	{
		this.space = space;
		System.out.println("space:" + space.getName());
		new Thread (new myWorker()).start();
	}

	public static class myWorker implements Runnable
	{
	public void run()
		{
			while (true)
			{
			try{
				System.out.println(" ------ >> Got " + space.count(null ,null) + "
				 entries in the space");

				System.out.println(" ------ >> Calling Take....");

				OrderEntry order = (OrderEntry)space.take (new OrderEntry() , null , 10000);
				if (order!= null)
				{
					System.out.println("Got" + order);
					space.write(order.execute(null), null , 10000);
				}
			}
			catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		}
	}
}

Custom Worker - Deployment Descriptor

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE gs-deployment SYSTEM "java://gs-deploy-desc.dtd" >

<gs-deployment Name="Workflow Example">
    <!-- Declare global attributes for the Deployment Descriptor  -->
    <Codebase Adaptive="yes">$[com.gigaspaces.system.codeserver]</Codebase>

    <Groups>
        <Group>${com.gs.jini_lus.groups}</Group>
    </Groups>

    <!-- Parameters that are applicable for all workers -->
    <Parameters>
        <Parameter Name="useWeakHashMap" Value="no"/>
        <Parameter Name="useTransactions" Value="no"/>
        <Parameter Name="timeout" Value="5000"/>
    </Parameters>

    <Association Type="requires" Name="GigaSpace" refid="space.desc"/>

    <ServiceBean Name="MyWorker">
        <Interfaces>
            <Interface>org.jini.rio.resources.servicecore.Service</Interface>
            <Resources>
                <JAR>gs-dl.jar</JAR>
            </Resources>
        </Interfaces>

        <ImplementationClass>example.MyOrderWorkFlow
            <Resources>
                <SharedComponent>
                    <ClassName>example.OrderEntry</ClassName>
                    <JAR>examples/workflow/lib/workflow.jar</JAR>
                </SharedComponent>
            </Resources>
        </ImplementationClass>

		<Association Type="requires" Property="space" Name="GigaSpace" refid="space.desc"/>
        <Maintain>1</Maintain>


    </ServiceBean>

    <Include id="space.desc">
        ${com.gigaspaces.grid.home}config/deployment/gigaspace.xml</Include>

</gs-deployment>

Wiki Content Tree


Your Feedback Needed!

We need your help to improve this wiki site. If you have any suggestions or corrections, write to us at techw@gigaspaces.com. Please provide a link to the wiki page you are referring to.

Labels