官术网_书友最值得收藏!

  • 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:

主站蜘蛛池模板: 县级市| 钟祥市| 尖扎县| 屯留县| 霍城县| 京山县| 黑山县| 兴安县| 泰来县| 萨嘎县| 苍梧县| 凌海市| 定南县| 酉阳| 屯留县| 布尔津县| 禄丰县| 清徐县| 土默特左旗| 海阳市| 耒阳市| 安塞县| 西吉县| 安国市| 平谷区| 襄汾县| 阿巴嘎旗| 胶南市| 长泰县| 木兰县| 雷州市| 横峰县| 宝山区| 门头沟区| 吴旗县| 南雄市| 萝北县| 顺义区| 嘉禾县| 南涧| 金堂县|