From 9e5732dc3e53a05c7993bc61b8a184dd404a3592 Mon Sep 17 00:00:00 2001 From: slewis Date: Wed, 14 Apr 2010 23:10:23 +0000 Subject: Added IHelloAsync and changed examples to use/reference it...via ds/component and via service tracker --- .../META-INF/MANIFEST.MF | 4 +- .../hello/ds/consumer/HelloClientComponent.java | 110 +++++++++++++++++---- 2 files changed, 93 insertions(+), 21 deletions(-) diff --git a/examples/bundles/org.eclipse.ecf.examples.remoteservices.hello.ds.consumer/META-INF/MANIFEST.MF b/examples/bundles/org.eclipse.ecf.examples.remoteservices.hello.ds.consumer/META-INF/MANIFEST.MF index 9cfffae17..e86850a63 100644 --- a/examples/bundles/org.eclipse.ecf.examples.remoteservices.hello.ds.consumer/META-INF/MANIFEST.MF +++ b/examples/bundles/org.eclipse.ecf.examples.remoteservices.hello.ds.consumer/META-INF/MANIFEST.MF @@ -8,7 +8,9 @@ Bundle-RequiredExecutionEnvironment: J2SE-1.5 Import-Package: org.eclipse.ecf.examples.remoteservices.hello, org.eclipse.ecf.remoteservice, org.eclipse.ecf.remoteservice.events, - org.eclipse.equinox.app;version="1.0.0" + org.eclipse.equinox.app;version="1.0.0", + org.eclipse.equinox.concurrent.future;version="1.0.0" Service-Component: OSGI-INF/helloclient.xml Bundle-Localization: bundle Export-Package: org.eclipse.ecf.examples.internal.remoteservices.hello.ds.consumer +Require-Bundle: org.eclipse.equinox.common;bundle-version="3.6.0" diff --git a/examples/bundles/org.eclipse.ecf.examples.remoteservices.hello.ds.consumer/src/org/eclipse/ecf/examples/internal/remoteservices/hello/ds/consumer/HelloClientComponent.java b/examples/bundles/org.eclipse.ecf.examples.remoteservices.hello.ds.consumer/src/org/eclipse/ecf/examples/internal/remoteservices/hello/ds/consumer/HelloClientComponent.java index 911ced09f..760eb73e0 100644 --- a/examples/bundles/org.eclipse.ecf.examples.remoteservices.hello.ds.consumer/src/org/eclipse/ecf/examples/internal/remoteservices/hello/ds/consumer/HelloClientComponent.java +++ b/examples/bundles/org.eclipse.ecf.examples.remoteservices.hello.ds.consumer/src/org/eclipse/ecf/examples/internal/remoteservices/hello/ds/consumer/HelloClientComponent.java @@ -9,37 +9,107 @@ ******************************************************************************/ package org.eclipse.ecf.examples.internal.remoteservices.hello.ds.consumer; +import org.eclipse.core.runtime.Assert; +import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.ecf.examples.remoteservices.hello.IHello; -import org.eclipse.ecf.remoteservice.IRemoteCallListener; +import org.eclipse.ecf.examples.remoteservices.hello.IHelloAsync; +import org.eclipse.ecf.remoteservice.IAsyncCallback; import org.eclipse.ecf.remoteservice.IRemoteService; import org.eclipse.ecf.remoteservice.IRemoteServiceProxy; import org.eclipse.ecf.remoteservice.RemoteServiceHelper; -import org.eclipse.ecf.remoteservice.events.IRemoteCallCompleteEvent; -import org.eclipse.ecf.remoteservice.events.IRemoteCallEvent; +import org.eclipse.equinox.concurrent.future.IFuture; public class HelloClientComponent { private static final String CONSUMER_NAME = "helloclientcomponent"; - public void bindHello(IHello hello) { - // First print out on console that we got something - System.out.println("Got proxy IHello="+hello); - //Call proxy. Note that this call may block or fail due to - // communication with remote service - hello.hello(CONSUMER_NAME+" via proxy"); + public void bindHello(IHello proxy) { + // First print out on console that we got a proxy instance + System.out.println("Got proxy IHello="+proxy); - // Get IRemoteService from proxy. This is possible, because for all ECF providers - // the proxy also implements org.eclipse.ecf.remoteservice.IRemoteServiceProxy - IRemoteService remoteService = ((IRemoteServiceProxy) hello).getRemoteService(); - // Create listener for callback in asynchronous call - IRemoteCallListener listener = new IRemoteCallListener() { - public void handleEvent(IRemoteCallEvent event) { - if (event instanceof IRemoteCallCompleteEvent) { - System.out.println("Completed hello remote service invocation using async"); + // Call proxy synchronously. Note that this call may block or fail due to + // synchronous communication with remote service + System.out.println("STARTING remote call via proxy..."); + proxy.hello(CONSUMER_NAME+" via proxy"); + System.out.println("COMPLETED remote call via proxy"); + System.out.println(); + + // If the proxy is also an instance of IHelloAsync then use + // this asynchronous interface to invoke methods asynchronously + if (proxy instanceof IHelloAsync) { + IHelloAsync helloA = (IHelloAsync) proxy; + // Create callback for use in IHelloAsync + IAsyncCallback callback = new IAsyncCallback() { + public void onSuccess(Object result) { + System.out.println("COMPLETED remote call with callback SUCCESS with result="+result); + System.out.println(); + } + public void onFailure(Throwable t) { + System.out.println("COMPLETED remote call with callback FAILED with exception="+t); + System.out.println(); } - }}; - // Call asynchronously with listener - RemoteServiceHelper.asyncExec(remoteService, "hello", new Object[] { CONSUMER_NAME + " via async" }, listener); + }; + + // Call asynchronously with callback + System.out.println("STARTING async remote call via callback..."); + helloA.helloAsync(CONSUMER_NAME + " via async proxy with listener", callback); + System.out.println("LOCAL async invocation complete"); + System.out.println(); + + // Call asynchronously with future + System.out.println("STARTING async remote call via future..."); + IFuture future = helloA.helloAsync(CONSUMER_NAME + " via async proxy with future"); + System.out.println("LOCAL async future invocation complete"); + System.out.println(); + try { + while (!future.isDone()) { + // do some other stuff + System.out.println("LOCAL future not yet done...so we're doing other stuff while waiting for future to be done"); + Thread.sleep(200); + } + // Now it's done, so this will not block + Object result = future.get(); + System.out.println("COMPLETED remote call with future SUCCEEDED with result="+result); + System.out.println(); + } catch (OperationCanceledException e) { + System.out.println("COMPLETED remote call with callback CANCELLED with exception="+e); + System.out.println(); + e.printStackTrace(); + } catch (InterruptedException e) { + System.out.println("COMPLETED remote call with callback INTERRUPTED with exception="+e); + System.out.println(); + e.printStackTrace(); + } + } + + // It's also possible to get the remote service directly from the proxy + IRemoteService remoteService = ((IRemoteServiceProxy) proxy).getRemoteService(); + Assert.isNotNull(remoteService); + + // In this case, we will make an non-blocking call and immediately get a 'future'...which is + // a placeholder for a result of the remote computation. This will not block. + System.out.println("STARTING async remote call via future..."); + IFuture future = RemoteServiceHelper.futureExec(remoteService, "hello", + new Object[] { CONSUMER_NAME + " future" }); + System.out.println("LOCAL async future invocation complete"); + System.out.println(); + // Client can execute arbitrary code here... + try { + // This blocks until communication and computation have completed successfully + while (!future.isDone()) { + // do some other stuff + System.out.println("LOCAL future not yet done...so we're doing other stuff while waiting for future to be done"); + Thread.sleep(200); + } + // Now it's done, so this will not block + Object result = future.get(); + System.out.println("COMPLETED remote call with future SUCCEEDED with result="+result); + System.out.println(); + } catch (Exception e) { + e.printStackTrace(); + } + + } } -- cgit v1.2.3