Developing and Deploying POJOs into Service Grid

  GigaSpaces 5.X

Documentation Home
Quick Start Guide
Release Notes

Previous release

  Search Here
Searching GigaSpaces Platform 5.X Documentation

                                               

Summary: Basics of developing, building and deploying a POJO, explained through a Hello World example.

Overview

The Service Grid provides the capability to build dynamic applications using Plain Old Java Objects (POJOs). Developing a POJO is simple: the Service Grid turns the POJO into a dynamic service, providing transparent Jini technology remoting, with built-in management, fault detection and automated deployment.

To learn how to utilize the service lifecycle in your application, refer to POJO Lifecycle in Service Grid.

The Hello application is minimal, and is composed of two elements:

  • Hello interface
  • HelloImpl, the implementation of the Hello interface.

Example

The example described in this section can be found in the <GigaSpaces ROOT>\ServiceGrid\examples\bean folder. The folder has the following structure:

In GigaSpaces version 5.2:

In GigaSpaces version 5.1:

Hello Interface

The Hello interface defines the semantics of the Hello POJO, and the method sayHello. The interface itself does not need to extend java.rmi.Remote, but the methods that are to be invoked remotely must throw java.rmi.RemoteException. Declaring that the method has remote semantics is an important specification item, this instructs the user(s) and implementors of this interface that the call may require remote communications.

The Hello interface is as follows:

package example;

import java.rmi.RemoteException;

public interface Hello {
    /**
     * Say hello!
     */
    String sayHello(String greetings) throws RemoteException;
}

Hello Implementation

Providing a concrete implementation for the Hello interface is straightforward:

package example;

public class HelloImpl implements Hello {
    public String sayHello(String greetings) {
        System.out.println("**** Greeter says : "+greetings);
        return("Hello!");
    }
}

Declaring POJO in Deployment Descriptor

This is the Service Grid deployment file. It should include the example - the Hello interface, the implementation class and the relevant libraries information, as follows.

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<\!DOCTYPE gs-deployment SYSTEM "java://gs-deploy-desc.dtd">
<gs-deployment Name="Hello World Example">
    <Codebase Adaptive="yes">file://${GS_GRID_HOME}examples${/}bean${/}lib</Codebase>

    <!-- Declare attributes for the Hello example -->
    <ServiceBean Name="Hello">
        <Interfaces>
            <Interface>example.Hello</Interface>
            <Resources>
                 <JAR>hello-dl.jar</JAR>
            </Resources>
        </Interfaces>

        <ImplementationClass>example.HelloImpl
            <Resources>
                 <JAR>hello.jar</JAR>
            </Resources>
        </ImplementationClass>

        <Maintain>1</Maintain>
    </ServiceBean>
</gs-deployment>

The Build File

Here is the build.xml file you need in order to build the hello libraries:

<?xml version='1.0' encoding='ISO-8859-1' standalone='yes'?>
<project name="Bean Example" default="all" >
        <property name="example.name" value="bean-example" />
        <property environment="env."/>
        <property name="examples.home" value="${basedir}
/.."/>
        <\!-\- import the common elements \-->
        <import file="../build_properties.xml"/>
        <property name="example.lib" value="${examples.home}/bean/lib"/>
<fileset dir="${example.src}">
        <patternset id="java.source">
        	<include name="example/*/.java"/>
			</patternset></fileset>
<fileset dir="${example.classes}" >

basedir="${example.classes}
 "/>
 basedir="${example.classes}
 "excludes="**/HelloImpl.class"/> </target>
</project>

Building the Example

Make sure you build the example before you run and deploy it
D:\GigaSpacesEE5.0\ServiceGrid\examples\bean>ant
Buildfile: build.xml
Overriding previous definition of reference to project.classesprepare:compile:hello.jar:
	[jar] Building jar: D:\GigaSpacesEE5.0_1503\ServiceGrid\examples\bean\
	lib\hello.jarhello-dl.jar:
	[jar] Building jar: D:\GigaSpacesEE5.0_1503\ServiceGrid\examples\bean\
	lib\hello-dl.jarjars:all:BUILD SUCCESSFUL
Total time: 2 seconds

Running the Example

How to run the example:

  1. Start the Service Grid:
    $ cd <product-install-root>/ServiceGrid/bin
    $ gs-all
    
  2. Deploy the example:
    • Using the Service Grid CLI:
      $ cd <product-install-root>/ServiceGrid/bin
      $ gs deploy ../examples/bean/config/hello.xml
      
    • Using the Service Grid UI :
      $ cd <product-install-root>/ServiceGrid/bin
      $ gs-ui
      

