use of org.eclipse.equinox.concurrent.future.IFuture in project ecf by eclipse.
the class HelloConsumerApplication method addingService.
/**
* Method called when a REMOTE IHello instance is registered.
*/
public Object addingService(ServiceReference reference) {
System.out.println("IHello service proxy being added");
// Since this reference is for a remote service,
// The service object returned is a proxy implementing the
// IHello interface
IHello proxy = (IHello) bundleContext.getService(reference);
// 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();
// Call other helloMessage method
System.out.println("STARTING remote call via proxy...");
proxy.helloMessage(new HelloMessage(CONSUMER_NAME + " via proxy", "howdy"));
System.out.println("COMPLETED remote call via proxy");
System.out.println();
// 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<String>() {
public void onSuccess(String 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 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...");
Future<String> 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
String 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();
} catch (ExecutionException e) {
System.out.println("COMPLETED remote call with callback INTERRUPTED with exception=" + e);
System.out.println();
e.printStackTrace();
}
// Call other helloMessage method
// Call asynchronously with callback
System.out.println("STARTING async remote call via callback...");
helloA.helloMessageAsync(new HelloMessage(CONSUMER_NAME + " via async proxy with listener", "howdy"), callback);
System.out.println("LOCAL async invocation complete");
System.out.println();
// Call asynchronously with future
System.out.println("STARTING async remote call via future...");
future = helloA.helloMessageAsync(new HelloMessage(CONSUMER_NAME + " via async proxy with future", "howdy"));
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
String 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();
} catch (ExecutionException e) {
System.out.println("COMPLETED remote call with callback INTERRUPTED with exception=" + e);
System.out.println();
e.printStackTrace();
}
}
// OSGi 4.2 remote service spec requires a property named 'service.imported' to be
// set to a non-null value. In the case of any ECF provider, this 'service.imported' property
// is set to the IRemoteService object associated with the remote service.
IRemoteService remoteService = (IRemoteService) reference.getProperty(IDistributionConstants.SERVICE_IMPORTED);
Assert.isNotNull(remoteService);
// This IRemoteService instance allows allows non-blocking/asynchronous invocation of
// remote methods. This allows the client to decide (at runtime if necessary) whether
// to do synchronous/blocking calls or asynchronous/non-blocking calls.
// It's also possible to get the remote service directly from the proxy
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");
// 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();
}
return proxy;
}
use of org.eclipse.equinox.concurrent.future.IFuture in project ecf by eclipse.
the class HelloClientComponent method bindHello.
public void bindHello(IHello proxy) {
// First print out on console that we got a proxy instance
System.out.println("Got proxy IHello=" + proxy);
// 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();
// 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<String>() {
public void onSuccess(String 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 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...");
Future<String> 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
String 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();
} catch (ExecutionException 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();
}
}
use of org.eclipse.equinox.concurrent.future.IFuture in project ecf by eclipse.
the class AbstractConcatConsumerTestCase method testAsyncResult.
public void testAsyncResult() throws Exception {
final IRemoteService service = getRemoteService(targetID, getRemoteServiceClass().getName(), getRemoteServiceFilter());
assertNotNull(service);
traceCallStart("callAsynchResult");
final IFuture result = service.callAsync(createRemoteConcat("ECF AsynchResults ", "are cool"));
Thread.sleep(ASYNC_WAITTIME);
traceCallEnd("callAsynchResult", result);
assertNotNull(result);
}
use of org.eclipse.equinox.concurrent.future.IFuture in project ecf by eclipse.
the class RemoteServiceTest method testServiceListener.
public void testServiceListener() throws Exception {
final IRemoteServiceContainerAdapter[] adapters = getRemoteServiceAdapters();
done = false;
final Object lock = new Object();
adapters[1].addRemoteServiceListener(new IRemoteServiceListener() {
public void handleServiceEvent(IRemoteServiceEvent event) {
if (event instanceof IRemoteServiceRegisteredEvent) {
IRemoteServiceRegisteredEvent e = (IRemoteServiceRegisteredEvent) event;
IRemoteServiceReference ref = e.getReference();
remoteService = adapters[1].getRemoteService(ref);
assertNotNull(remoteService);
synchronized (lock) {
done = true;
lock.notify();
}
}
}
});
// Now register service on server (adapters[0]). This should result in notification on client (adapters[1])
// in above handleServiceEvent
adapters[0].registerRemoteService(new String[] { IConcatService.class.getName() }, createService(), customizeProperties(null));
// wait until block above called asynchronously
int count = 0;
synchronized (lock) {
while (!done && count++ < 10) {
try {
lock.wait(1000);
} catch (InterruptedException e) {
fail();
}
}
}
assertTrue(done);
if (remoteService == null)
return;
// We've got the remote service, so we're good to go
assertTrue(remoteService != null);
traceCallStart("callAsynchResult");
final IFuture result = remoteService.callAsync(createRemoteConcat("ECF AsynchResults ", "are cool"));
traceCallEnd("callAsynch");
assertNotNull(result);
Thread.sleep(SLEEPTIME);
}
use of org.eclipse.equinox.concurrent.future.IFuture in project ecf by eclipse.
the class SSLRemoteServiceTest method testServiceListener.
public void testServiceListener() throws Exception {
final IRemoteServiceContainerAdapter[] adapters = getRemoteServiceAdapters();
done = false;
final Object lock = new Object();
adapters[1].addRemoteServiceListener(new IRemoteServiceListener() {
public void handleServiceEvent(IRemoteServiceEvent event) {
if (event instanceof IRemoteServiceRegisteredEvent) {
IRemoteServiceRegisteredEvent e = (IRemoteServiceRegisteredEvent) event;
IRemoteServiceReference ref = e.getReference();
remoteService = adapters[1].getRemoteService(ref);
assertNotNull(remoteService);
synchronized (lock) {
done = true;
lock.notify();
}
}
}
});
// Now register service on server (adapters[0]). This should result in notification on client (adapters[1])
// in above handleServiceEvent
adapters[0].registerRemoteService(new String[] { IConcatService.class.getName() }, createService(), customizeProperties(null));
// wait until block above called asynchronously
int count = 0;
synchronized (lock) {
while (!done && count++ < 10) {
try {
lock.wait(1000);
} catch (InterruptedException e) {
fail();
}
}
}
assertTrue(done);
if (remoteService == null)
return;
// We've got the remote service, so we're good to go
assertTrue(remoteService != null);
traceCallStart("callAsynchResult");
final IFuture result = remoteService.callAsync(createRemoteConcat("ECF AsynchResults ", "are cool"));
traceCallEnd("callAsynch");
assertNotNull(result);
Thread.sleep(SLEEPTIME);
}
Aggregations