Effective Advice on Spring @Async : Final Part .


In my previous articles, I discussed the Spring Async concept and How to use it effectively. If you want to revisit,  below are the links, Also for new visitors I recommend,  please go through previous parts after reading this one.


Part 1::  How Spring Async work internally, and how to use wisely so it assigns a task to a new thread.
Part2::  How to handles Exception, If something goes wrong while executing a task.

In this part, we will discuss How Spring Async work with the web.
I am very excited to share an experience with you about Spring Async and HttpRequest, as an interesting incident happened in one of my projects, and I believe by sharing it I can save some valuable time of yours in future.
Let me try to depict the scenario in Crisp word, 
Objective: The  objective was to pass some information from UI  to a backend controller, which will do some work and eventually calls an async mail service for triggers a mail.
One of my Juniors did the following code(Try to replicate the code intention with below code snippet, not the actual code ), can you spot where the problem lies?
The Controller:: Which collect information from UI as a form  HTTP Servelet request, do some operation and pass it to async Mail service.
package com.example.demo; 

import javax.servlet.http.HttpServletRequest; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetController {

@Autowired
private AsyncMailTrigger greeter;
@RequestMapping(value = "/greet", method = RequestMethod.GET)
public String greet(HttpServletRequest request) throws Exception {
String name = request.getParameter("name");
greeter.asyncGreet(request);
System.out.println(Thread.currentThread() + " Says Name is " + name);
System.out.println(Thread.currentThread().getName() + " Hashcode" + request.hashCode());
return name;
}
}
The Async Mail Service, I marked it as @Component you can easily change it to @Service. Here I have one method called asyncGreet which takes the HttpRequest, fetch the information from there and say, trigger the mail(this part is omitted for simplicity). Notice I put a Thread.sleep() here for a purpose, I will discuss the same later.
package com.example.demo;


import java.util.Map; 
import javax.servlet.http.HttpServletRequest; 
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class AsyncMailTrigger {

@Async
public void asyncGreet(HttpServletRequest request) throws Exception {
System.out.println("Trigger mail in a New Thread :: "  + Thread.currentThread().getName());
System.out.println(Thread.currentThread().getName() + " greets before sleep" + request.getParameter("name"));
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + " greets" + request.getParameter("name"));
System.out.println(Thread.currentThread().getName() + " Hashcode" + request.hashCode());
} 

}

Now the main class
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync
public class SpringAsyncWebApplication {

public static void main(String[] args) {
SpringApplication.run(SpringAsyncWebApplication.class, args);
}

}
If I run this program output will very similar to below
Thread[http-nio-8080-exec-1,5,main] Says Name is Shamik
http-nio-8080-exec-1 Hashcode 821691136
Trigger mail in a New Thread:: task-1
task-1 greets before sleep Shamik
task-1 greets null task-1 Hashcode 821691136

Pay attention to the output, the request has the information still before sleep but after that, it magically disappears? Strange isn't?  But It is the same request object hashcode proves the same.
What happened ? what is the reason behind the disappearance of the information from Request? That was happening to my junior, the mail recipients, recipients name disappear from the request and mail is not triggered.
Let's put Sherlock hats to investigate the problem.
It is a very common problem with the request, to understand the problem have a look, how a request lifecycle works.
The request is created by the servlet container right before the call to servlet service method, In Spring then request passes through dispatcher servlet, in the Dispatcher servlet identifies the controller by request mapping and calls the desired method in the controller, and when the request has been served, servlet container either delete the request object or reset the state of the request object. (This totally depends on the container implementation, actually it maintains a pool of request). However, I am not going to the deep dive how container maintains the request object. 
But keep one thing in mind once the request has been served and response is committed, container reset its state or destroy the request object. 
Now put Spring Async part into the consideration, What async did,  it picks one thread from the thread pool and assigns the task to it, In our case, we pass the request object to the async thread and in the asyncGreet method, we are trying to extract info directly from the request.
But as this is async our main thread (Controller part)  will be not waiting for this thread to complete, so it's print the statement and commits the response, and refresh the state of the request object.
Ironically we pass the request object directly to the async thread, still, the point where the response is not committed in the main thread, request holds the data, I explicitly put a sleep a statement  so in main thread response can be committed and refresh the request state, so after sleep we experience there is no data in the request, it vanishes, a great experiment to prove the incident.

