通用,注解驱动的事件通知框架 [英] Generic, annotation-driven event notification frameworks

查看:144
本文介绍了通用,注解驱动的事件通知框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

而在Java简单,界面驱动的事件通知框架已经出现自pre-寒武纪时期(例如java.beans.PropertyChangeSupport),它正变得越来越流行的框架使用注解驱动的事件通知来代替。

While simple, interface-driven event notification frameworks in Java have been around since pre-Cambrian times (e.g. java.beans.PropertyChangeSupport), it is becoming increasingly popular for frameworks to use annotation-driven event notification instead.

有关示例,请参阅JBossCache 2.2 。监听器类有其监听注释的方法,而不是符合刚性接口。这是相当容易编程,以及更易于阅读,因为你没有写,你是不感兴趣的侦听器回调的空实现(是的,我知道侦听器适配器超)。

For an example, see JBossCache 2.2. The listener class has its listener methods annotated, rather than conforming to a rigid interface. This is rather easier to program to, and easier to read, since you don't have to write empty implementations of listener callbacks that you're not interested in (and yes, I know about listener adapter superclasses).

下面是从JBossCache的文档的例子:

Here's a sample from the JBossCache docs:

@CacheListener
public class MyListener {
   @CacheStarted
   @CacheStopped
   public void cacheStartStopEvent(Event e) {
         switch (e.getType()) {
            case Event.Type.CACHE_STARTED:
               System.out.println("Cache has started");
               break;    
            case Event.Type.CACHE_STOPPED:    
               System.out.println("Cache has stopped");
               break;    
         }
   }    

   @NodeCreated    
   @NodeRemoved
   @NodeVisited
   @NodeModified
   @NodeMoved
   public void logNodeEvent(NodeEvent ne) {
         log("An event on node " + ne.getFqn() + " has occured");
   }

}

这里的问题是,这是非常多的复杂过程编写框架来支持这样的事情,因为它的注解反射性。

The problem with this, is that it's very much more of an involved process writing the framework to support this sort of thing, due to the annotation-reflection nature of it.

所以,在我关机充电下来写一个通用框架的道路,我希望有人已经这样做了。有没有人碰到这样的事?

So, before I charge off down the road of writing a generic framework, I was hoping someone had done it already. Has anyone come across such a thing?

推荐答案

您已经可以做到这一点今天 EventBus

You can already do this today with EventBus.

下面的例子是从 EventBus入门指南。状态栏,更新的基础上发布的事件,而无需注册StatusBar控件/插件作为发布者(S)的监听器。如果没有EventBus,状态栏将需要添加为监听到很多类。状态栏也可以创建和销毁在任何时间。

Following example is from EventBus Getting Started guide. Statusbar that updates based on published events, and no need to register statusbar control/widget as listener of publisher(s). Without EventBus, statusbar will need to be added as listener to many classes. Statusbar can also be created and destroyed at any time.

public StatusBar extends JLabel {
    public StatusBar() {
        AnnotationProcessor.process(this);
    }
    @EventSubscriber(eventClass=StatusEvent.class)
    public void updateStatus(StatusEvent statusEvent) {
        this.setText(statusEvent.getStatusText();
    }
}

一个类似的项目是 ELF(事件监听器框架),但它似乎是不太成熟。

A similar project is ELF (Event Listener Framework) but it seems to be less mature.

我目前正在研究有关事件通知框架上的Publish-Subscribe事件驱动编程|千电子伏的春季VS的Java EE开发和后续文章。

I'm currently researching about event notification frameworks on Publish-Subscribe Event Driven Programming | Kev's Spring vs Java EE Dev and the followup articles.

这篇关于通用,注解驱动的事件通知框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