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

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:

主站蜘蛛池模板: 剑阁县| 永德县| 霍林郭勒市| 勃利县| 丰城市| 沙湾县| 得荣县| 莱芜市| 青冈县| 吴忠市| 都江堰市| 措美县| 南宁市| 嘉定区| 随州市| 霍林郭勒市| 宜宾县| 图们市| 张家川| 蓬安县| 萨嘎县| 安新县| 太湖县| 镇远县| 渭南市| 红河县| 开平市| 凯里市| 宁阳县| 波密县| 日照市| 来安县| 互助| 永靖县| 墨脱县| 卫辉市| 富裕县| 延吉市| 临猗县| 新疆| 斗六市|