当前位置: 首页 > news >正文

Fundamental Components Of An Event-Driven Archi...

为什么80%的码农都做不了架构师?>>>   hot3.png

Fundamental Components Of An Event-Driven Architecture

http://www.giocc.com/fundamental-components-of-an-event-driven-architecture.html

published:  April 9, 2012

My favorite type of architecture is event-driven because of the trivial train of thought as action-reaction. Consider what happens when an incoming tornado approaches; what do we do? Notify the locals. Consider what happens when an online purchase occurs; what do we do? Ship it. Consider what happens when an event occurs; what do we do? Handle it.

No Less Fundamental Than These Components

An event-driven architecture comprises of a simple set of components to run yet it can represent a variety of situations and how to handle them.

EventsAn occurrence to handle.Event HandlersA method to handle an occurence.Event LoopMaintains the flow of interaction between events and event handlers.

The Flow of Events

The flow of events is straightforward: events are sent to the event loop and the event loop dispatches each event to their respective event handler.

Dispatching Events to Handlers

Dispatching Events to Handlers

Consequently, events of type A will be handled by Handler A and events of type B will be handled by Handler B.

Interestingly, the flow of events is often modeled through simple language just as the introduction to this article does. Abstractly, all events and their handling can be described the following manner: if event p occurs, then execute q.

Events

The heart of an event-driven architecture.

Events contain two necessary properties: type and data. The type of the event determines which handlers handle the event. The data of the event is for the handler to use.

Conceptually, we can conceive an event object as a container for data which contains meta data such as the event type to determine how to handle the data.

We can then model a simple event in Java:

public static class Event { public char type; public String data; public Event(char type, String data) { this.type = type; this.data = data;
    }
}

Event Handlers

Handlers are specific ways to deal with an event. Common operations include filtering or transforming the data associated with the event.

public static void printEventA(Event e) {
    System.out.println(e.data);
}

The example code above is a handler for events of type A. Simply, the code prints the data of the event to the console.

public static void printEventB(Event e) {
    System.out.println(e.data.toUpperCase());
}

This second example transforms the event data to upper case before the data appears in the console.

Event Loop

This processes all events as they arrive by dispatching them to their respective handlers until a terminating condition occurs.

public static void main(String[] args) {
    Queue<Event> events = new LinkedList<Event>();
    events.add(new Event('A', "Hello"));
    events.add(new Event('B', "event-driven"));
    events.add(new Event('A', "world!"));

    Event e; while (!events.isEmpty()) {
        e = events.remove(); if (e.type == 'A')
            printEventA(e); if (e.type == 'B')
            printEventB(e);
    }
}

Before initializing our event loop, we create a Queue of event objects to schedule a set of events for processing so that we can see some output. Consequently, the event loop here begins at the while-loop. Yes, the event loop is frequently a loop.

In the event loop, each event is removed from the queue and handled according to their type until there are no more events to process. In total, we should process the three events in order of which they were added. Consider how the first event is handled:

Path for Handling Event A

Path for Handling Event A

It’s easy to see now that the events are simply data that are passed down a specific path towards a handler. Consider the first event processed: the event is passed into the event loop where it is appropriately routed to Handler A.

Putting It All Together

In this Java code example, I’ve created concrete implementations of a simple simulation of events through an event-driven architecture.

  • Events are defined by the Event class.
  • Event handlers are methods (printEventA and printEventB) which accept events as parameters.
  • The event loop is implemented within the main method which passes schedules events into their respective event handlers.
import java.util.LinkedList; import java.util.Queue; public class EventMachine { // Event definition public static class Event { public char type; public String data; public Event(char type, String data) { this.type = type; this.data = data;
        }
    } // Event handler A public static void printEventA(Event e) {
        System.out.println(e.data);
    } // Event handler B public static void printEventB(Event e) {
        System.out.println(e.data.toUpperCase());
    } public static void main(String[] args) {
        Queue<Event> events = new LinkedList<Event>();
        events.add(new Event('A', "Hello"));
        events.add(new Event('B', "event-driven"));
        events.add(new Event('A', "world!")); // Event loop Event e; while (!events.isEmpty()) {
            e = events.remove(); if (e.type == 'A')
                printEventA(e); if (e.type == 'B')
                printEventB(e);
        }
    }
}

The following output should then be:

Hello
EVENT-DRIVEN
world!

Extending the Fundamentals

The implementation of these components are not limited to this tutorial. Event handlers can pass the events to further handlers and event loops can route events to their respective handler using a map data structure for matching handlers in logarithmic time instead of linear time as used in this tutorial.

Next time, I’ll show you how to create an event-driven framework in Java with some design patterns.

转载于:https://my.oschina.net/dongwq/blog/171444

相关文章:

  • 移动周报:十款最实用的Android UI设计工具
  • implicit declaration of function 'copy_from_user'
  • 灯标技术--向服务器发送信息而不需要服务器返回信息
  • 虚基类虚继承
  • 导出无法正常启动的VMware虚拟机中的文件
  • CentOS 下的邮件通知
  • Linux内核--usb子系统的分析
  • 安装db2 提示不是有效的win32应用程序?
  • 建站须知
  • Java中如何实现类似C++结构体的二级排序
  • 防暴力破解 Fail2Ban之python
  • JMETER 2.10的安装
  • 缓存 - 运行时内存缓存
  • lvs群集-nat模型
  • 原创D3D几何实例化的DEMO
  • 【5+】跨webview多页面 触发事件(二)
  • 5分钟即可掌握的前端高效利器:JavaScript 策略模式
  • Docker下部署自己的LNMP工作环境
  • eclipse的离线汉化
  • es的写入过程
  • ES学习笔记(12)--Symbol
  • linux安装openssl、swoole等扩展的具体步骤
  • Linux快速配置 VIM 实现语法高亮 补全 缩进等功能
  • MYSQL 的 IF 函数
  • Vue ES6 Jade Scss Webpack Gulp
  • Vue2.x学习三:事件处理生命周期钩子
  • 融云开发漫谈:你是否了解Go语言并发编程的第一要义?
  • 深度学习中的信息论知识详解
  • 实战:基于Spring Boot快速开发RESTful风格API接口
  • 温故知新之javascript面向对象
  • 详解NodeJs流之一
  • 项目管理碎碎念系列之一:干系人管理
  • 验证码识别技术——15分钟带你突破各种复杂不定长验证码
  • ​MySQL主从复制一致性检测
  • ​虚拟化系列介绍(十)
  • # 执行时间 统计mysql_一文说尽 MySQL 优化原理
  • (16)Reactor的测试——响应式Spring的道法术器
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (附源码)springboot家庭财务分析系统 毕业设计641323
  • (附源码)计算机毕业设计ssm基于B_S的汽车售后服务管理系统
  • (一)Dubbo快速入门、介绍、使用
  • (已解决)vue+element-ui实现个人中心,仿照原神
  • (转)memcache、redis缓存
  • .Family_物联网
  • .jks文件(JAVA KeyStore)
  • .NET 3.0 Framework已经被添加到WindowUpdate
  • .net 4.0发布后不能正常显示图片问题
  • .NET BackgroundWorker
  • .NET Core MongoDB数据仓储和工作单元模式封装
  • .Net Core和.Net Standard直观理解
  • .Net 中Partitioner static与dynamic的性能对比
  • .net6+aspose.words导出word并转pdf
  • .NetCore项目nginx发布
  • .NET开发人员必知的八个网站
  • .net下的富文本编辑器FCKeditor的配置方法