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

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:

主站蜘蛛池模板: 梧州市| 江门市| 佳木斯市| 海宁市| 罗江县| 手游| 宝应县| 神农架林区| 民勤县| 独山县| 云安县| 兴安县| 驻马店市| 重庆市| 潼南县| 宜城市| 茌平县| 泌阳县| 洮南市| 延庆县| 沙湾县| 美姑县| 安阳市| 平谷区| 东乌| 广宁县| 肇源县| 临泽县| 新巴尔虎右旗| 额尔古纳市| 子洲县| 平邑县| 城固县| 上栗县| 瑞丽市| 沙河市| 闽侯县| 炉霍县| 屯留县| 长武县| 泽州县|