幸福框架:发布订阅模式 之 同步订阅、异步订阅和离线订阅
2013-05-21 06:43
483 查看
背景
事件驱动解除了发布者和订阅者之间的耦合,在UI层面,我明经常采用这种编程理念。服务器端最近也开始流行起来了,我也一直小范围的在采用。今天就跟大家分享一下我写的一个小框架。框架原理
一张图片胜过前言万语。
代码示例
下载地址:http://yunpan.cn/Q5SUcWdiA2mmk。
项目结构
关键代码
TestEvent.csusing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Happy.Event; using Happy.Event.Offline; namespace Happy.Event.Demo.Simple { [TestEventInterceptor1Attibute(1)] [TestEventInterceptor2Attibute(2)] [Persistable(3)] internal sealed class TestEvent : IEvent { } }
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using Microsoft.Practices.ServiceLocation; using Microsoft.Practices.Unity; using Happy.Event; using Happy.Event.Offline; namespace Happy.Event.Demo.Simple { class Program { static readonly string _QueueName = "Happy.Event.Demo.Simple"; static void Main(string[] args) { InitUnity(); var writeQueue = OfflineEventQueueFactory.CreateMSMQOfflineEventQueue(_QueueName); EventPublisher.Current.AddService(writeQueue); EventPublisher.Current.Publish(new TestEvent()); new Thread(() => { var readQueue = OfflineEventQueueFactory.CreateMSMQOfflineEventQueue(_QueueName); var offlineEventProcessor = new OfflineEventProcessor(readQueue); offlineEventProcessor.Start(); }) .Start(); Console.ReadLine(); } static void InitUnity() { var container = new UnityContainer(); ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container)); container.RegisterType<ISyncEventSubscriber<TestEvent>, TestSyncEventSubscriber1>("TestSyncEventSubscriber1"); container.RegisterType<ISyncEventSubscriber<TestEvent>, TestSyncEventSubscriber2>("TestSyncEventSubscriber2"); container.RegisterType<IAsyncEventSubscriber<TestEvent>, TestAsyncEventSubscriber1>("TestAsyncEventSubscriber1"); container.RegisterType<IAsyncEventSubscriber<TestEvent>, TestAsyncEventSubscriber2>("TestAsyncEventSubscriber2"); container.RegisterType<IOfflineEventSubscriber<TestEvent>, TestOfflineEventSubscriber1>("TestOfflineEventSubscriber1"); container.RegisterType<IOfflineEventSubscriber<TestEvent>, TestOfflineEventSubscriber2>("TestOfflineEventSubscriber2"); } } }
执行结果
备注
基于事件的编程,我在真实项目中有过实战的,这个框架目前还没在项目中使用。研发先行。相关文章推荐
- 发布订阅模式 之 同步订阅、异步订阅和离线订阅
- “一切都是消息”--MSF(消息服务框架)之【发布-订阅】模式
- “一切都是消息”--MSF(消息服务框架)之【发布-订阅】模式
- 分布式消息总线,基于.NET Socket Tcp的发布-订阅框架之离线支持,附代码下载
- 验证通知是同步还是异步执行....据代码验证,通知其实是一个同步执行的过程,按照通知中心发布通知,然后订阅通知,接收通知step by step
- “一切都是消息”--MSF(消息服务框架)之【发布-订阅】模式
- 分布式服务框架学习笔记8 ActiveMQ入门2 发布/订阅模式
- zeroMQ初体验-31.发布/订阅模式进阶-黑盒的高速订阅者
- SqlServer2008 数据库同步的两种方式 (发布、订阅)
- 在服务器端采用同步处理模式和异步处理模式的分析
- RabbitMQ 使用 | 第三篇:发布/订阅模式
- [SQL Server] 使用事务发布与订阅实现数据同步
- SqlServer2008 数据库同步的两种方式 (发布、订阅)
- SQL Server 2008 数据库同步的两种方式 (发布、订阅)
- 数据库同步的两种方式(发布、订阅使用方法
- 发布-订阅模式(观察者模式)
- js事件编程的发布/订阅模式(一对一关系)
- Dojo的订阅/发布模式
- Redis订阅和发布模式和Redis事务
- SQL Server 复制 - 发布订阅(SQL Server 数据同步)