Using a GridServer-Generated Proxy

Using the Service API is easy and flexible, but there is an easier way to access Service implementations you write and deploy on GridServer. After registering the Service Type using the Administration Tool, a menu action becomes available on the Services > Services > Service Types page for creating client-side access proxies in either Java or C#.

There are several advantages to using proxies:

No manual coding required — the proxy code itself does the argument passing and return value casting. It’s a lot easier to use a class that looks like your Service!
Type-safe — Argument and return types are checked at compile-time, instead of on the Engine at runtime.
Vendor Neutral — Application code does not need to have compile-time dependency with any DataSynapse-specific libraries if the proxies are used. The proxies themselves make use of DataSynapse libraries, but do not expose this dependency to your application.

The following code demonstrates calling the add method on the proxy object:

// Create an instance of the Service Proxy
JavaAdderProxy adder = new JavaAdderProxy();
// Perform Synchronous add
double sum = adder.add(8.0, 10.0);
System.out.println("Result of Add: " + sum);

So what’s going on behind the scenes? Here is a look at the JavaAdderProxy code for the method add:

public double add(double in0, double in1) throws Exception {
return ((java.lang.Double) execute("add", new Object[] {new java.lang.Double(in0), new java.lang.Double(in1)})).doubleValue();
}

If you want to call this method asynchronously (possibly to have many requests done in parallel) you can create a callback class that implements an interface found in the proxy. The callback interface defined in the proxy is found below:

public interface Callback extends ServiceBindingStub.AsyncCallback {
public void handleResponse(Object response, int id);
public void handleError(Exception e, int id);
}

Use this class in a fashion similar to that of example 3.3. Instead of implementing the ServiceInvocationHandler class you must implement the JavaAdderProxy.Callback class. The benefit to this approach, however, is that the code you write for this callback does not have to import any DataSynapse classes. All you need to do is import your proxy and use it.

When using a GridServer-generated proxy, asynchronous calls are performed by invoking an overloaded method that has the same name and arguments as the synchronous version, but adds an additional argument for the callback. The proxy also has a waitUntilInactive method to pause the current thread until all outstanding asynchronous calls have completed. The following example illustrates this:

// Perform Asynchronous adds
AdderCallback callback = new AdderCallback();
for (int i=0; i < 10; i++) {
    adder.add(i, i, callback);
}
// Wait until all the invocations have returned
adder.waitUntilInactive();
System.out.println("Total: " + callback.getTotal());