What we will learn from this experiment?
Never pass Request object or any object related to Request/Response(headers), directly while using async you never know when your response will be committed and refresh the state If you do you will face an intermittent error.

What can be done?
If you need to pass a bunch of information from request create a value object and set the information and pass the value object to Spring Async, in this manner, you can create a concrete solution.
RequestVO Object
 package com.example.demo; 
public class RequestVO {

String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
} 

}
Async Mail service
 package com.example.demo;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class AsyncMailTrigger {

@Async
public void asyncGreet(RequestVO reqVO) throws Exception {
System.out.println("Trigger mail in a New Thread :: "  + Thread.currentThread().getName());
System.out.println(Thread.currentThread().getName() + " greets before sleep" + reqVO.getName());
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + " greets" + reqVO.getName());

} 

}
Greet Controller
package com.example.demo; 

import javax.servlet.http.HttpServletRequest; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetController {

@Autowired
private AsyncMailTrigger greeter;
@RequestMapping(value = "/greet", method = RequestMethod.GET)
public String greet(HttpServletRequest request) throws Exception {
String name = request.getParameter("name");
RequestVO vo = new RequestVO();
vo.setName(name);
//greeter.asyncGreet(request);
greeter.asyncGreet(vo);
System.out.println(Thread.currentThread() + " Says Name is " + name);
System.out.println(Thread.currentThread().getName() + " Hashcode" + request.hashCode());
return name;
}
}
Output
Thread[http-nio-8080-exec-1,5,main] Says Name is Shamik
http-nio-8080-exec-1 Hashcode 1669579896
Trigger mail in a New Thread:: task-1
task-1 greets before sleep Shamik
task-1 greets Shamik

Conclusion: Hope you enjoyed the article If you have questions, please put your questions on the comment box.



Effective Advice on Spring @Async-Part 1

As per the current trend, I can see from Juniors to Seniors all are using Spring boot as a weapon to build software. Why that should not be, it is developer friendly and its "convention over configuration" style helps the developer to only focus on business logic If they are ignorant how Springs works still just seeing a Spring boot tutorial one can start using Spring boot.
I like that one,  at least major taskforce in support project is now being utilized as they do not have to know Spring inner details, just put some annotations and write the business code and voila, with that said sometimes in some cases  you have to know "How it works", what I am trying to say, you need to know your tool better then only you can utilize your tool as a PRO.
In this Article, I will give you an idea of how you can use asynchronous processing in Spring.
Any pieces of logic, which is not directly associated with business logic (Cross-cutting concerns) or a logic whose response not needed in invoker context to determine next flow or any business computation is an ideal candidate of Asyncronization. Also Integrating two distributed system Asyncronization technique is being used to make them decoupled.
In Spring we can use Asynchronization using @Async annotation, but wait here if you use randomly @Async annotation on top of a method and think your method will be invoked as a asynchronous fashion in a separate thread you are wrong. you need to know How @Async works and it's limitations, without that you can't understand Async behavior.
How @Async Works?
When you put an @Async annotation on a method, underlying it creates a proxy of that object where @Async is defined (JDK Proxy/CGlib) based on the proxyTargetClass property. then Spring tries to find a thread pool associated with the context, to submit this method's logic as a separate path of execution to be exact it searches a unique TaskExecutor bean or a bean named as taskExecutor if not found then use default SimpleAsyncTaskExecutor.
Now, as it creates a proxy and submits the job to TaskExecutor thread pool, it has few limitations which you must have to know, otherwise, you will scratch your head why your Async is not worked or create a new thread!!!

Limitations of @Async 
1. Suppose you write a class and identify a method which will act as Async and put @Async on top of that method, now if you want to use that class from another class by creating local instance then it will not fire the async, It has to be picked up by Spring @ComponentScan annotation or created inside a class marked as @Configuration.

