|
Summary: Basics of developing, building and deploying a POJO, explained through a Hello World example.
OverviewThe 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.
The Hello application is minimal, and is composed of two elements:
ExampleThe 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 InterfaceThe 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 ImplementationProviding 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 DescriptorThis 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 FileHere 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
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 ExampleHow to run the example:
Deploying the ServiceTo deploy the service:
Client Accessing Deployed ServiceThe 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
RELATED TOPICS
|
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