Use C # And IBM MQSeries for message publishing and subscription

Source: Internet
Author: User
Tags mqseries sub command

Use C # And IBM MQSeries for message publishing and subscription

Create
The reservation information will be deleted when the reservation is closed after the reservation is made

MQ provides an mqpubsubsample that uses C # To publish and subscribe to the routine. CS: first, two important options are explained. The combination of these two options affects the topic opening mode and message subscription and receiving in the publishing and subscription processes.

1. Managed/unmanaged (managed/unmanaged ):
The managed dynamic queue uses MQ to store subscription information, which is not stored in the queue specified by the user.

2. Durable/undurable ):
A message subscription can be divided into non-durable subscribe and durable subscrip-tion. A non-durable subscription can only be performed when the client is active, that is, the message can be received only when it is connected to the MQ queue manager.

Messages sent to a topic are lost and never received when the client is offline. During persistent subscription, the client registers an ID that identifies itself with the MQ queue manager. when the client is offline, the MQ queue manager saves all messages sent to the topic for this ID, when the customer connects again, all the messages sent to the topic when they are offline are obtained based on their own ID. The default value is non-persistent.
According to the MQ documentation, subscription will be cleared only in the following four cases.
1. Connection loss with a non-durable
2, mqclose with mqco_remove_sub
3, administrative delete sub command
4. The subscription expires. The subscription period is specified when the subscription is set.

Therefore, in general, a non-persistent subscription can obtain all the information published during the connection period, and a persistent subscription can obtain all the information published between the registered subscription and the canceled subscription. The persistence is different from the message persistence, so it is irrelevant to the publisher.

 

The following code illustrates how different options can be opened.

Int openoptionsforget = MQC. mqso_create | MQC. mqso_fail_if_quiescing | MQC. mqso_managed | MQC. mqso_non_durable; string topicname = "bomstatus"; string topicobject = NULL; int desttype = MQC. mqot_topics; string subname = "Wuhan _" + "sample1"; mqtopic destforget = NULL; mqdestination unmanageddest = NULL; string unmanageddestname = default_queue; int unmanageddestopenoptions = MQC. mqoo_input_exclusive | MQC. mqoo_fail_if_quiescing; // switch flag to test bool managed = true; bool durable = true; If (managed & durable) // managed persistence {openoptionsforget = MQC. mqso_create | MQC. mqso_resume | MQC. mqso_fail_if_quiescing | MQC. mqso_managed | MQC. mqso_durable; subname = "Wuhan _" + "sample1"; destforget = mqqmgr. accesstopic (topicname, topicobject, openoptionsforget, null, subname); destforget. subscriptionrefe Rence. closeoptions = MQC. mqco_remove_sub;} else if (! Managed & durable) // unmanaged persistence {openoptionsforget = MQC. mqso_create | MQC. mqso_fail_if_quiescing | MQC. mqso_durable; subname = "Wuhan _" + "sample2"; unmanageddestname = default_queue; unmanageddestopenoptions = MQC. mqoo_input_exclusive | MQC. mqoo_fail_if_quiescing; unmanageddest = mqqmgr. accessqueue (unmanageddestname, unmanageddestopenoptions); destforget = mqqmgr. accesstopic (unmanageddest, topicname, Topicobject, openoptionsforget, null, subname); destforget. subscriptionreference. closeoptions = MQC. mqco_remove_sub;} else if (managed &&! Durable) // hosting non-persistent {openoptionsforget = MQC. mqso_create | MQC. mqso_fail_if_quiescing | MQC. mqso_managed | MQC. mqso_non_durable; destforget = mqqmgr. accesstopic (topicname, topicobject, MQC. mqtopic_open_as_subget, openoptionsforget);} else if (! Managed &&! Durable) // unmanaged non-persistent {openoptionsforget = MQC. mqso_create | MQC. mqso_fail_if_quiescing | MQC. mqso_non_durable; unmanageddestname = default_queue; unmanageddestopenoptions = MQC. mqoo_input_exclusive | MQC. mqoo_fail_if_quiescing; unmanageddest = mqqmgr. accessqueue (unmanageddestname, unmanageddestopenoptions); destforget = mqqmgr. accesstopic (unmanageddest, topicname, topicobject, openoptionsforget );}

After setting these parameters, you can start publishing and subscribing.

// Publish messageforput = new mqmessage (); string publishstr = "publish test data at" + datetime. now. tostring (); messageforput. writestring (publishstr); mqqmgr. put (desttype, topicobject, null, topicname, messageforput); MessageBox. show ("Publish message success, message is:" + publishstr); // subscribe to try {messageforget = new mqmessage (); destforget. get (messageforget); string messagedatafromget = messageforget. readline (); MessageBox. show ("subscribe message is:" + messagedatafromget);} catch (mqexception mqe) {MessageBox. show ("mqexception caught. "+ mqe. message );}

 

After durability is used, a predetermined record appears in the MQ Resource Manager statement, as shown in:
 

When using the MQ routine, the program reports the mqrc_sub_already_exists (Reason code 2432) error of the topic when the topic (accesstopic) is enabled for the second time in a persistent scenario)
You must use the MQC. mqso_create | MQC. mqso_resume option to avoid this error.

You can use the following use cases to experience the difference between persistence and non-persistence.
1. Set durable to false, publish a new message, and continuously enable connections to receive subscriptions. The published message should be available.
2. Set durable to false to publish a new message. After the connection is closed, the subscription is received and the published message cannot be received.
3. Set durable to true to publish a new message. After the connection is closed, connect again with the same sub to receive the subscription. The published message should be available.

 

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.