This document discusses asynchronous programming in Spring Framework. It begins with an introduction to asynchronous programming and its benefits. It then explains the differences between synchronous, multithreading, and asynchronous programming. It provides examples of how asynchronous programming works in Spring using annotations like @Async and @EnableAsync. It also covers best practices for asynchronous programming in Spring including exception handling. The document concludes by noting some cons of asynchronous programming and reinforcing its key benefits.
2. Contents-:
1. Introduction Of Asynchronous
2. Difference Between SYNC & MULTITHREADING & ASYNC
Programming.
3. Diagrammatic Differences
4. How Works Multithreading Programming & Asynchronous Programming
5. USE CASEs Of Asynchronous
6. Explore the asynchronous execution support in Spring Step-By-Step
7. Exception Handling In Asynchronous Programming
8. CONs Of Asynchronous Programming
9. Conclusion
3. INTRODUCTION OF ASYNCHRONOUS PROGRAMMING
1.Asynchronous programming is a form of parallel programming.
2. It allows a unit of work, to run separately from the primary thread. When the work
is complete, it notifies to the main thread.
3. It uses the same thread to process multiple requests without any request blocking the
thread.
4.Asynchronous programming has actually been around for a long time, but in recent
years, it’s become more widely used.
6.It helps to developers to
to run applications faster.
5. DIFFERENCE BETWEEN SYNC & MULTITHREADING & ASYNC
PROGRAMMING
-Sync is single-thread, means one program will run at a time.
Sync is blocking pattern, means it can send one request to the server at a time
and will wait for, that request to be answered by the server.
-Multi-Thread is multi-thread programs, But every thread has assigned seprate task.
Multithreading programming is all about concurrent execution
of different functions.
-Async is multi-thread, but programs can run in parallel, if blocking comes
so it will create seprate thread and assigned the blocked task & continue it’s
execution.
Async is non-blocking, which means it will send multiple requests to a server.
6. In Multithreading Programming
-A thread is a single continuous flow of control within a program.
-Multithreading is a technique where the processor uses multiple threads to execute
multiple processes concurrently.
-In Multithreading every thread has different tasks like
( It’s like different workers has differnet jobs)
In Asynchronous Programming
-Asynchronous programming is about the asynchronous sequence of Tasks,
while multithreading is about multiple threads running in parallel
7. USE CASE Of Asynchronous
-Asynchronous programming should only be used in programming independent tasks.
1. Responsive UI is a great use case for asynchronous programming.
E.g. In a shopping app. When a user pulls up their order, the font size should
increase. Instead of waiting to load the history and update the font size, asynchronous
programming can make both actions simultaneously.
2. One more example is, It is use to make a call to an API.
where asynchronous functions say, “Get me the data from a website, and when it
gets here, insert that & fetched data back into my script.”
8. Explore the asynchronous execution support in Spring Step-By-Step
- Here We'll explore the asynchronous execution support in Spring
with the help of @Async & @EnableAsync etc.
- Simply put this annotation on a method of a bean with @Async
then it will make it execute in a separate thread.
OR
In other words, the caller will not wait for the completion of the called method.
9. Step:1 Enable Async Support
Let's start by enabling asynchronous processing with Java configuration.
-We'll do this by adding the @EnableAsync to a configuration class:
The enable annotation is enough to provide Async support.
By default , @EnableAsync detects Spring's @Async annotation
Or
The @EnableAsync annotation switches on Spring's ability to run @Async methods in a
background thread pool.
10. Step:2 Put @Async annotation on methods
Annotating a method of a bean with @Async will make it execute in a separate thread.
But We Have Follow Some Rules
Applying @Async Annotation On Types Of Methods
1. Methods With Void Return Type
(This is the simple way to configure a method with void return type
to run asynchronously)
2. Methods With Return Type (By Wrapping The Actual Return In The Future)
11. A. Methods With Void Return Type
-Here sendTextMessage is a seprate task so spring will create seprate thread
automatically because of @Async method.
12. B. Methods With Return Type
-Here sayHello is a seprate task so spring will create seprate thread
automatically, because of @Async method & return future object, untill
getting future object first thread will keep continuous searching for result
compilition.
13. Limitations On @Async annotation
1. It must be applied to public methods only.
2. Self-invocation not allowed.
(calling the async method from within the same class won’t work.)
Reasons-:
-The reasons are simple: The method needs to be public so that it can be proxied.
-And self-invocation doesn't work because it bypasses the proxy and calls the
underlying method directly like NORMAL METHOD.
Here @Async method will not work.
14. Exception Handling
-When a method return type is a Future, exception handling is easy. Coz
Future.get() method will throw the exception called ExecutionException.
-But if the return type of method is void, exceptions will not be propagated to the
caller thread. So, we need to add extra configurations to handle exceptions.
Extra configuration are like-:
1. First we have to create a custom async exception handler by
implementing AsyncUncaughtExceptionHandler interface.
& Override The handleUncaughtException() method,
which is invoked when any uncaught asynchronous exception will come.
2. After That we have to implement AsyncConfigurer interface in the configuration
class. & We also need to override
the getAsyncUncaughtExceptionHandler() method to return our custom
asynchronous exception handler.
15. Sample Code Of Exception Handling.
Configuration 1: Implementing AsyncUncaughtExceptionHandler to handle excep.
Configuration 2: Implementing AsyncConfigurer for returning Handler
16. CONs Of Asynchronous Programming
-It requires a lot of callbacks and recursive functions which might be typical to handle
during development.
-Asynchronous scripting might be difficult to implement in some programming
languages.
-Code can get messy and difficult to debug.
17. Conclusion
-In this session, we looked running asynchronous code with Spring boot.
-We started with the very basic configuration and annotation to make it work.
-But we also looked at more advanced configurations such as providing our own
executor or exception handling strategies, etc…