Parallel Processing Tutorial - Order Management

  Search Here
Searching XAP 6.0 Documentation

                                               

Summary: This tutorial shows how to implement a Master-Worker pattern, using OpenSpaces processing units, in order to perform parallel processing.

This page is specific to:
GigaSpaces 6.0

If you're interested in another version, click it below:
GigaSpaces 5.x
GigaSpaces 6.5

Overview

This tutorial assumes knowledge of JavaSpaces terms like the space, Entries and leases. If you're not familiar with these concepts, start with the previous tutorial, Plain JavaSpaces.

In this tutorial you will use OpenSpaces to implement a simple Order Management System. The tutorial illustrates how JavaSpaces can be used for parallel processing – execution of business logic by a number of autonomous software instances.

To do this, you will use the master-worker pattern, in which a master creates a set of tasks, puts them in a shared space, and waits for the tasks to be picked up and completed by a number of workers. The main advantages of this pattern are that the master and workers operate independently of each other (saving the need for complex coordination), and that the load is automatically balanced between the workers sharing the tasks.

Using JavaSpaces with OpenSpaces

JavaSpaces is especially suitable for implementing a master-worker pattern: the space can serve as a shared work area for numerous masters and workers, which can work together without knowing each other and without requiring central coordination.

However, plain JavaSpaces, as demonstrated in the previous tutorial, is too limited for a real-world master-worker implementation. OpenSpaces offers a powerful yet simple-to-use framework that overcomes the practical limitations of JavaSpaces.

You will learn how to use OpenSpaces to find the space more easily; and to allow each component to work under a local transaction, without requiring a transaction coordinator.

In this tutorial you will construct your applications as simple OpenSpaces Processing Units, which enable the packaging and deployment of application services. In the next tutorials you will learn how to gain the full benefits of the Processing Unit concept implementing Space-Based Architecture (SBA).

The Processing Unit is configured using Spring with deploy/runtime context extensions, such as bean-level properties. This allows you to use the same Processing Unit deployment with no changes, and deploy it within different environments (pre-prod, prod, etc.).

Application Components

You will build a Master-Worker application, in which the Master is a client (or multiple clients) that submits orders to the space, and takes them back from the space after they are processed by the workers.

There are two types of workers:

  • A Validator, which takes orders from the space and either approves or rejects them.
  • A Processor, which takes approved orders and performs a simple mathematical operation on them.



Application Workflow

The basic workflow, as it is represented by Order objects in the space:

  1. One or more clients continuously write new Order objects to the space; each Order is tagged with a client ID.
  2. If one or more Validators are running, they take new orders from the space.
  3. The Validators check the orders, mark them either as approved or rejected, and write them back to the space.
  4. If one or more Processors are running, they take any approved orders (which have gone through a Validator).
  5. The Processors work on the orders, and write back Processed order objects to the space.
  6. Each of the Clients routinely checks to see if there are any Processed orders marked with its unique ID. If so, it takes the results from the space.
  7. Each of the Clients is also registered for notifications received whenever a Rejected order is written to the space, marked with its unique ID. If so, it takes the results from the space.

Notice that in this workflow, no component is dependent on any of the others. Clients write new orders to the space without caring how many Validators or Processors are running and which orders they have processed. They simply watch the space, and take processed orders using a polling mechanism or rejected orders using a notification mechanism when they appear. The same goes for the Validators and Processors – they simply watch the space for new orders to process or validate, and if they find any, perform the required operation and write the result back to the space.

Tutorial Map

Tutorial Sections:

  • Overview – tutorial overview.
  • Tutorial Map – the sections in this page.
  • WorkFlow Animation – 50 second flash animation displaying the workflow.
  • Code – full code review of the entire application.
  • Build And Compile – how to prepare the application for running.
  • Deployment – how to deploy and run the application.
  • Running Within The IDE – how to run the application within Eclipse IDE.
  • Download – full downloadable package with source code and compiling/running scripts.
  • What's next – next tracks, performance boost, durability and scalability with SBA.


WorkFlow Animation


Writing The Code

Domain Model POJO Services and Spring Wiring

The following tabs describe the Domain Model and the POJO service beans in each of the Processing Units, as well as the Processing Units' configuration files, which deifne their structure:

Build And Compile

Once everything is ready, we need to build, package and deploy the application.

The following JAR files need to be in your classpath, all of which reside in the <GigaSpaces Root>\lib directory or its subfolders:

  • JSpaces.jar
  • openspaces/openspaces.jar
  • spring/spring.jar
  • jini/jsk-platform.jar
  • jini/jsk-lib.jar

In order to deploy the application, we need to deploy each of the three Processing Units separately. To do this, every Processing Unit must be placed in the <GigaSpaces Root>\deploy directory. Every Processing Unit is actually a folder (whose name is the name of the Processing Unit later used for deployment) with several subfolders. Here's a typical Processing Unit directory as it resides under the <GigaSpaces Root>\deploy directory:

As the image shows, under the deploy folder of the product, we've put the pp-oms-client folder, which is in fact the application's Client Processing Unit. The folder name states the name of the Processing Unit.

