Hello SBA

  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.

Hello SBA Example

This example illustrates a basic SBA application running an IWorker implementation within a deployed fault-tolerant partitioned space.

A feeder pushes data into the partitioned space, where the IWorker implementation is running in each active partition node, consuming the data.

When the GSC hosting the active partitions failes, the backup partitions take over moving their hosted workers into active mode.

To configure Space URL parameters using the gs.space.url.arg. prefix, refer to the Configuring Space URL Parameters section.

Installing the Example

Extract the example zip file under <GigaSpaces Root>/ServiceGrid/examples.

The example folder structure is detailed below:

\GigaSpaces ROOT\ServiceGrid\examples\helloSBA
+---bin	-> scripts to run
+---build
|   \---classes
|       \---com
|           \---gigaspaces
|               \---sba
+---config
|   +---deployment  -> the helloSBA-dd.xml deployment descriptor
|   +---overrrides
|   \---space-config
|       \---config
|           \---schemas  -> space and cluster schema
+---lib
\---src
    \---com
        \---gigaspaces
            \---sba -> worker , feeder and entry classes

Building the Example

To run the example, run the following script from the GigaSpaces Root\ServiceGrid\examples\helloSBA\bin folder:

E:\GigaSpacesEE5.2\ServiceGrid\examples\helloSBA\bin>build.cmd
Buildfile: ..\build.xml

prepare:

compile:
    [javac] Compiling 3 source files to E:\GigaSpacesEE5.2\ServiceGrid\examples\helloSBA\build\classes

prepare:

${example.name}.jar:
      [jar] Building jar: E:\GigaSpacesEE5.2\ServiceGrid\examples\helloSBA\lib\helloSBA.jar

jars:

install:
     [copy] Copying 1 file to E:\GigaSpacesEE5.2\ServiceGrid\deploy\examples\helloSBA

all:

BUILD SUCCESSFUL

This compiles the source, and generates the helloSBA.jar example.

Running the Example

  1. Run startAll – this starts one GSM and two GSCs.
  2. Run gs-deploy – this deploys the partitioned space with the IWorkers. Please wait until the GSM and GSCs start before you run the deploy utility.
  3. Once you deploy, run gs-ui. Here is what you should see:

  4. Run start-feeder – this is the client application that pushes data into the partitioned space.
  5. Once the feeder connects to the partitioned space, and writes data into it, you should see the following in the GSC console:
    02:57:23.62 - MyEntry[routingKey=0,str=str0,anInt=0]
    02:57:23.671 - MyEntry[routingKey=2,str=str2,anInt=6]
    02:57:24.78 - MyEntry[routingKey=4,str=str4,anInt=12]
    02:57:24.625 - MyEntry[routingKey=6,str=str6,anInt=18]
    02:57:25.31 - MyEntry[routingKey=8,str=str8,anInt=24]
    02:57:25.750 - MyEntry[routingKey=10,str=str10,anInt=30]
  6. The feeder output should include the following:
    Connect to jini://*/*/space?timeout=1000
    CONFIG: Sets the system property ${com.gs.home} with value: E:\GigaSpacesEE5.2\
    .
    .....................................

Testing Failover

  1. Keep the feeder running.
  2. Kill one of the GSCs and see if the feeder continues without an exception. At this stage, the remaining GSC should handle the load from the feeder.
  3. Start a new GSC – you should see backup spaces starting automatically on this GSC. The GSM knows that each partition need at least two instances up and running.
  4. Repeat step 2 and 3 several times to see how the recovery process works.

The IWorker Implementation

The MyWorker implements the com.j_spaces.worker.IWorker. For more details about implementing the space IWorker refer to the Space Worker section.

package com.gigaspaces.sba;

import java.text.SimpleDateFormat;
import java.util.Date;
import net.jini.core.lease.Lease;
import com.j_spaces.core.IJSpace;
import com.j_spaces.worker.IWorker;

public class MyWorker implements IWorker {
	private IJSpace space;

	SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss.S");

