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

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.

主站蜘蛛池模板: 缙云县| 额济纳旗| 上栗县| 云南省| 平顺县| 灵武市| 铜陵市| 洞口县| 玉山县| 阳高县| 清徐县| 榕江县| 城固县| 玉溪市| 邹城市| 乌苏市| 仙游县| 竹溪县| 三河市| 勃利县| 星座| 芷江| 佛山市| 通城县| 泌阳县| 濮阳县| 宜宾市| 重庆市| 天峨县| 修文县| 卢氏县| 陆良县| 衡南县| 张家港市| 宁津县| 稻城县| 蓝田县| 定襄县| 手机| 广州市| 孝义市|