## AOP Asynchronous Method Invocation

This aspect can run outside of JBoss application server

### Goal

The aim of this AOP service is to achieve asynchronous method invocation to plain old JAVA object (POJO).

### Features

- Implemented as a standalone service (can run outside of JBOSS).
- Ability to define Asynchronous method declaration in XML or using JDK1.5 annotations.
- Support ONEWAY and REQUEST/REPLY asynchronous method invocation.
- Support of instance and static asynchronous method invocation.
- Support of public, protected, private and package method.
- Support of concurrent invocation on same instance/static method.
- Ability to define Timeout value for duration of asynchronous method execution.
- Thread pooling Management (Min/Max threads, keep-alive, abort/wait policy)

### Example

#### POJO implementation using JDK1.4 or JDK1.5 annotations for Asynchronous method declaration. For JDK 1.4 you need the AnnotationCompiler

JDK 1.4

    public class POJO {

    /** @@org.jboss.aspects.asynchronous.aspects.jboss.Asynchronous  */
    	public void processBusinessModel(){...}

    /** @@org.jboss.aspects.asynchronous.aspects.jboss.Asynchronous (timeout=5000)*/
    	public long processBusinessModel(...){}

    }

JDK 5.0

    import org.jboss.aspects.asynchronous.aspects.jboss.Asynchronous;

    public class POJO {
    	public void processBusinessModel(){...}

            @Asynchronous
    	public long processBusinessModel(...){}

    }

#### Asynchronous Method Invocation and result process within same thread of execution

    POJO pojo = new POJO(...);

/* Asynchronous Method Invocation */

    long result=pojo.processBusinessModel(...);
    ...
    ...

/* non-blocking call */

    if (((AsynchronousFacade)pojo).isDone())
    {
    AsynchronousFacade aF=(AsynchronousFacade)pojo;

/* Test response code returned */

    if (aF.getResponseCode()==OK)

/* get method response processed asynchronously */

    result = ((Long)aF.getReturnValue()).longValue();

/* Test if method timed out */

    else if (aF.getResponseCode()==TIMEOUT)
    {...}
    else
    ...
    }

    else {

/* blocking call */

    AsynchronousResponse aR = ((AsynchronousFacade)pojo).waitForResponse();
    If (aR.getResponseCode()==OK)

/* get method response processed asynchronously */

    result=((Long)aR.getReturnValue()).longValue();

/* Test if method timed out */

    else if (aR.getReponseCode()==TIMEOUT)
    {...}
    else
    {...}
    }

### Implementation

Few pointers regarding the implementation . The implementation is broken down in 4 components :

- Asynchronous Engine Definition\\

/org/jboss/aspects/asynchronous\\ /org/jboss/aspects/asynchronous/common\\

- Asynchronous Engine Implementation with the concurrent package\\

/org/jboss/aspects/asynchronous/concurrent\\

- Asynchronous Facade Definition and Implementation (Mixin Class)\\

/org/jboss/aspects/asynchronous/aspects\\

- Asynchronous Aspect Implementation with JBOSS-AOP\\

/org/jboss/aspects/asynchronus/aspects/jboss\\ The Asynchronous Aspect is composed of one Mixin class (AsynchronousFacade.java) and one method interceptor.(AsynchronousAspect.java)

### Configuration

- JBOSS-AOP.xml

If not running within JBoss you'll need this binding:

    <?xml version="1.0" encoding="UTF-8"?>
    <aop>

    <aspect class="org.jboss.aspects.asynchronous.aspects.jboss.AsynchronousAspect" scope="PER_VM"/>

    <bind pointcut="execution(* *->@org.jboss.aspects.asynchronous.aspects.jboss.Asynchronous(..))">
       <advice name="execute" aspect="org.jboss.aspects.asynchronous.aspects.jboss.AsynchronousAspect"/>
    </bind>

    <introduction expr="has(* *->@org.jboss.aspects.asynchronous.aspects.jboss.Asynchronous(..)) OR class(@org.jboss.aspects.asynchronous.aspects.jboss.Asynchronous)">

           <mixin>
              <interfaces>
                org.jboss.aspects.asynchronous.aspects.AsynchronousFacade
              </interfaces>
              <class>org.jboss.aspects.asynchronous.aspects.AsynchronousFacadeImpl</class>
              <construction>new org.jboss.aspects.asynchronous.aspects.AsynchronousFacadeImpl()</construction>
           </mixin>
    </introduction>

    </aop>

- threadManager.properties

### Author

- Claude Hussenet (mailto:chussenet@yahoo.com)