JEE6如何在观察者和被观察者之间建立联系(观察者模式)

发布于 2024-09-16 19:39:40 字数 1112 浏览 4 评论 0原文

我读过一些关于 JEE6 上的观察者模式实现的博客文章,有些事情困扰着我...我找不到任何信息 atm,所以我在那里问...

我发现了以下示例:

@Stateless
[...]
public class ParisJugService {

   @Inject
   Event event;

   public void helloParis(){
        System.out.println("Hello Paris");
        event.fire("hello Paris invoked!");
   }
}

@Stateless
public class EventReceiver {

    public void onHelloParis(@Observes String message){
        System.out.println("----------- " + message);
    }
}

public class MyEvent {
    String data;
    Date eventTime;
    ....

}

public class EventProducer {

  @Inject @Any Event<MyEvent> event;


  public void doSomething() {
       MyEvent e=new MyEvent();
      e.data="This is a test event";
      e.eventTime=new Date();
      event.fire(e);

  }

}


public class EventConsumer {


    public void afterMyEvent(@Observes MyEvent event) {

        // .. Insert event logic here
    }

}

不明白如何事件消费者和事件生产者之间的链接已完成...

是通过方法的命名约定吗? (其中“on”、“after”、“before”等词有意义)

是通过比较给定的参数(此处为 String 和 MyEvent)吗?

我不知道也看不出还有什么可能......

I've read some blog articles about Observer pattern implementation on JEE6 and something bother me... I can't find any information atm so i ask there...

I've found the following exemples:

@Stateless
[...]
public class ParisJugService {

   @Inject
   Event event;

   public void helloParis(){
        System.out.println("Hello Paris");
        event.fire("hello Paris invoked!");
   }
}

@Stateless
public class EventReceiver {

    public void onHelloParis(@Observes String message){
        System.out.println("----------- " + message);
    }
}

And

public class MyEvent {
    String data;
    Date eventTime;
    ....

}

public class EventProducer {

  @Inject @Any Event<MyEvent> event;


  public void doSomething() {
       MyEvent e=new MyEvent();
      e.data="This is a test event";
      e.eventTime=new Date();
      event.fire(e);

  }

}


public class EventConsumer {


    public void afterMyEvent(@Observes MyEvent event) {

        // .. Insert event logic here
    }

}

I can't understand how the link between event consumer and event producer is done...

Is it by naming convention of the methods? (Where the words "on", "after", "before"... will have sense)

Is it by comparison of given arguments (here String and MyEvent)?

I can't know and don't see what else could it be...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等待我真够勒 2024-09-23 19:39:40

容器保存有关其中所有 bean 的元数据。启动时,它将所有观察者方法及其观察到的事件类型(参数的类)一起注册。每当触发事件时,都会检查观察者列表中是否有接受适当类型事件的方法。

The container keeps metadata about all beans inside it. On startup it registers all observer methods together with the event type (the Class of the argument) they observe. Whenever an event is fired, the list of observers is checked for methods that accept the appropriate type of event.

蓝海似她心 2024-09-23 19:39:40

事件触发和侦听器之间的链接基于事件类型。

在您的示例中:

public class EventConsumer {

    public void afterMyEvent(@Observes MyEvent event) {
        // .. Insert event logic here
    }

}

此类将接收 MyEvent 类型的所有触发事件,无论它们来自何处。

来源:http://download-llnw。 oracle.com/javaee/6/api/javax/enterprise/event/Observes.html

The link between the event firing and the listener is based on the event type.

In your example:

public class EventConsumer {

    public void afterMyEvent(@Observes MyEvent event) {
        // .. Insert event logic here
    }

}

This class will receive all fired events of type MyEvent, wherever they come from.

Source: http://download-llnw.oracle.com/javaee/6/api/javax/enterprise/event/Observes.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文