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

How it works...

First, we are building a server to manage our event and observers:

public static void main(String[] args){
try(SeContainer container =
SeContainerInitializer.newInstance().initialize()){
container
.getBeanManager()
.fireEvent(new ExampleEvent("event: "
+ System.currentTimeMillis()));
}
}

This will give us all of the resources needed to run the recipe as if it were a Jakarta EE server.

Then, we build an observer, as follows:

public void thisEventBefore(
@Observes @Priority(Interceptor.Priority.APPLICATION - 200)
MyEvent event){

System.out.println("thisEventBefore: " + event.getValue());
}

So, we have three important topics:

  • @Observes: This annotation is used to tell the server that it needs to watch the events fired with MyEvent.
  • @Priority: This annotation informs in which priority order this observer needs to run; it receives an int parameter, and the execution order is ascendant.
  • MyEvent event: This is the event being observed.

In the thisEventBefore method and thisEventAfter, we only changed the @Priority value and the server took care of running it in the right order.

主站蜘蛛池模板: 青海省| 吕梁市| 乐陵市| 长汀县| 泗水县| 马山县| 石渠县| 金阳县| 北海市| 芦山县| 和平县| 建宁县| 巴林左旗| 华蓥市| 永兴县| 高唐县| 响水县| 昔阳县| 桑日县| 通榆县| 大厂| 晋江市| 彭水| 临泽县| 东兴市| 汾阳市| 随州市| 汾西县| 斗六市| 隆昌县| 张北县| 长沙市| 理塘县| 卢龙县| 张家港市| 台南市| 澜沧| 浦东新区| 宝兴县| 满城县| 兰考县|