	public void init(IJSpace _space, String arg1, String arg2) throws Exception {
		space = _space;
	}

	public void close() {
	}

	public void run() {
		MyEntry template = new MyEntry();
		try {
			while (true) {
				MyEntry msg = (MyEntry) space.take(template, null,Lease.FOREVER);
				System.out.println(format.format(new Date()) + " - "
						+ msg.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Client Feeder

Here is the client feeder application.

The feeder simply connects to the space and writes an Entry every 200ms.

package com.gigaspaces.sba;

import net.jini.core.lease.Lease;
import com.j_spaces.core.IJSpace;
import com.j_spaces.core.client.SpaceFinder;

public class MyFeeder
{
	private IJSpace	mySpace;

	public static void main(String[] args)
	{
		if (args.length != 1)
		{
			System.out.println("Usage: <spaceURL>");
			System.out.println("<protocol>://host:port/containername/spacename");
			System.exit(1);
		}

		IJSpace space = null;
		try
		{
			System.out.println("Connect to " + args[0]);

			space = (IJSpace) SpaceFinder.find(args[0]);
			if (space == null)
			{
				System.out.println("Space not found: " + args[0]);
				System.exit(-1);
			}

			for (int i = 0;; ++i)
			{
				MyEntry myEntry = new MyEntry();

				myEntry.routingKey = new Integer(i);
				myEntry.str = "str" + i;
				myEntry.anInt = new Integer(i * 3);
				if (i%10 == 0 )
				{
					System.out.print(".");
					if (i%1000 == 0 ) System.out.print("\n");
				}
				space.write(myEntry, null, Lease.FOREVER);

				Thread.sleep(200);	
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

}

Space Entry Class

Here is the Space Entry class. The first index defines the routing field used when data is partitioned across the partitions.

package com.gigaspaces.sba;
import net.jini.core.entry.Entry;
public class MyEntry implements Entry
{
	public Integer routingKey;
	public String str;
	public Integer anInt;
	public static String[] __getSpaceIndexedFields()
	{
	    String[] indexedFields = {"routingKey"};
	    return indexedFields;
	}

	public String toString()
	{
		return "MyEntry[routingKey=" + routingKey + ",str=" + str + ",anInt=" + anInt + "]";
	}
}

Deployment Descriptor

Here is the Deployment Descriptor used to deploy the partitioned spaces.

<?xml version="1.0"?>
<!DOCTYPE gs-deployment SYSTEM "java://gs-deploy-desc.dtd">
<gs-deployment Name="Hello SBA">
    <Codebase>$[com.gigaspaces.system.codeserver]</Codebase>

<Groups IncludeGlobalDecl="no">
    <Group>${com.gs.jini_lus.groups}</Group>
</Groups>

<!--
Defines the system requirements and the parameters for a scaling
policy handler
-->
<ServiceLevelAgreements>
    <SystemRequirements>
        <Utilization High=".9" ID="System" />
        <Utilization High=".9" ID="CPU" />
        <Utilization High=".9" ID="Memory" />
        <SystemComponent Name="GigaSpacesSupport">
            <Attribute Name="Version" Value="5*" />
        </SystemComponent>
    </SystemRequirements>
</ServiceLevelAgreements>

<!--
Defines the GigaSpacesFaultDetectionHandler
-->
<FaultDetectionHandler ClassName="com.gigaspaces.grid.space.GigaSpacesFaultDetectionHandler">
    <Configuration IncludeGlobalDecl="no">
        <Component Name="com.gigaspaces.grid.space.GigaSpacesFaultDetectionHandler">
            <Parameter Name="retryCount" Value="1" />
            <Parameter Name="invocationDelay" Value="5000" />
        </Component>
    </Configuration>
</FaultDetectionHandler>

<!--
Defines service provisioning configuration properties
-->
<ServiceProvisionConfig>
    <Configuration IncludeGlobalDecl="no">
        <Component Name="service.provision">
            <Parameter Name="proxyPreparer" Value="new com.gigaspaces.grid.space.SpaceProxyPreparer()" />
        </Component>
    </Configuration>
</ServiceProvisionConfig>

<!--
Defines middleware instantiation handler
-->
<Configuration IncludeGlobalDecl="no">
    <Component Name="service.load">
        <Parameter Name="serviceBeanFactory" Value="new com.gigaspaces.grid.space.SpaceHandler()" />
    </Component>
</Configuration>

<Parameters IncludeGlobalDecl="no">
    <Parameter Name="gs.space.url.arg.spaceName" Value="space" />
    <Parameter Name="gs.space.url.arg.total_members" Value="2,1" />
    <Parameter Name="gs.space.url.arg.cluster_schema" Value="partitioned-sync2backup" />
    <Parameter Name="gs.space.url.arg.schema" Value="sba" />
</Parameters>

<!-- Partion 1-->
<ServiceBean	AutoAdvertise="yes" 
				DiscoveryManagementPooling="yes" 
				MatchOnName="yes" 
				Name="sba.1" 
				ProvisionType="dynamic">


	<!--
	Defines the interface/client jars
	-->
	<Interfaces>
		<Interface>net.jini.space.JavaSpace</Interface>
		<Resources Algorithm="sha" ComputeHttpmd="no">
			<JAR>JSpaces-dl.jar</JAR>
			<JAR>gs-dl.jar</JAR>
			<JAR>examples/helloSBA/lib/helloSBA.jar</JAR>
		</Resources>
	</Interfaces>

	<!--
	Defines the Implementation/server jars
	-->
	<ImplementationClass>com.j_spaces.core.client.SpaceFinder
		<Resources>
			<JAR>examples/helloSBA/lib/helloSBA.jar</JAR>
			<JAR>../lib/JSpaces.jar</JAR>
			<JAR>lib/gs-space-framework.jar</JAR>
			<JAR>../lib/common/backport-util-concurrent.jar</JAR>
		</Resources>
	</ImplementationClass>

	<MaxPerMachine>1</MaxPerMachine>
	<Maintain>2</Maintain>
</ServiceBean>

<!-- Partion 2-->
<ServiceBean	AutoAdvertise="yes" 
				DiscoveryManagementPooling="yes" 
				MatchOnName="yes" 
				Name="sba.2" 
				ProvisionType="dynamic">


	<!--
	Defines the interface/client jars
	-->
   <Interfaces>
		<Interface>net.jini.space.JavaSpace</Interface>
		<Resources Algorithm="sha" ComputeHttpmd="no">
			<JAR>JSpaces-dl.jar</JAR>
			<JAR>gs-dl.jar</JAR>
		</Resources>
	</Interfaces>
	
	<!--
	Defines the Implementation/server jars
	-->
	<ImplementationClass>com.j_spaces.core.client.SpaceFinder
		<Resources>
			<JAR>examples/helloSBA/lib/helloSBA.jar</JAR>
			<JAR>../lib/JSpaces.jar</JAR>
			<JAR>lib/gs-space-framework.jar</JAR>
			<JAR>../lib/common/backport-util-concurrent.jar</JAR>
		</Resources>
	</ImplementationClass>

	<MaxPerMachine>1</MaxPerMachine>
	<Maintain>2</Maintain>
</ServiceBean>

</gs-deployment>

Space Schema

The space schema is located under GigaSpaces Root\ServiceGrid\examples\helloSBA\config\space-config\config\schemas and is called sba-space-schema.xml. The space schema includes the following IWorker settings:

<workers>
	<worker-names>MyWorker</worker-names>
	<!--interrupt the worker on shutdown-->
	<interrupt>false</interrupt>
	<MyWorker>
		 <enabled>true</enabled>
		 <shutdown-space-on-init-failure>true</shutdown-space-on-init-failure>
		 <active-when-backup>false</active-when-backup>
		 <class-name>com.gigaspaces.sba.MyWorker</class-name>
		 <arg>
		 </arg>
		 <description>MyWorker</description>
	</MyWorker>
</workers>

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

 
(None)