Under META-INF\spring is our pu.xml file, relevant for the feeder Processing Unit. Also, directly under the deploy folder is our compiled code, with its appropriate package structure (the com folder in the image above).

Finally, the shared-lib folder includes all libraries that are used globally by this Processing Unit and potentially other Processing Units. In this case, shared-lib holds a JAR file that includes our domain model object. The same JAR is placed under the shared-lib folder of each of the Processing Units in our application.

To summarize the building and packaging process:

  1. Compile your code.
  2. Create a common JAR file for your shared libraries.
  3. Create a folder of the Processing Unit that complies with the structure shown in the image, and copy all the necessary files into it accordingly. Give the folder any name you want for this Processing Unit.
  4. Copy the created folder into the <GigaSpaces Root>\deploy directory.
  5. Repeat for every Processing Unit in the application.

The full example package also includes two prepared automated ways to build and compile the application:

Using supplied batch scripts for Windows/Unix:

  1. Run <Example Folder>\bin\compile.bat/.sh – compiles the modules and arranges their structure for deployment.
  2. Run <Example Folder>\bin\copy_to_deploy_folder.bat/.sh – copies the ready-to-deploy modules to the <GigaSpaces Root>\deploy folder.

Using supplied build.xml file and ant:

  1. Running build compiles all the different modules. In the case of the Client, Validator and Processor modules, build compiles the classes directly into their respective Processing Unit structure.
  2. Running dist finalizes the structure of all Processing Units by copying the common module JAR into the shared-lib directory in the Processing Unit structure.
    For example, in the client module, dist copies the JAR to client/pu/pp-oms-client/shared-lib, and makes client/pu/pp-oms-client a ready-to-use Processing Unit.
  3. Running copy-local-client, copy-local-validator and copy-local-processor copies the finalized Processing Units to the <GigaSpaces Root>\deploy folder, ready to be deployed into the Service Grid.

Deployment

You are now ready to deploy your application, but first, a short recap:

Our Order Management System application has 3 Processing Units: Client, Validator and Processor, and an Enterprise Data Grid that contains a single space instance. The domain model includes the Order object.

There are several ways to deploy the application and to run a Processing Unit. A Processing Unit can either run in standalone mode in your IDE (for development and testing purposes), or on top of the Service Grid, in SLA-driven containers that are called GSCs (Grid Service Containers). In this section, we'll show the latter approach, which is used in production environments.

Because we want to deploy to the Service Grid, we first need to start it. Running the Service Grid is as easy as running one GSM (Grid Service Manager) from <GigaSpaces Root\bin, and several GSCs, on top of which we run our Processing Units. The deployed application looks like this:



Though the image above shows the Client Processing Unit and the space in one container and the Validator and Processor Processing Units in a second container; they might be arranged differently, according to the deployment order. For example, the Validator and Client might be in one SLA container, while the other encapsulates the Processor and the space. We can run more SLA containers (GSCs) and have each component inside its own SLA container.

Starting Service Grid Components

  1. To start the GSM, execute:
    <GigaSpaces Root>\bin\gsm.(sh/bat)
  2. Run two GSCs by executing the following twice:
    <GigaSpaces Root>\bin\gsc.(sh/bat)
  3. Start the GigaSpaces Management Center:
    <GigaSpaces Root>\bin\gs-ui.(sh/bat)
  4. Click the left Deployments tab.



  5. The two running GSCs are displayed in the Details area. Both of them are still empty, because no Processing Units or space instances have been deployed.

    EDG Space Instance Deployment

  6. After selecting the Deployments tab, click the Deploy Application button ( ) to open the Deployment Wizard.
  7. In the Deployment Wizard, select the Enterprise Data Grid option and click Next:



  8. Fill the Data Grid Name attribute with spacePP and choose blank for the Cluster schema attribute as shown below:



  9. Click Deploy to deploy the EDG, the following screen should appear:



  10. After the deployment is successful, click Close.



  11. Now, have another look at the lower side of the Deployments tab. You should see that one of the GSCs contains the EDG space instances you just deployed.



