JEE6如何在观察者和被观察者之间建立联系(观察者模式)
我读过一些关于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
容器保存有关其中所有 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.事件触发和侦听器之间的链接基于事件类型。
在您的示例中:
此类将接收 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:
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