Async Annotation use in a Class
package com.example.ask2shamik.springAsync.demo;
import java.util.Map;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class AsyncMailTrigger {
@Async
public void senMail(Map<String,String> properties) {
System.out.println("Trigger mail in a New Thread :: "  + Thread.currentThread().getName());
properties.forEach((K,V)->System.out.println("Key::" + K + " Value ::" + V));
}
}

Caller Class
package com.example.ask2shamik.springAsync.demo;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class AsyncCaller {

@Autowired
AsyncMailTrigger asyncMailTriggerObject;

public void rightWayToCall() {
System.out.println("Calling From rightWayToCall Thread " + Thread.currentThread().getName());
asyncMailTriggerObject.senMail(populateMap());

}

public void wrongWayToCall() {
System.out.println("Calling From wrongWayToCall Thread " + Thread.currentThread().getName());
AsyncMailTrigger asyncMailTriggerObject = new AsyncMailTrigger();
asyncMailTriggerObject.senMail(populateMap());
}

private Map<String,String> populateMap(){
Map<String,String> mailMap= new HashMap<String,String>();
mailMap.put("body", "A Ask2Shamik Article");
return mailMap;

}
}

Here I create two methods one use the @Autowired version of AsyncMailtrigger, which will be picked by @ComponentScan but in a WrongWayTo Call method I create the object in local so it will not be picked up by @ComponentScan hence not spawn a new thread it executed inside the main thread.
Outcome
 Calling From rightWayToCall Thread main
2019-03-09 14:08:28.893  INFO 8468 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
Trigger mail in a New Thread :: task-1
Key::body Value ::A Ask2Shamik Article
++++++++++++++++
Calling From wrongWayToCall Thread main
Trigger mail in a New Thread :: main
Key::body Value ::A Ask2Shamik Article

2. Never use @Async on top of a private method as in runtime it will not able to create a proxy, so not working.
 @Async
private void senMail() {
System.out.println("A proxy on Private method "  + Thread.currentThread().getName());
} 
3. Never writes Async method in the same class where caller method invokes the same Async method , so always remember using this reference Async not worked, because in this case,although it creates a proxy but this call bypass the proxy and call directly the method so Thread will not be spawned and developer in a wrong assumption that, it will work like async fashion, Most developers implement async in this way carelessly, so be very careful while writing Async, Caller method  should be in different class, while calling  Async method.


Example
package com.example.ask2shamik.springAsync.demo;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class AsyncCaller {
@Autowired
AsyncMailTrigger asyncMailTriggerObject;

public void rightWayToCall() {
System.out.println("Calling From rightWayToCall Thread " + Thread.currentThread().getName());
asyncMailTriggerObject.senMail(populateMap());
}
public void wrongWayToCall() {
System.out.println("Calling From wrongWayToCall Thread " + Thread.currentThread().getName());
this.senMail(populateMap());
}
private Map<String,String> populateMap(){
Map<String,String> mailMap= new HashMap<String,String>();
mailMap.put("body", "A Ask2Shamik Article");
return mailMap;

}
@Async
public void senMail(Map<String,String> properties) {
System.out.println("Trigger mail in a New Thread :: "  + Thread.currentThread().getName());
properties.forEach((K,V)->System.out.println("Key::" + K + " Value ::" + V));
}
} 

 The last piece is to execute the application, please note we use @EnableAsync annotation, by this Spring submits @Async methods in a background thread pool. This class can customize,  the used Executor by defining a new bean, I will discuss it in later with an Example How to do that.
 package com.example.ask2shamik.springAsync;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import com.example.ask2shamik.springAsync.demo.AsyncCaller;

@SpringBootApplication
@EnableAsync
public class DemoApplication {

@Autowired
AsyncCaller caller;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);

}
 @Bean
 public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {
        caller.rightWayToCall();
        Thread.sleep(1000);
        System.out.println("++++++++++++++++");
        Thread.sleep(1000);
        caller.wrongWayToCall();

        };
    }
}

Conclusion: Hope now you are able to understand How Async works internally and its limitation, now in next Article I will discuss on Exception handler in Async.