|
Summary: Sample implementation showing how to perform remote invocation for business logic of parallel processing units.
OverviewIf you wish to allow users to utilize the GigaSpaces Grid, you can invoke your business logic on remote processes. Proxies to the remote objects are generated automatically. Remoting is implemented using JavaSpaces as the transport layer, similar to existing Spring remoting technologies such as RMI or web services. The remoting support is composed of three logical units: Taker, Worker and Delegate. The Taker is responsible for accessing the Delegate, which, in turn, executes code located on the Worker. These three units may be co-located within the same virtual machine, or separately deployed on three different nodes / virtual machines. The different parties communicate via Task and Result objects. This section explains how Spring remoting works in GigaSpaces, by means of an example. A sample implementation is shown for Master, Worker, Task and Result classes and related classes. The MasterThe Master executes remote business logic running on the Worker. The ITask implementation is the actual business logic executed on the Worker. The remote worker returns a Result object: ITask proxy = (ITask)applicationContext.getBean("proxy"); Result res = proxy.execute("data"); The WorkerThe Worker implementation uses the generic DelegatingWorker: public class Worker { //member for gigaspaces template private GigaSpacesTemplate template; //The delegator worker private DelegatingWorker iTestBeanWorker; private ApplicationContext applicationContext; private Thread itbThread; protected void init() throws Exception { applicationContext = new ClassPathXmlApplicationContext("gigaspaces_master_remoting.xml"); template = (GigaSpacesTemplate)applicationContext.getBean("gigaspacesTemplate"); iTestBeanWorker = (DelegatingWorker)applicationContext.getBean("testBeanWorker"); } protected void start() { itbThread = new Thread(iTestBeanWorker); itbThread.start(); } public static void main(String[] args) { try { System.out.println("nWelcome to Spring GigaSpaces Worker remote Example!n"); Worker worker = new Worker(); worker.init(); worker.start(); } catch (Exception ux) { ux.printStackTrace(); System.err.println("transError problem..." + ux.getMessage()); } } } The Task and its execution by the WorkerThis is the task interface: public interface ITask extends Serializable{ public Result execute(String data); } This is the ITask interface implementation used at the worker: public class Task implements ITask{ private long counter = 0; public Task() { } /** * Execute the task */ public Result execute(String data) { counter++; System.out.println("I am doing the task id = "+counter+" with data : "+data); Result result = new Result(); result.setTaskID(counter); // do the calc result.setAnswer(data); return result ; } } The ResultThe Result object used to transport the Answer back to the client caller: public class Result implements Serializable { private long taskID; // task id private String answer = null; // result public Result() {} public String getAnswer() {return answer; } public void setAnswer(String answer){this.answer = answer;} public long getTaskID(){ return taskID;} public void setTaskID(long taskID){this.taskID = taskID;} } gigaspaces_master_remoting.xmlThis file includes properties injected into the following classes:
GigaSpacesUidFactoryTo ensure each client gets the relevant result object back from the Worker, the Task and Result are injected with a unique UID generated on the client side. DelegatingWorkerThe org.springmodules.javaspaces.gigaspaces.DelegatingWorker class configures the Worker. This is not a singleton class. The DelegatingWorker class includes the following properties:
GigaSpacesInterceptorThe com.gigaspaces.spring.GigaSpacesInterceptor controls the client side Interceptor that submits the task into the space and gets the result back. The latter can be done either synchronously or asynchronously, allowing the client to either wait or continue with its activities before the result is returned from the worker. The GigaSpacesInterceptor extends the JavaSpaceInterceptor, which supports UID injection to the task and result objects, allowing clients to retrieve the related result for a specific task.
ProxyFactoryBeanorg.springframework.aop.framework.ProxyFactoryBean is the client-side proxy.
Application Context File<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <!-- Root application context --> <beans> <!-- Declaration of GigaSpaces factory bean --> <bean id="gigaspace" class="org.springmodules.javaspaces.gigaspaces.GigaSpacesFactoryBean"> <property name="urls"> <list> <value>jini://*/*/remotingSpace</value> </list> </property> </bean> <!-- Declaration of GigaSpaces uid factory --> <bean id="gigaSpacesUidFactory" class="org.springmodules.javaspaces.gigaspaces.remote.support.GigaSpacesUidFactory"> <property name="space" ref="gigaspace"/> </bean> <!-- Declaration of GigaSpace template--> <bean id="gigaspacesTemplate" class="org.springmodules.javaspaces.gigaspaces.GigaSpacesTemplate"> <property name="space" ref="gigaspace" /> </bean> <bean id="testBeanWorker" class=" org.springmodules.javaspaces.DelegatingWorker" singleton="false" > <property name="javaSpaceTemplate" ref="gigaspacesTemplate"/> <property name="delegate"><ref local="taskBean"/></property> <property name="businessInterface"><value>com.gigaspaces.spring.examples. remote.ITask</value></property> </bean> <bean id="javaSpaceInterceptor" class=" org.springmodules.javaspaces.gigaspaces.GigaSpacesInterceptor"> <property name="javaSpaceTemplate" ref="gigaspacesTemplate"/> <property name="uidFactory" ref="gigaSpacesUidFactory"/> <property name="synchronous"><value>true</value></property> <!-- The Time for waiting to take the result from the space by the master --> <property name="timeoutMillis"><value>3000</value></property> <!-- Comment out this property for "service seeking" behavior where the endpoint is assumed to host a service to invoke. --> <!-- <property name="serializableTarget" ref="taskBean"> </property> --> </bean> <!-- This is the client-side proxy. --> <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="interceptorNames"> <list> <value>javaSpaceInterceptor</value> <!-- <value>PerformanceMonitorInterceptor</value> --> </list> </property> <property name="proxyInterfaces"> <list> <value>com.gigaspaces.spring.examples.remote.ITask</value> </list> </property> </bean> <!-- Simple test target --> <bean id="taskBean" class="com.gigaspaces.spring.examples.remote.Task" > </bean> </beans> |
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