NETMQ Publish Subscription Mode Publisher-subscriber

Source: Internet
Author: User

The first part is quoted in: Click to open

1: Brief Introduction

Pub-sub mode is not generally processed by the system's key data. The Publisher does not care if the Subscriber receives the published message, nor does the subscriber know if he or she has received all the messages from the publisher. You also don't know when subscribers start receiving messages. Similar to broadcasting, radio. So logically, it's not reliable. This can be solved by combining with the request response model.


Figure 1: Simple publish-Subscribe mode


Figure 2: Publish subscription mode combined with Request response mode

2: Case

Defining the Ipublishser interface

namespacenetmqdemopublisher{ Public Interfaceipublisher:idisposable {/// <summary>        ///Publish a message/// </summary>        /// <param name= "Topicname" >Theme</param>        /// <param name= "Data" >content</param>        voidPublish (stringTopicname,stringdata); }}

Publisher Implementation Class

namespacenetmqdemopublisher{ Public classPublisher:ipublisher {Private Object_lockobject =New Object(); PrivatePublishersocket _publishersocket;  PublicPublisher (stringendPoint) {_publishersocket=NewPublishersocket (); _publishersocket.options.sendhighwatermark= +;        _publishersocket.bind (EndPoint); }        #regionImplementation of IDisposable/// <summary>        ///performs an application-defined task related to releasing or resetting an unmanaged resource. /// </summary>         Public voidDispose () {Lock(_lockobject) {_publishersocket.close ();            _publishersocket.dispose (); }        }        /// <summary>        ///Publish a message/// </summary>        /// <param name= "Topicname" >Theme</param>        /// <param name= "Data" >content</param>         Public voidPublish (stringTopicname,stringdata) {            Lock(_lockobject) {_publishersocket.sendmoreframe (topicname).            Sendframe (data); }        }        #endregion    }}

Publisher window interface

function code implemented in the interface

namespacenetmqdemopublisher{ Public Partial classPublisherform:form {PrivateIpublisher Publisher;  PublicPublisherform () {InitializeComponent (); Publisher=NewPublisher ("tcp://127.0.0.1:8888"); }        Private voidButton1_Click (Objectsender, EventArgs e) {            stringStrcontent =TextBox1.Text; ListViewItem Item=NewListViewItem (string. Format ("topic:netmq,data:{0}", strcontent));            LISTVIEW1.ITEMS.ADD (item); Publisher. Publish ("NETMQ", strcontent); }    }}

Defining the Isubscriber interface

namespacenetmqdemosubscriber{ Public Interfaceisubscriber:idisposable {/// <summary>        ///Events/// </summary>        Eventaction<string,string>nofity; /// <summary>        ///sign up for a subscription topic/// </summary>        /// <param name= "Topics" ></param>        voidRegistersubscriber (list<string>topics); /// <summary>        ///sign up for subscription/// </summary>        voidRegistersbuscriberall (); /// <summary>        ///Remove all subscription messages and close/// </summary>        voidRemovesbuscriberall (); }}

Subscriber Implementation Class

namespacenetmqdemosubscriber{ Public classSubscriber:isubscriber {PrivateSubscribersocket _subscribersocket =NULL; Private string_endpoint =@"tcp://127.0.0.1:9876";  PublicSubscriber (stringendPoint) {_subscribersocket=NewSubscribersocket (); _endpoint=EndPoint; }        #regionImplementation of IDisposable/// <summary>        ///performs an application-defined task related to releasing or resetting an unmanaged resource. /// </summary>         Public voidDispose () {Throw Newnotimplementedexception (); }        #endregion        #regionImplementation of Isubscriber Public Eventaction<string,string> nofity =Delegate { }; /// <summary>        ///sign up for a subscription topic/// </summary>        /// <param name= "Topics" ></param>         Public voidRegistersubscriber (list<string>topics)        {Innerregistersubscriber (topics); }        /// <summary>        ///sign up for subscription/// </summary>         Public voidRegistersbuscriberall () {innerregistersubscriber (); }        /// <summary>        ///Remove all subscription messages and close/// </summary>         Public voidRemovesbuscriberall () {innerstop (); }        #endregion        #regionInternal implementation/// <summary>        ///sign up for a subscription message/// </summary>        /// <param name= "Topics" >Subscribe to Topics</param>        Private voidInnerregistersubscriber (list<string> topics =NULL) {innerstop (); _subscribersocket=NewSubscribersocket (); _subscribersocket.options.receivehighwatermark= +;            _subscribersocket.connect (_endpoint); if(NULL==topics)            {_subscribersocket.subscribetoanytopic (); }            Else{topics. ForEach (Item=_subscribersocket.subscribe (item)); } Task.Factory.StartNew (()=            {                 while(true)                {                    stringmessagetopicreceived =_subscribersocket.receiveframestring (); stringmessagereceived =_subscribersocket.receiveframestring ();                Nofity (messagetopicreceived, messagereceived);        }            }); }        /// <summary>        ///Close Subscription/// </summary>        Private voidInnerstop () {_subscribersocket.close (); }        #endregion    }}

Subscriber Window Interface

Form function code

namespacenetmqdemosubscriber{ Public Partial classSubscriberform:form {PrivateIsubscriber subscriber;  PublicSubscriberform () {InitializeComponent (); }        Private voidSubscriberform_load (Objectsender, EventArgs e) {Subscriber=NewSubscriber ("tcp://127.0.0.1:8888"); Subscriber.            Registersbuscriberall (); Subscriber. Nofity+=Delegate(stringSstringS1) {ListViewItem Item=NewListViewItem (string. Format ("Topic:{0},data:{1}", S, s1));            LISTVIEW1.ITEMS.ADD (item);        }; }    }}

After running, Publiser open one, Subscirber open three, for testing

SOURCE download

If you think the article is good, remember to pay attention to the public number Yo!

NETMQ Publish Subscription Mode Publisher-subscriber

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.