Java Doc for AsyncEnabled.java in  » Web-Server » JicarillaHTTP » org » jicarilla » lang » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Server » JicarillaHTTP » org.jicarilla.lang 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


org.jicarilla.lang.AsyncEnabled

All known Subclasses:   org.jicarilla.lang.AbstractAsyncEnabled,
AsyncEnabled
public interface AsyncEnabled (Code)

Any component that implements this interface indicates it is able to accept method calls asynchronously. Clients and/or containers of the component have the option to call the handle() method rather than the work interface method directly.

The AsyncEnabled contract

This interface is intentionally very broad. In fact, its method signature could be used for an Interceptor as well, and thus even be the basis of a generic Aspect-Oriented Programming tool. That's not the intention here: the contract surrounding AsyncEnabled is more specific.

The invocation passed to the AsyncEnabled.handle(Invocation) method should be an invocation that should be directly applicable to the receiving instance. In other words, the following implementation should always produce correct behaviour:

 // FRAGMENT
 AsyncEnabled instance = new SomeAsyncEnabled()
 {
 public Object handle( Invocation invocation )
 {
 return invocation.getMethod().invoke( this, invocation.getArgs() );
 }
 public String someMethod()
 {
 return "Hello Asynchronous client";
 }
 }
 

This means that the first call in the code below is okay, while the second one is problematic.

 // FRAGMENT
 // this is okay....
 Method m = instance.getClass().getMethod( "someMethod", new Object[0] );
 Object[] args = new Object[0];
 Invocation invocation = new Invocation( instance, null, m, args );
 instance.handle( invocation ); // returns "Hello Asynchronous client"
 // but this is not!
 Method m = SomeClass.getClass().getMethod( "doStuff", new Object[0] );
 Object[] args = new Object[0];
 Invocation invocation = new Invocation( instance, null, m, args );
 instance.handle( invocation );
 // exception: can't invoke SomeClass.doStuff() on instance!
 

Furthermore, you should not assume that all methods of an AsyncEnabled class can be invoked asynchronously. The mechanism to determine whether any particular method can indeed be invoked asynchronously is not specified by this interface, but I suggest clear API documentation in addition to any other mechanisms.

Dealing with return values

When a method is invoked asynchronously, it is not possible for the caller to receive the return value of that method directly. This is best solved by using a callback.

A callback is an object that the caller supplies as an argument to the method call. The AsyncEnabled component will, on completion of the asynchronous call, provide the return value to the callback object. In the simplest case, the callback object is simply the caller itself, and the return value is provided through some kind of simple setter method. The below is a simple example; much more elaborate setups are possible.

A Complete Example

In the below example, the Client class will send its commandline arguments asynchronously to an AsyncService. This service creates a thread (which sleeps for a random time just to demonstrate how asynchronous it all is) to process the argument, sending the result back to the Client via the Callback interface. Of course, this example is as contrived as any!

 // SAMPLE CODE
 interface Callback
 {
 public void receiveResult( Object result, long uniqueIdentifier );
 }
 interface SomeService
 {
 public String doSomethingImmediately( String argument );
 }
 public void doSomething( String argument,
 Callback callback long uniqueIdentifier );
 }
 class AsyncService implements AsyncEnabled, SomeService
 {
 public Object handle( Invocation invocation )
 {
 new Thread( new Runnable()
 {
 public void run()
 {
 Thread.sleep((int)(Math.random()*1000));
 invocation.getMethod().invoke( this, invocation.getArgs() );
 } }.start();
 return null;
 }
 public String doSomethingImmediately( String argument )
 {
 return "task " + * argument + " done!";
 }
 public void doSomething( String argument, Callback callback,
 long uniqueIdentifier )
 {
 Object result = doSomethingImmediately( argument );
 callback.receiveResult( result, uniqueIdentifier );
 }
 }
 public class Client implements Callback
 {
 public static long uniqueIdentifier = 0;
 public void receiveResult( Object result, long uniqueIdentifier )
 {
 System.out.println("received results of invocation " +
 uniqueIdentifier + ": " + result;
 }
 public void main( String[] args )
 {
 AsyncService service = new AsyncService();
 for( int i = 0; i < args.length; i++ )
 {
 String arg = args[i];
 long id = uniqueIdentifier++;
 Invocation inv = new Invocation( service, null,
 service.getClass().getMethod("doSomething",
 new Class[] { String.class, Callback.class, Long.class } ),
 new Object[] { args[i], this, id } );
 service.handle( inv );
 }
 }
 }
 

author:
   Leo Simons
version:
   $Id: AsyncEnabled.java,v 1.1 2004/03/23 13:37:56 lsimons Exp $




Method Summary
public  Objecthandle(Invocation invocation)
     Asynchronously deal with a 'method call'.
Parameters:
  invocation - the value to return to the calling object.



Method Detail
handle
public Object handle(Invocation invocation) throws UnsupportedInvocationException, InterruptedException(Code)
Asynchronously deal with a 'method call'.
Parameters:
  invocation - the value to return to the calling object. Usually this will benull as asynchronous communication is likely to work usingcallbacks.
throws:
  UnsupportedInvocationException -



www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.