Git Product home page Git Product logo

eventdispatcher's Introduction

事件分发器/EventDispatcher

Java实现的事件分发器,有任何疑问可以在当前仓库下给我提issue。具体的分析请参考我的博客:事件分发器

An implementation of event dispatcher in Java,if you have any question, please propose issues to me in this repo. For the detailed explainations, please refer to my blog article, here is the reference link: Event dispatcher

使用方法

事件类型

如下面的代码所示, 我们预先定义了三个事件类型, EVENT_NONE, EVENT_TIMEREVENT_TEST, 需要新的事件, 则可以在在EventTypeEnum中添加.

public enum EventTypeEnum {
  EVENT_NONE("noneEvent"),
  EVENT_TIMER("timer"),
  EVENT_TEST("test");

  String value;

  EventTypeEnum(String value) {
    this.value = value;
  }
}

订阅者

订阅者可以是任意一个成员方法, 默认事件类型是NONE, 如果需要订阅具体的事件类型,那就需要在注解OnEvent中设定特定的事件类型参数. 可以参考如下代码.

public class Subscriber {

  @OnEvent(eventType = EventTypeEnum.EVENT_TIMER)
  private void getTimer(Event event) {
    System.out.println(Thread.currentThread() 
        + "==" + System.currentTimeMillis() 
        +  " -- Current time:" + event.getData());
  }

  @OnEvent(eventType = EventTypeEnum.EVENT_TEST)
  private void processTest(Event event) throws InterruptedException {
    System.out.println(Thread.currentThread() 
        + "==" + System.currentTimeMillis() 
        + " : s"  + event.getData());
    TimeUnit.SECONDS.sleep(10L);
  }
}

在一个类中可以有多个订阅事件的成员方法, 不受限制.

发布者(分发器)

发布者则是一个分发器, 在设计原理上可以参考我在文章开头所展示的博文。

  1. 需要有订阅对象
  2. 分发器让订阅对象订阅事件,否则订阅行为不生效
  3. 启动分发器

如下代码所示,Subscriber同时订阅了EVENT_TIMEREVENT_TEST事件,同时Subscriber在处理EVENT_TEST是阻塞的,但是这并不影响EVENT_TIMER事件的分发,因此事件的分发是非阻塞的。但是需要注意的一点是, 因为订阅者实际是类中的成员方法,方法之间存在对成员变量的修改时,需要注意并发问题。

  public static void main(String[] args) throws InterruptedException {

    Subscriber subscriber = new Subscriber();
    EventDispatcher eventDispatcher = new EventDispatcher();
    eventDispatcher.subscribe(EventTypeEnum.EVENT_TIMER, subscriber);
    eventDispatcher.subscribe(EventTypeEnum.EVENT_TEST, subscriber);
    eventDispatcher.start();
    while (true) {
      TimeUnit.SECONDS.sleep(3);
      eventDispatcher.putEvent(new Event(EventTypeEnum.EVENT_TEST, "Block code test"));
    }
  }
Thread[ForkJoinPool.commonPool-worker-1,5,main]==1590769005160 -- Current time:Sat May 30 00:16:45 CST 2020
Thread[ForkJoinPool.commonPool-worker-1,5,main]==1590769006142 -- Current time:Sat May 30 00:16:46 CST 2020
Thread[ForkJoinPool.commonPool-worker-1,5,main]==1590769007141 : sBlock code test
Thread[ForkJoinPool.commonPool-worker-2,5,main]==1590769007142 -- Current time:Sat May 30 00:16:47 CST 2020
Thread[ForkJoinPool.commonPool-worker-2,5,main]==1590769008147 -- Current time:Sat May 30 00:16:48 CST 2020
Thread[ForkJoinPool.commonPool-worker-2,5,main]==1590769009148 -- Current time:Sat May 30 00:16:49 CST 2020
Thread[ForkJoinPool.commonPool-worker-2,5,main]==1590769010146 : sBlock code test
Thread[ForkJoinPool.commonPool-worker-3,5,main]==1590769010152 -- Current time:Sat May 30 00:16:50 CST 2020
Thread[ForkJoinPool.commonPool-worker-3,5,main]==1590769011152 -- Current time:Sat May 30 00:16:51 CST 2020
Thread[ForkJoinPool.commonPool-worker-3,5,main]==1590769012152 -- Current time:Sat May 30 00:16:52 CST 2020
Thread[ForkJoinPool.commonPool-worker-3,5,main]==1590769013151 : sBlock code test
Thread[ForkJoinPool.commonPool-worker-4,5,main]==1590769013156 -- Current time:Sat May 30 00:16:53 CST 2020
Thread[ForkJoinPool.commonPool-worker-4,5,main]==1590769014161 -- Current time:Sat May 30 00:16:54 CST 2020
Thread[ForkJoinPool.commonPool-worker-4,5,main]==1590769015166 -- Current time:Sat May 30 00:16:55 CST 2020
Thread[ForkJoinPool.commonPool-worker-4,5,main]==1590769016152 : sBlock code test
Thread[ForkJoinPool.commonPool-worker-5,5,main]==1590769016169 -- Current time:Sat May 30 00:16:56 CST 2020
Thread[ForkJoinPool.commonPool-worker-1,5,main]==1590769017169 -- Current time:Sat May 30 00:16:57 CST 2020
Thread[ForkJoinPool.commonPool-worker-1,5,main]==1590769018172 -- Current time:Sat May 30 00:16:58 CST 2020
Thread[ForkJoinPool.commonPool-worker-1,5,main]==1590769019152 : sBlock code test
Thread[ForkJoinPool.commonPool-worker-5,5,main]==1590769019175 -- Current time:Sat May 30 00:16:59 CST 2020
Thread[ForkJoinPool.commonPool-worker-2,5,main]==1590769020178 -- Current time:Sat May 30 00:17:00 CST 2020
Thread[ForkJoinPool.commonPool-worker-2,5,main]==1590769021176 -- Current time:Sat May 30 00:17:01 CST 2020
Thread[ForkJoinPool.commonPool-worker-2,5,main]==1590769022153 : sBlock code test

eventdispatcher's People

Contributors

southernyard avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Forkers

stevenchen1976

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.