|
Summary: Demonstrates a simple application which uses a Space Based Architecture (SBA) to process a workflow.
Hello SBA ExampleThis 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.
Installing the ExampleExtract 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 ExampleTo 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
Testing Failover
The IWorker ImplementationThe 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 FeederHere 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 ClassHere 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 DescriptorHere 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 SchemaThe 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.

Add Comment