Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it a good idea to create custom "Asynch" annotation for asynchronous execution of a method call?
    primarykey
    data
    text
    <p>Following code create that Annotation.</p> <p>As described here, <a href="http://engineering.webengage.com/2012/03/12/a-peek-into-webengages-security-layer-super-cool-use-of-java-annotations" rel="nofollow">http://engineering.webengage.com/2012/03/12/a-peek-into-webengages-security-layer-super-cool-use-of-java-annotations</a></p> <pre><code>/** * Defining the Asynch interface */ @Retention(RetentionPolicy.RUNTIME) public @interface Asynch {} /** * Implementation of the Asynch interface. Every method in our controllers * goes through this interceptor. If the Asynch annotation is present, * this implementation invokes a new Thread to execute the method. Simple! */ public class AsynchInterceptor implements MethodInterceptor { public Object invoke(final MethodInvocation invocation) throws Throwable { Method method = invocation.getMethod(); Annotation[] declaredAnnotations = method.getDeclaredAnnotations(); if(declaredAnnotations != null &amp;&amp; declaredAnnotations.length &gt; 0) { for (Annotation annotation : declaredAnnotations) { if(annotation instanceof Asynch) { //start the requested task in a new thread and immediately //return back control to the caller new Thread(invocation.getMethod().getName()) { public void execute() { invocation.proceed(); } }.start(); return null; } } } return invocation.proceed(); } } </code></pre> <p>And it is used with method calls that returns nothing ( void ).</p> <p>Example,</p> <pre><code>/** * So, earlier we had a simple method in our interface which we later * annotated with the Asynch @interface. Bang! The caller doesn't need * to worry about it now. This method (no matter who the caller is) * gets executed asynchronously. Ain't that awesome? */ @Asynch public void refreshSurveyStatusOnResponse(String licenseCode, Integer surveyId); </code></pre> <p>What are the pros and cons ? What if we used a message queue and worker thread pool to solve instead of asynchronous method call ? What solution could have been used from standard java instead of such homegrown solution? Above method seems to have one backlog that Asynch method calls do not return any value in such a case above code will break. ,Is it logical to expect a return value on asynchronous method call ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload