Remoting and MSMQ 结合做的一个DEMO
生活随笔
收集整理的這篇文章主要介紹了
Remoting and MSMQ 结合做的一个DEMO
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
真的非常感謝CnBlogs.com,因為有很多教程里寫的知識點并不全面,而且也沒附帶例子.通過這里我學習到了很多很多.希望網站能越辦越好哦.
下面是我學習分布式開發時寫的一個小demo,當然功能有限,技術也很低.希望大師們能拍拍磚,不吝賜教.呵呵
軟件運行的一個截圖
下面說說流程吧,
1:利用public static object CreateInstance(Type type, object[] args, object[] activationAttributes);方法創建一個類(并激活自定義的構造函數)
2:利用MSMQ異步接收事件參數來觸發remoting的事件
大約就是這樣的里程,不過看上去蠻簡單的,可里面用到很多MSMQ和remoting事件的知識點哦..下面是一段在remoting里面調用的dll原代碼
Code
using?System;
using?System.Collections.Generic;
using?System.Text;
using?System.Messaging;?//此空間必須引用Mssaging空間才能訪問哦
namespace?MyRemotingAndMsmq
{
????/*
?????*關于Remoting和MSMQ系列的文章博客園里有很多
?????*而且介紹的又很全面,在此我也就不具體的講解了。
?????*在此篇博客里我主要是針對Remoting和MSMQ結合的一些問題做個demo.
?????*并在重點處做個標記,本文并不全文注釋,如有任何疑問請發表看法,
?????*歡迎大家互相學習,切磋。
?????*此程序具體流程很簡單,如下解釋
?????*一:利用一個Class定義個串行化類,并傳給Remoting,Remoting接收MSMQ
?????*?里的參數,并在客戶端用Remoting事件觸發來接收內容。其實真實過程中
?????*?并不需要這么多的流程,只是本文是用來闡述這兩種方法結合使用的例子,所以
?????*?很多此一舉,呵呵。。。。
????*/
????public?class?MyContext?:?MarshalByRefObject??//此類用來虛擬實例化事件參數類的類
????{
????????private?string?name?=?null;
????????private?string?address?=?null;
????????private?string?message?=?null;
????????public?MyContext(string?name,?string?address,?string?message)?//此構造函數將用客戶端激活模式來激活
????????{
????????????this.name?=?name;
????????????this.address?=?address;
????????????this.message?=?message;
????????}
????????public?string?Name
????????{
????????????get?{?return?name;?}
????????????set?{?name?=?value;?}
????????}
????????public?string?Address
????????{
????????????get?{?return?address;?}
????????????set?{?address?=?value;?}
????????}
????????public?string?Message
????????{
????????????get?{?return?message;?}
????????????set?{?message?=?value;?}
????????}
????}
????[Serializable]??//此類是用來
????public?class?MyEvent:EventArgs
????{
????????private?string?name?=?null;
????????private?string?address?=?null;
????????private?string?message?=?null;
????????public?MyEvent(string?name,?string?address,?string?message)?//此構造函數將用客戶端激活模式來激活
????????{
????????????this.name?=?name;
????????????this.address?=?address;
????????????this.message?=?message;
????????}
????????public?string?Name
????????{
????????????get?{?return?name;?}
????????????set?{?name?=?value;?}
????????}
????????public?string?Address
????????{
????????????get?{?return?address;?}
????????????set?{?address?=?value;?}
????????}
????????public?string?Message
????????{
????????????get?{?return?message;?}
????????????set?{?message?=?value;?}
????????}
????}
????public?class?MyMsmq?:?MarshalByRefObject??//因為需要傳動MyMsmq類,所以需要編組
????{
????????public?string?context?="觸發事件";??//因為此字段需要在類外部訪問,所以需要定義public級別
????????string?name?=?@".\private$\my";
????????MessageQueue?mm?=?null;
????????public?MyContext?mycontext?=?null;
????????public?void?Shu()
????????{
????????????if?(MessageQueue.Exists(name))
????????????{
????????????????mm?=?new?MessageQueue(name);
????????????}
????????????else
????????????{
????????????????mm?=?MessageQueue.Create(name);
????????????}
????????????context?=?mycontext.Name?+?","?+?mycontext.Address?+?","?+?mycontext.Message;
????????????System.Messaging.Message?m?=?new?System.Messaging.Message();
????????????m.Label?=?"異步消息發送";
????????????m.Body=context;
????????????mm.Send(m);
????????????mm.ReceiveCompleted?+=?new?ReceiveCompletedEventHandler(mm_ReceiveCompleted);?//定義異步接收的事件
????????????mm.BeginReceive();
????????}
????????void?mm_ReceiveCompleted(object?sender,?ReceiveCompletedEventArgs?e)
????????{
????????????MessageQueue?mq?=?(MessageQueue)sender;
????????????System.Messaging.Message?m?=?mq.EndReceive(e.AsyncResult);
????????????m.Formatter?=?new?XmlMessageFormatter(new?Type[]?{?typeof(string)?});
????????????context?=?m.Body.ToString();//改變context
????????????mq.BeginReceive();//接收下一次事件?
????????}
????}
????public?class?MyRemoting?:?MarshalByRefObject
????{
????????public?string?context;
????????string[]?contexts;
????????public?delegate?void?MyDeledate(object?sender,?MyEvent?e);
????????public?event?MyDeledate?MyHandle;
????????MyEvent?e?=?null;
????????public?MyMsmq?mm?=?null;
????????public?void?Shu()
????????{
????????????if?((mm.context?!=?"觸發事件"))??//這里代表MSMQ異步接收事件成功
????????????{
????????????????contexts?=?mm.context.Split(new?char[]{?','});
????????????????if?(contexts.Length?<?2?&&?contexts.Length?>?0)
????????????????{
????????????????????e?=?new?MyEvent(contexts[0].ToString(),?contexts[1].ToString(),?"自定義參數");
????????????????????MyHandle(this,?e);
????????????????}
????????????????if?(contexts.Length?>?1)
????????????????{
????????????????????e?=?new?MyEvent(contexts[0].ToString(),?contexts[1].ToString(),?contexts[2].ToString());
????????????????????MyHandle(this,?e);
????????????????}
????????????????
????????????}
????????}
????}
}
服務器端需要創建3個遠程對象,typeof(MyContext),typeof(MyMsmq),typeof(MyRemoting).代碼如下
Code
????private?void?Form1_Load(object?sender,?EventArgs?e)
????????{
????????????RemotingConfiguration.ApplicationName?=?"remotingandmsmq";?//客戶激活模式需要的URI需要在此定義
????????????BinaryServerFormatterSinkProvider?bk?=?new?BinaryServerFormatterSinkProvider();
????????????BinaryClientFormatterSinkProvider?ck?=?new?BinaryClientFormatterSinkProvider();
????????????bk.TypeFilterLevel?=?TypeFilterLevel.Full;
????????????Dictionary<string,?string>?wo?=?new?Dictionary<string,?string>();
????????????wo["port"]?=?"8086";
????????????TcpChannel?tcp?=?new?TcpChannel(wo,?ck,?bk);//因為TcpServerChannl不支持安全性,所以用TcpChannel
????????????ChannelServices.RegisterChannel(tcp);
????????????RemotingConfiguration.RegisterActivatedServiceType(typeof(MyContext));
????????????RemotingConfiguration.RegisterActivatedServiceType(typeof(MyMsmq));
????????????RemotingConfiguration.RegisterActivatedServiceType(typeof(MyRemoting));
????????}原代碼下載這只是一個空的winform服務器,目的是在等待客戶端調用.
客戶端調用如下
Code
???private?void?button1_Click(object?sender,?EventArgs?e)
????????{
????????????//RemotingConfiguration.RegisterActivatedClientType(typeof(MyContext),?"tcp://localhost:8086/remotingandmsmq");
????????????object[]?obj?={?new?UrlAttribute("tcp://localhost:8086/remotingandmsmq")?};
????????????object[]?objs?=?new?object[3];
????????????objs[0]?=?"小徐";
????????????objs[1]?=?"揚州";
????????????objs[2]?=?"能不能觸發了";
????????????MyContext?mm?=?(MyContext)Activator.CreateInstance(typeof(MyContext),?objs,?obj);
????????????//以上利用public?static?object?CreateInstance(Type?type,?object[]?args,?object[]?activationAttributes);
????????????//來完成類MyContext的構造函數
????????????RemotingConfiguration.RegisterActivatedClientType(typeof(MyMsmq),?"tcp://localhost:8086/remotingandmsmq");
????????????MyMsmq?my?=?new?MyMsmq();
????????????my.mycontext?=?mm;
????????????my.Shu();
????????????RemotingConfiguration.RegisterActivatedClientType(typeof(MyRemoting),?"tcp://localhost:8086/remotingandmsmq");
????????????MyRemoting?mym?=?new?MyRemoting();
????????????mym.mm?=?my;
????????????//MessageBox.Show(mym.mm.context.ToString());
????????????ClassLibrary1.Class1?c?=?new?ClassLibrary1.Class1();
????????????mym.MyHandle?+=?new?MyRemoting.MyDeledate(c.StatusHandler);
????????????mym.Shu();
????????}
????????void?mym_MyHandle(object?sender,?MyEvent?e)
????????{
????????????throw?new?Exception("The?method?or?operation?is?not?implemented.");
????????}
????????private?void?Form1_Load(object?sender,?EventArgs?e)
????????{
????????????BinaryServerFormatterSinkProvider?serverProvider?=?new?BinaryServerFormatterSinkProvider();
????????????BinaryClientFormatterSinkProvider?clientProvider?=?new?BinaryClientFormatterSinkProvider();
????????????serverProvider.TypeFilterLevel?=?TypeFilterLevel.Full;
????????????Dictionary<string,?string>?wo?=?new?Dictionary<string,?string>();
????????????wo["port"]?=?"0";
????????????TcpChannel?channel?=?new?TcpChannel(wo,?clientProvider,?serverProvider);
????????????ChannelServices.RegisterChannel(channel);
????????}網站里關于這兩個話題的博客很多,在這里我就不多加解釋了,網友們自行查閱資料.重點地方我都做下注釋了......
下面是我學習分布式開發時寫的一個小demo,當然功能有限,技術也很低.希望大師們能拍拍磚,不吝賜教.呵呵
軟件運行的一個截圖
下面說說流程吧,
1:利用public static object CreateInstance(Type type, object[] args, object[] activationAttributes);方法創建一個類(并激活自定義的構造函數)
2:利用MSMQ異步接收事件參數來觸發remoting的事件
大約就是這樣的里程,不過看上去蠻簡單的,可里面用到很多MSMQ和remoting事件的知識點哦..下面是一段在remoting里面調用的dll原代碼
Code
using?System;
using?System.Collections.Generic;
using?System.Text;
using?System.Messaging;?//此空間必須引用Mssaging空間才能訪問哦
namespace?MyRemotingAndMsmq
{
????/*
?????*關于Remoting和MSMQ系列的文章博客園里有很多
?????*而且介紹的又很全面,在此我也就不具體的講解了。
?????*在此篇博客里我主要是針對Remoting和MSMQ結合的一些問題做個demo.
?????*并在重點處做個標記,本文并不全文注釋,如有任何疑問請發表看法,
?????*歡迎大家互相學習,切磋。
?????*此程序具體流程很簡單,如下解釋
?????*一:利用一個Class定義個串行化類,并傳給Remoting,Remoting接收MSMQ
?????*?里的參數,并在客戶端用Remoting事件觸發來接收內容。其實真實過程中
?????*?并不需要這么多的流程,只是本文是用來闡述這兩種方法結合使用的例子,所以
?????*?很多此一舉,呵呵。。。。
????*/
????public?class?MyContext?:?MarshalByRefObject??//此類用來虛擬實例化事件參數類的類
????{
????????private?string?name?=?null;
????????private?string?address?=?null;
????????private?string?message?=?null;
????????public?MyContext(string?name,?string?address,?string?message)?//此構造函數將用客戶端激活模式來激活
????????{
????????????this.name?=?name;
????????????this.address?=?address;
????????????this.message?=?message;
????????}
????????public?string?Name
????????{
????????????get?{?return?name;?}
????????????set?{?name?=?value;?}
????????}
????????public?string?Address
????????{
????????????get?{?return?address;?}
????????????set?{?address?=?value;?}
????????}
????????public?string?Message
????????{
????????????get?{?return?message;?}
????????????set?{?message?=?value;?}
????????}
????}
????[Serializable]??//此類是用來
????public?class?MyEvent:EventArgs
????{
????????private?string?name?=?null;
????????private?string?address?=?null;
????????private?string?message?=?null;
????????public?MyEvent(string?name,?string?address,?string?message)?//此構造函數將用客戶端激活模式來激活
????????{
????????????this.name?=?name;
????????????this.address?=?address;
????????????this.message?=?message;
????????}
????????public?string?Name
????????{
????????????get?{?return?name;?}
????????????set?{?name?=?value;?}
????????}
????????public?string?Address
????????{
????????????get?{?return?address;?}
????????????set?{?address?=?value;?}
????????}
????????public?string?Message
????????{
????????????get?{?return?message;?}
????????????set?{?message?=?value;?}
????????}
????}
????public?class?MyMsmq?:?MarshalByRefObject??//因為需要傳動MyMsmq類,所以需要編組
????{
????????public?string?context?="觸發事件";??//因為此字段需要在類外部訪問,所以需要定義public級別
????????string?name?=?@".\private$\my";
????????MessageQueue?mm?=?null;
????????public?MyContext?mycontext?=?null;
????????public?void?Shu()
????????{
????????????if?(MessageQueue.Exists(name))
????????????{
????????????????mm?=?new?MessageQueue(name);
????????????}
????????????else
????????????{
????????????????mm?=?MessageQueue.Create(name);
????????????}
????????????context?=?mycontext.Name?+?","?+?mycontext.Address?+?","?+?mycontext.Message;
????????????System.Messaging.Message?m?=?new?System.Messaging.Message();
????????????m.Label?=?"異步消息發送";
????????????m.Body=context;
????????????mm.Send(m);
????????????mm.ReceiveCompleted?+=?new?ReceiveCompletedEventHandler(mm_ReceiveCompleted);?//定義異步接收的事件
????????????mm.BeginReceive();
????????}
????????void?mm_ReceiveCompleted(object?sender,?ReceiveCompletedEventArgs?e)
????????{
????????????MessageQueue?mq?=?(MessageQueue)sender;
????????????System.Messaging.Message?m?=?mq.EndReceive(e.AsyncResult);
????????????m.Formatter?=?new?XmlMessageFormatter(new?Type[]?{?typeof(string)?});
????????????context?=?m.Body.ToString();//改變context
????????????mq.BeginReceive();//接收下一次事件?
????????}
????}
????public?class?MyRemoting?:?MarshalByRefObject
????{
????????public?string?context;
????????string[]?contexts;
????????public?delegate?void?MyDeledate(object?sender,?MyEvent?e);
????????public?event?MyDeledate?MyHandle;
????????MyEvent?e?=?null;
????????public?MyMsmq?mm?=?null;
????????public?void?Shu()
????????{
????????????if?((mm.context?!=?"觸發事件"))??//這里代表MSMQ異步接收事件成功
????????????{
????????????????contexts?=?mm.context.Split(new?char[]{?','});
????????????????if?(contexts.Length?<?2?&&?contexts.Length?>?0)
????????????????{
????????????????????e?=?new?MyEvent(contexts[0].ToString(),?contexts[1].ToString(),?"自定義參數");
????????????????????MyHandle(this,?e);
????????????????}
????????????????if?(contexts.Length?>?1)
????????????????{
????????????????????e?=?new?MyEvent(contexts[0].ToString(),?contexts[1].ToString(),?contexts[2].ToString());
????????????????????MyHandle(this,?e);
????????????????}
????????????????
????????????}
????????}
????}
}
服務器端需要創建3個遠程對象,typeof(MyContext),typeof(MyMsmq),typeof(MyRemoting).代碼如下
Code
????private?void?Form1_Load(object?sender,?EventArgs?e)
????????{
????????????RemotingConfiguration.ApplicationName?=?"remotingandmsmq";?//客戶激活模式需要的URI需要在此定義
????????????BinaryServerFormatterSinkProvider?bk?=?new?BinaryServerFormatterSinkProvider();
????????????BinaryClientFormatterSinkProvider?ck?=?new?BinaryClientFormatterSinkProvider();
????????????bk.TypeFilterLevel?=?TypeFilterLevel.Full;
????????????Dictionary<string,?string>?wo?=?new?Dictionary<string,?string>();
????????????wo["port"]?=?"8086";
????????????TcpChannel?tcp?=?new?TcpChannel(wo,?ck,?bk);//因為TcpServerChannl不支持安全性,所以用TcpChannel
????????????ChannelServices.RegisterChannel(tcp);
????????????RemotingConfiguration.RegisterActivatedServiceType(typeof(MyContext));
????????????RemotingConfiguration.RegisterActivatedServiceType(typeof(MyMsmq));
????????????RemotingConfiguration.RegisterActivatedServiceType(typeof(MyRemoting));
????????}原代碼下載這只是一個空的winform服務器,目的是在等待客戶端調用.
客戶端調用如下
Code
???private?void?button1_Click(object?sender,?EventArgs?e)
????????{
????????????//RemotingConfiguration.RegisterActivatedClientType(typeof(MyContext),?"tcp://localhost:8086/remotingandmsmq");
????????????object[]?obj?={?new?UrlAttribute("tcp://localhost:8086/remotingandmsmq")?};
????????????object[]?objs?=?new?object[3];
????????????objs[0]?=?"小徐";
????????????objs[1]?=?"揚州";
????????????objs[2]?=?"能不能觸發了";
????????????MyContext?mm?=?(MyContext)Activator.CreateInstance(typeof(MyContext),?objs,?obj);
????????????//以上利用public?static?object?CreateInstance(Type?type,?object[]?args,?object[]?activationAttributes);
????????????//來完成類MyContext的構造函數
????????????RemotingConfiguration.RegisterActivatedClientType(typeof(MyMsmq),?"tcp://localhost:8086/remotingandmsmq");
????????????MyMsmq?my?=?new?MyMsmq();
????????????my.mycontext?=?mm;
????????????my.Shu();
????????????RemotingConfiguration.RegisterActivatedClientType(typeof(MyRemoting),?"tcp://localhost:8086/remotingandmsmq");
????????????MyRemoting?mym?=?new?MyRemoting();
????????????mym.mm?=?my;
????????????//MessageBox.Show(mym.mm.context.ToString());
????????????ClassLibrary1.Class1?c?=?new?ClassLibrary1.Class1();
????????????mym.MyHandle?+=?new?MyRemoting.MyDeledate(c.StatusHandler);
????????????mym.Shu();
????????}
????????void?mym_MyHandle(object?sender,?MyEvent?e)
????????{
????????????throw?new?Exception("The?method?or?operation?is?not?implemented.");
????????}
????????private?void?Form1_Load(object?sender,?EventArgs?e)
????????{
????????????BinaryServerFormatterSinkProvider?serverProvider?=?new?BinaryServerFormatterSinkProvider();
????????????BinaryClientFormatterSinkProvider?clientProvider?=?new?BinaryClientFormatterSinkProvider();
????????????serverProvider.TypeFilterLevel?=?TypeFilterLevel.Full;
????????????Dictionary<string,?string>?wo?=?new?Dictionary<string,?string>();
????????????wo["port"]?=?"0";
????????????TcpChannel?channel?=?new?TcpChannel(wo,?clientProvider,?serverProvider);
????????????ChannelServices.RegisterChannel(channel);
????????}網站里關于這兩個話題的博客很多,在這里我就不多加解釋了,網友們自行查閱資料.重點地方我都做下注釋了......
轉載于:https://www.cnblogs.com/xuting/archive/2009/07/27/1532069.html
總結
以上是生活随笔為你收集整理的Remoting and MSMQ 结合做的一个DEMO的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java的OOP编程思想
- 下一篇: 计算机基础应用的培养活动记录,计算机应用