Deploying the Processing Units

  1. To deploy the Processing Unit, click the deploy button ( ) – the Deployment Wizard opens:



  2. In the first page of the wizard (screen above), click the SBA Application - Processing Unit radio button, and click Next to see the deployment dialog:



  3. In the Processing Unit name field, select the name of the Processing Unit. This name is the same as the name of the Processing Unit directory, located under the <GigaSpaces Root>\deploy directory.

    For example, if you copied the Client Processing Unit folder under the deploy directory with the name pp-oms-client, select pp-oms-client in the Processing Unit Name field.
    (When using the scripts supplied in the example, the Processing units' folders are named pp-oms-client, pp-oms-validator and pp-oms-processor).



  4. Click Deploy. the following screen should appear showing the deployment status:



  5. Wait until the Processing Unit successfully finishes deploying, then click Close.
  6. Now, have another look at the lower side of the Deployments tab. You should see that one of the GSCs contains the Processing Unit you just deployed.



  7. Repeat the deployment process twice more for the other Processing Units (remember, you are deploying the Client, Validator and Processor Processing Units).
    At the bottom of the Deployments tab, you should now see:



Expected Output

  1. In the GSC command windows, you can see something like the following as your output lines.
    • For the Client:
      .
      .
      CLIENT wrote orderEvent: userName[USER678267834] status[New]
      .
      .
      CLIENT took Processed OrderEvent , Total rejected taken [772], Total processed taken [765]
      .
      .
      CLIENT took Rejected OrderEvent , Total rejected taken [773], Total processed taken [765]
      .
      .
      
    • For the Validator:
      .
      .
      VALIDATOR validating orderEvent:userName[USER1761393384] status[New]
      VALIDATOR set orderEvent status to: Approved
      .
      .
      VALIDATOR validating orderEvent:userName[USER678267834] status[New]
      VALIDATOR set orderEvent status to: Rejected
      .
      .
      
    • For the Prcessor:
      .
      .
      PROCESSOR: Processing orderEvent:userName[USER1761393384] status[Approved]
      .
      .
      

Running Within Eclipse IDE

You can execute the Processing Units inside your IDE. This enables you to easily modify and test your applications. To do this, we use the OpenSpaces Integrated Processing Unit Container:

  1. Deploy an EDG space instance named spacePP, see instructions.

    In the next tutorials, when we learn how to use the Processing Unit concept to implement SBA (Space-Based Architecture), we use Processing Units running embedded spaces, instead of deploying an external space instance.

  2. If you haven't done so yet, import your project to eclipse (includes defining an Eclipse workspace variable called GS_HOME, pointing to your <GigaSpaces Root> directory, since all the project JARs are referenced with this variable).
  3. Choose one of the 3 Processing Unit projects: client, validator, or processor from the package navigator:



  4. Click Run > Open Run Dialog to open the Create, manage, and run configurations dialog.
  5. Create New launch configuration for Java Application:



  6. Name the new configuration and type org.openspaces.pu.container.integrated.IntegratedProcessingUnitContainer in the Main class field:



  7. Click Run to run the chosen Processing Unit inside the integrated container.
  8. Repeat these steps for the other two Processing Units.

How this Works

The org.openspaces.pu.container.integrated.IntegratedProcessingUnitContainer class (available inside the OpenSpaces JAR lib folder) contains a main method that when executed, searches the project for the directory META-INF\spring that contains the Processing Unit configuration file – pu.xml, and then loads the Processing Unit.

Download Full Example Package


The full Java tutorial example package including execution scripts:

Download the zip file

Extract this directory into your <GigaSpaces Root> directory.
After unzipping find the package for this tutorial at <GigaSpaces Root>\examples\Tutorials\Parallel Processing\PP_OrderManagement.

How to import the project to Eclipse IDE after downloading

Full Package Directory Structure

After downloading and extracting, your example folder should look something like this:

PP_Order_Management – tutorial folder
client – Client Processing Unit project
src
com\gigaspaces\examples\tutorials\parallelprocessing\client – Client service beans Java source files.
OrderEventFeeder.java – POJO service bean that feeds orderEvent objects to the space
OrderEventCounterDisplayer.java – POJO service bean that counts orderEvents and displays their information
META-INF\spring
pu.xml – Client Processing Unit configuration XML file
validator – Validator Processing Unit project
src
com\gigaspaces\examples\tutorials\parallelprocessing\validator – Validator service beans Java source files
OrderEventValidator.java – POJO service bean that validates the orderEvents
META-INF\spring
pu.xml – Validator Processing Unit configuration XML file
processor – Processor Processing Unit project
src
com\gigaspaces\examples\tutorials\omsgs\processor – Processor service beans Java source files
OrderEventProcessor.java – POJO service bean that processes the orderEvents
META-INF\spring
pu.xml – Processor Processing Unit configuration XML file
common – domain model used by all the Processing Units
src\com\gigaspaces\examples\tutorials\omsgs\common
OrderEvent.java
docs – expected output files for the 3 Processing Units
expected_output_pp-oms-client.txt
expected_output_pp-oms-validator.txt
expected_output_pp-oms-processor.txt
bin - build and compile scripts.
compile.bat/.sh – a script that compilies and builds the 3 Processing Units (Windows/Unix)
copy_to_deploy_folder.bat/.sh – a script that copies the compiled Processing Units to the GigaSpaces deploy folder
setExampleEnv.bat/.sh – sets the environment variables for the example scripts
build.xml – XML file used with Ant to compile/build/prepare for deployment
readme.txt

Importing Project to Eclipse IDE

  1. If you haven't done so yet, first download GigaSpaces XAP and the example package.
  2. Start Eclipse, using import existing project. Import the 4 sub-projects:
    parallelprocessing-client, parallelprocessing-validator, parallelprocessing-processor and parallelprocessing-common
    from <GigaSpaces Root>\examples\tutorials\Parallel_Processing\PP_OrderManagment to your workspace.
  3. Add a variable to the Eclipse workspace named GS_HOME pointing to your <GigaSpaces Root> directory.


What's Next?

Try Another Tutorial
GigaSpaces XAP Help Portal
GigaSpaces EDG Help Portal

Further Reading


GigaSpaces 6.0 Documentation Contents (Current Page in Bold)

    Java