- Java EE 8 and Angular
- Prashant Padmanabhan
- 229字
- 2021-07-02 19:22:35
Asynchronous events
These were introduced as part of CDI 2.0, and allow for raising events in an asynchronous way. The earlier version 1 had only synchronous event firing support, leading to the event firing code having to wait for all the observers to finish execution, and only then could the next line of code execute. It also meant that an exception in one observer would prevent other observers from getting called. This was because all the observer calls were made within the same thread.
With CDI 2, there's support for asynchronous observer invocation. The observers will be running in different threads than the thread which fires the event. Owing to the nature of different threads, the async events won't allow mutation of the payload. None of the CDI built-in contexts will be propagated across these threads, other than the Application context which is shared across. Async event firing is achieved by using the fireAsync method on the Event interface and is observed by using the new @ObservesAsync annotation. The fireAsync method returns a CompletionStage instance that was introduced in Java 8, allowing for the handling of exceptions which are made available after a completion of calls in a suppressed manner.
Having understood how synchronous and asynchronous events behave, let's look at a summary of the difference between the two methods, provided by the Event interface, namely fire and fireAsync:

- Deploying Node.js
- Android 9 Development Cookbook(Third Edition)
- 機械工程師Python編程:入門、實戰與進階
- INSTANT OpenNMS Starter
- 小學生C++創意編程(視頻教學版)
- Visual C++開發入行真功夫
- 大數據分析與應用實戰:統計機器學習之數據導向編程
- TMS320LF240x芯片原理、設計及應用
- SciPy Recipes
- Clojure for Java Developers
- Python網絡爬蟲實例教程(視頻講解版)
- 區塊鏈:技術與場景
- 3D Printing Designs:Design an SD Card Holder
- MATLAB語言及編程實踐:生物數學模型應用
- Fast Data Processing with Spark 2(Third Edition)