Asynchronous it’s mean not Synchronous, ok it was a lame joke I’m sorry. When we’re talking about asynchronous it’s more like you’re delegate your work to another reliable man and don’t need to wait and supervise him, you can move to doing another task and when this man done with his work he will tell you. In technical explanation your work or in this case we call it task, was proceed on the different time table, it’s not depends with another task, and mostly was delegate into another thread. The simpler explanation between the different of asynchronous and synchronous.
Synchronous: you’re in queue to enter a parking lot which of course have ticketing system, you can’t get in before every car in front of you pick their ticket and then start to move in. This is also apply on queue behind you.
Asynchronous: you’re on your favorite coffee shop which is very popular and always full of people.You can order your favorite coffee on the same time with another customer and you don’t have to wait your coffee to be brewed and served before another customer order it’s done. The coffee shop workers will continuously take order, brew coffee and serving. People will get their coffee as soon as it’s ready.
Asynchronous with Java
Java was already support asynchronous programming with
Future that represent pending result from asynchronous process and offers method
get which is return the result of computation from asynchronous process. But, there was a little problem,
get method will cause an blocking until the computation was done and it’s make the asynchronous in java was pointless.
Then, here comes the savior of asynchronous in Java 8, beside a hot stuff like Lambda Expression and Stream, Java 8 also shipped a cool stuff and the name is
CompletableFuture. Not only implemented
Future to perform asynchronous it’s also implements
CompletionStage which is a promise, so with
CompletableFuture you can attach a callback. For example the code for
CompletableFuture is like this:
CompletableFuture support multi-chaining callbacks and support lambdas and of course stream, but actually the one that I want to show you today not about this hot stuff rather than an old API but it’s still powerful and magnificent, it was Asynchronous annotations.
Events was one of the shiniest feature on CDI specifications which is straight forward implementation of Observer Design Pattern yet easy to understand. Events CDI add more decoupling to the Dependency Injection by allowing notification of unknown target at development time. Events allow beans to communicate without any compile-time dependency. One bean can define an event, another bean can fire the event, and yet another bean can handle the event. The beans can be in separate packages and even in separate tiers of the application. Bellow is example how to fire an events:
Then to observer an events you will have to add Observer annotations in an enable bean of your application.
However this CDI have some limitations, which is it can’t be ordered and it’s not asynchronous. Out of the box events are synchronous method.
Combine Asynchronous with Events CDI
I told you before beside hot stuff like
CompletableFuture there is asynchronous annotations which is actually a
Future. We can combine this with Events CDI to make it perform an asynchronous. So, let’s start write some code. You will build a simple application with upload file feature asynchronously. So, the user don’t need to wait until the file uploaded and it’s need to show a error message or success message as soon as the file was submit to be uploaded. This application will be build with Java EE 7 (JSF, CDI, EJB) and Wildfly Application Server.
Create Upload Page with JSF
You will use one of the most popular web component in JavaEE, Java Server Faces (JSF). In order to made JSF work properly first we need to configure the servlet in
web.xml like this.
Then you can create your index.html which consist of upload file form and submit button.
Then don’t forget to create your ManagedBean class.
For uploading files, we can used
Copy interface, so in your submit method it will be like this.
Create EJB Listener
The next steps was build a listener with Observer annotations, this class will listen when the events was fired. Don’t forget to add Asynchronous annotations in your method to make the Event CDI asynchronously and move the logic of uploading file from ManagedBean class into this listener class.
I made the file that uploaded will stored in Wildfly Application Server with folder name uploads. So, you can freely change where to store the file but I suggest to keep it on the same directory of the Application Server.
Next, you need to refactor your ManagedBean class to have an Events CDI. This will be responsible in fire the event into your Observable class.
Congratulations, you’re already build an asynchronous upload files with Events CDI. Actually you can use
CompletableFuture as well, because Asynchronous annotations it’s just a
Future and it’s already shipped on since Java EE 6, it’s a little bit old right? The main reason why I choose this method was because
CompletableFuture is will be powerful if you’re combine it with lambdas, but in this case for uploading files it will be cumbersome because you need to catch many IO exception and it made your code a little bit hard to read. So, the full source code was on github and this time for you to explore many possibility to do Asynchronous with Java.