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

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

问题描述

虽然 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.

You can already do this today with 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(事件监听器框架),但它似乎不太成熟.

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