Package com.thoughtworks.proxy.toys.future

A toy to delay method execution into future.

See:
          Description

Class Summary
Future<T> Factory for proxy instances that run any method call concurrently and return the method result later.
Future.FutureBuild<T>  
Future.FutureWith<T>  
FutureInvoker Invoker that implements transparent asynchronous method calls.
 

Package com.thoughtworks.proxy.toys.future Description

A toy to delay method execution into future.

The package provides a proxy factory creating proxies, that runs any method call asynchronously and returns the result of the method later on. Main component is the Future toy, a utility class creating these proxies. Such a proxy contains an instance of a FutureInvoker that delegates creates proxies as immediate return values with the HotSwapping toy and runs the method call itself in a different thread. The result of the method call is hot swapped into the originally returned proxy when the method returns the correct value. The HotSwapping proxy contains a null object created with the Null toy while the correct value is not yet available.

One use case is for long running operations while the program flow can safely continue. The example demonstrates the parsing of XML for a very slow InputSource:

DocumentBuilder documentBuilder = Future.proxy(DocumentBuilderFactory.newInstance().newDocumentBuilder())
            .build(new CglibProxyFactory());
        Document document = documentBuilder.parse(new SlowInputSource(new StringReader("<root/>")));
        System.out.println("Root document name: " + document.getDocumentElement().getNodeName());
        Thread.sleep(200); // should do something more useful here
        System.out.println("Root document name: " + document.getDocumentElement().getNodeName());

Note that it makes only sense to use this toy for methods that return a non-final object, because the return value has to be proxied also. Therefore you have also take care that the ProxyFactory in use can create proxies for the provided object as well as for any return type of each method called through that proxy.



Copyright © 2005-2010 Codehaus. All Rights Reserved.