Deploying the Service

To deploy the service:

  1. Start the following:
    • Service Grid UI - <GigaSpaces Root>\ServiceGrid\bin\gs-ui
    • Service Grid Container - <GigaSpaces Root>\ServiceGrid\bin\gsc
    • Service Grid Manager - <GigaSpaces Root>\ServiceGrid\bin\gsm
  2. Start the Deployment Wizard by clicking the Deploy button, select An application, as defined by a deployment descriptor, and click Next.

    • A – Deployment button.
  3. Click Browse... next to Select a Deployment Descriptor and select the following descriptor: <GigaSpaces Root>\ServiceGrid\examples\bean\config\hello.xml (do not select a configuration override).

  4. Click Next, select a codebase server and Verify if necessary.
  5. Click Deploy.
    The deployment graph is displayed:

  6. If you want to view service properties, such as number of planned instances, click the Hello node:

  7. If you want to increase the number of running Hello services, simply click the Increase button for the Planned Instances property. This starts another Hello service instance on the existing running container:

Client Accessing Deployed Service

The following example demonstrates a client application obtaining a proxy for the deployed Hello service and invoking the business logic running within the Service from the client application.

package example;import java.rmi.RMISecurityManager;

import net.jini.core.entry.Entry;
import net.jini.core.lookup.ServiceItem;
import net.jini.core.lookup.ServiceTemplate;
import net.jini.discovery.LookupDiscoveryManager;
import net.jini.lease.LeaseRenewalManager;
import net.jini.lookup.ServiceDiscoveryManager;

public class HelloClient {
 static public void main(String[] args) throws Exception {  

System.out.println("Welcome to Gigaspaces Service Grid POJO Service Client Example!");  

if (System.getSecurityManager() == null)
   System.setSecurityManager(new RMISecurityManager());  

System.out.println("Looking for Lookup Service...");  

Class[] classes = new Class[] { example.Hello.class };
  
ServiceTemplate tmpl = new ServiceTemplate(null, classes,
    new Entry[] { new net.jini.lookup.entry.Name("Hello") });  

LookupDiscoveryManager lookupDiscovery = new LookupDiscoveryManager(
  /* DiscoveryGroupManagement.ALL_GROUPS */new String[] { "gs-grid" },
    null, null);

  if (lookupDiscovery != null)
   System.out.println("Found Lookup Service:" + lookupDiscovery);  

System.out.println("Getting Hello Service Proxy ...");  

ServiceDiscoveryManager serviceDiscovery =
	new ServiceDiscoveryManager(lookupDiscovery, new LeaseRenewalManager());  
// lookup for service
  ServiceItem returnObject = (ServiceItem) serviceDiscovery.lookup(tmpl,null, 10000);  

serviceDiscovery.terminate();  if (returnObject != null) {
   System.out.println("\nFound Hello Service Proxy!");
   Hello hello = (Hello) returnObject.service;
   System.out.println("\nCalling Hello Service method...");
   String ret = hello.sayHello("Hello from Client");
   System.out.println("\nGot:" + ret +" from Hello Service!");
   System.out.println("\nDone Gigaspaces Service Grid POJO Service Client Example!");
  } else {
   System.out.println("Can't discover Hello Service!");
  }
 }
}

You can use the following script to run the client:

\GigaSpacesEE5.0\ServiceGrid\examples\bean\bin>run_HelloWorldClient.bat 

You should have the following output displayed:

Welcome to Gigaspaces Service Grid POJO Service Client Example!
Looking for Lookup Service...
Found Lookup Service:net.jini.discovery.LookupDiscoveryManager@26e431
Getting Hello Service Proxy ...

Found Hello Service Proxy!

Calling Hello Service method...

Got:Hello! from Hello Service!

Done Gigaspaces Service Grid POJO Service Client Example!
Press any key to continue . . .

Advanced Options

  • Start another Service Grid container and move one of the running Services into the newly-started container by dragging one instance from the first container's panel into the second container
  • Close one container and see how the Service Grid detects the change and runs the two instances on the remaining container.

RELATED TOPICS

Developing and Deploying POJOs into Service Grid
JavaSpaces POJO
POJO Class and Field Level Annotations
Spring
Spring Configuration Files
Spring Infrastructure Services

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