|
Summary: Sync Remoting allows you to use remote invocations of POJO services, with the space as the transport layer.
OverviewSync remoting is characterized by the nature of how the client communicates with the server. Under the wires, sync remoting uses the GigaSpaces space filter capabilities. Define ContractIn order to support remoting, the first step is to define the contract between the client and the server. In our case, the contract is a simple Java interface. Here is an example: public interface IDataProcessor { /** * Process a given Data object, returning the processed Data object. */ Data processData(Data data); }
Implement ContractNext, an implementation of this contract needs to be provided. This implementation will "live" on the server side. Here is a sample implementation: public class DataProcessor implements IDataProcessor { public Data processData(Data data) { data.setProcessor(true); return data; } } Export Service Over SpaceNext step is exporting the service over the space. Exporting the service is done on the server side. As with other Spring remoting support, exporting the service and the method of exporting the service is a configuration-time decision. Here is an example of Spring XML configuration: Exporting the service as a remote space service uses the space support for filters.
Using Service on Client SideIn order to use the exported IDataProcessor on the client side, beans should use the IDataProcessor interface directly: public class DataRemoting { private IDataProcessor dataProcessor; public void setDataProcessor(IDataProcessor dataProcessor) { this.dataProcessor = dataProcessor; } } Configuring the injected IDataProcessor proxy can done in the following manner: Remote Routing HandlerMany times, space remoting is done by exporting services in a space with a partitioned cluster topology. The service is exported when working directly with a cluster member (and not against the whole space cluster). When working with such a topology, the client side remoting automatically generates a random routing index value. In order to control the routing index, the following interface can be implemented: public interface RemoteRoutingHandler<T> { /** * Returns the routing field value based on the remoting invocation. If <code>null</code> * is returned, will use internal calcualtion of the routing index. */ T computeRouting(SpaceRemotingInvocation remotingEntry); } Here is a sample implementation which uses the first parameter Data object type as the routing index. public class DataRemoteRoutingHandler implements RemoteRoutingHandler<Long> { public Long computeRouting(SpaceRemotingInvocation remotingEntry) { if (remotingEntry.getMethodName().equals("processData")) { Data data = (Data) remotingEntry.getArguments()[0]; return data.getType(); } return null; } } Finally, the wiring is done in the following manner: Broadcast RemotingWhen using sync remoting (since filters are used), a remote invocation can be broadcasted to all active (primary) cluster members. The configuration of enabling broadcasting is done on the client level, by setting the broadcast flag to true: Remote Result ReducerWhen broadcasting remote invocations to all active cluster members, and the remote method returns a result, on the client side an array of all remote results needs to be processed. By default, the first result is returned, and it can be overriden usign the return-first-result flag (which will then return the array of responses). The sync remoting proxy allows for a pluggable remote result reducer that can reduce a collection of remoting results into a single one. Here is the defined interface: public interface RemoteResultReducer { /** * Reduces a list of Space remoting invocation results to an Object value. Can use * the provided remote invocation to perform different reduce operations, for example * based on the {@link SpaceRemotingInvocation#getMethodName()}. * * <p>An exception thrown from the reduce operation will be propagated to the client. * * @param results A list of results from (usually) broadcast sync remote invocation. * @param remotingInvocation The remote invocation * @return A reduced return value (to the calling client) * @throws Exception An exception that will be propagated to the client */ Object reduce(SpaceRemotingResult[] results, SpaceRemotingInvocation remotingInvocation) throws Exception; } |
(works on Firefox 2 and Internet Explorer 7)