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

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

问题描述

虽然简单,Java之前的界面驱动的事件通知框架已经存在于寒武纪之前(例如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

以下示例来自 EventBus入门指南。基于已发布事件进行更新的状态栏,无需注册状态栏控件/窗口小部件作为发布者的监听器。没有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(Event Listener Framework),但似乎不太成熟。

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

我正在研究关于发布订阅事件驱动编程的事件通知框架| Kev的Spring vs Java EE Dev 和后续文章。

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天全站免登陆