|
OverviewThis simple OpenSpaces application illustrates a couple of OpenSpaces core features, namely:
Developing and deploying an application with OpenSpaces is easy and consists of 4 simples steps:
To achieve linear scalability all you need to do is to deploy multipe instances of your packaged application - the Processing Unit (PU). 1. The POJO domain modelThe domain model consists of the objects that we will write to a space and read back. In this example we have the Message object, a simple object that contains a string. public class Message { private String message; public Message() { } public Message(String message) { this.message = message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } 2. The POJO ServiceOnce we have the domain model we can implement the business logic of the application, based on the model we defined. This very simple example uses the GigaSpace API to write one message with a random message into the space and then take (read and delete) from the space. We defined the service class as a Spring Bean to take advantage of the Spring framework public class HelloWorldBean implements InitializingBean and defined the GigaSpace object. private GigaSpace gigaSpace;
Now note that we defined a setter method for the GigaSpace object. This method will later be implicitly called by the Spring framework as defined in the Spring configuration file to inject the GigaSpace instance. public void setGigaSpace(GigaSpace gigaSpace) { this.gigaSpace = gigaSpace; } Finally, the business logic of our service is implemented in the afterPropertiesSet() method which is called as part of the Spring Bean life cycle, when the Spring container starts (the Spring container in our case is the Processing Unit). public void afterPropertiesSet() throws Exception { Assert.notNull(gigaSpace, "gigaSpace property is required"); RandomData randomData = new RandomDataImpl(); Message message = new Message("Hello World " + randomData.nextLong(0l, 100l)); System.out.println("Writing Message [" + message.getMessage() + "]"); gigaSpace.write(message); message = gigaSpace.take(new Message()); System.out.println("Took Message [" + message.getMessage() + "]"); } 3. Wiring with SpringAfter defining the domain model and implementing the service, we need to wire them all together. [..] <os-core:space id="space" url="/./mySpace" /> <os-core:giga-space id="gigaSpace" space="space" /> <bean id="helloworld" class="org.openspaces.example.helloworld.HelloWorldBean"> <property name="gigaSpace" ref="gigaSpace" /> </bean> [..] The first two elements are used to define a GigaSpace object that connects to a space using the url /./mySpace. 4.1 Building and PackagingThis example comes with a build.xml ant file and with a build.(bat/sh) to execute ant (note that there is no need to pre-install ant, the ant jars are already bundled in the GigaSpaces lib directory of the product). From the example root directory <GigaSpaces Root>\examples\openspaces\helloworld call: build build This will compile the code to a pu directory and copy the deployment descriptor of the Processing Unit, pu.xml.
next call: build dist This call will first copy the common math library jar (which is used in this example) under the lib directory and create a packaged jar from the entire contents of the pu directory. The created package called helloworld.jar will be used for the deployment of the Processing Unit to the grid. 4.2 DeploymentStandalone DeploymentThere are two options to deploy the Processing Unit we just created. First option is to run a standalone processing unit container and deploy our processing unit into it. This is done simply by calling the following command from your <GigaSpaces Root>\bin directory: puInstance ..\examples\openspaces\helloworld\pu\helloworld This command will create the standalone container and deploy the helloworld processing unit to it. Because the business logic is defined in the afterPropertiesSet() method which is part of the Spring bean lifecycle, the method will be excuted as soon as the application is deployed to the container and you should see output similar to this: Writing Message [Hello World 98] Took Message [Hello World 98] Grid DeploymentThe second option is to deploy the processing unit to GigaSpaces Service Grid. To start the Service Grid, you'll need to start one Grid Service Manager (GSM) by calling <GigaSpaces Root>\bin\gsm and at least one Grid Service Container (GSC) by calling <GigaSpaces Root>\bin\gsc. To deploy the processing unit to the container go back to the example root directory <GigaSpaces Root>\examples\openspaces\helloworld and call: build deploy-local-helloworld In the console window of the container (GSC) you will see the output as below Writing Message [Hello World 25] Took Message [Hello World 25] In addition you will see the following message, indicating that the Processing Unit was deployed to the grid FINE [org.jini.rio.cybernode]: hello-world.PU [Look And Feel - ServiceGrid]: advertised |
(works on Firefox 2 and Internet Explorer 7)