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

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.

主站蜘蛛池模板: 古蔺县| 靖宇县| 江门市| 中江县| 文山县| 嘉定区| 平潭县| 乳源| 页游| 行唐县| 庄浪县| 许昌市| 嘉善县| 深泽县| 安庆市| 天祝| 西昌市| 吴桥县| 墨竹工卡县| 新田县| 肇庆市| 左权县| 桂林市| 闻喜县| 信丰县| 凭祥市| 静海县| 德阳市| 德钦县| 丰原市| 扎赉特旗| 贵溪市| 永城市| 宁德市| 大邑县| 张北县| 刚察县| 南江县| 林芝县| 隆安县| 廉江市|