OpenSpaces Hello World Example

  Search Here
Searching GigaSpaces XAP/EDG 6.0 Documentation

                                               

Summary: This example introduces OpenSpaces and the Processing Unit with its configuration file, the pu.xml.

This page is specific to:
GigaSpaces 6.0

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

Example Root <GigaSpaces Root>\examples\openspaces\helloworld
Example Source Message.java; HelloWorldBean.java

Overview

This simple OpenSpaces application illustrates a couple of OpenSpaces core features, namely:

  • Introduction to the Processing Unit concept, which uses Spring as its container.
  • The definition of a Space within a processing unit.
  • The definition of the GigaSpace simplified interface based on a Space.
  • The definition of a simple bean that interacts with the Space using the GigaSpace API.
  • Deployment of a Processing Unit to the Service Grid

Developing and deploying an application with OpenSpaces is easy and consists of 4 simples steps:

  1. Implement the POJO domain model of your application.
  2. Implement the POJO services, i.e. the business logic of the application.
  3. Wire the services together using the Spring framework.
  4. Package and deploy the application to a grid.

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 model

The 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 Service

Once 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 Spring

After defining the domain model and implementing the service, we need to wire them all together.
The configuration of the Processing Unit is done within a single xml file where we define the entities within that PU such as the Javaspaces space as well as our service HelloWorldBean.

[..]
<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.
The third element defines our service as a Spring bean. The sub-element property means that the GigaSpace object defined above will be injected into our service bean by calling the setGigaSpace method.

4.1 Building and Packaging

This 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.

The deployment descriptor should always reside under the META-INF\spring directory of your application.

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 Deployment

Standalone Deployment

There 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 Deployment

The 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



GigaSpaces 6.0 Documentation Contents (Current Page in Bold)

    Java

    C++

    .NET

    Middleware Capabilities

    Configuration and Management

Add GigaSpaces wiki search to your browser search engines!
(works on Firefox 2 and Internet Explorer 7)

Labels

 
(None)