使用C#和IBM MQSeries进行消息发布订阅(二)

 

使用C#和IBM MQSeries进行消息发布订阅(二)


开发环境:MQSeries7.0 Visual studio 2008

2011-8-19 创建

C#发布订阅的程序主要参考C:\Program Files\IBM\WebSphere MQ\tools\dotnet\samples\cs下的MQPubSubSample.cs,但总觉得IBM提供的发布订阅的例子不是很好,因为通常情况下我们的发布和订阅程序是两个部分,而在例子中将发布和订阅混杂在一起,以至于搞不清楚哪些选项用于发布,哪些用于订阅上。因此写了两个简化的函数,以便在C#中更好的使用MQ7发布订阅功能。

实际MQ7实现的发布订阅很简单,例如在托管队列的情况下,发布者需要提供的信息包括
1,主题
2,队列管理器
3,发布内容

订阅者需要提供
1,主题
2,订阅名称(持久订阅情况)
3,队列管理器

然后发布者的流程为
1,连接队列管理器
2,创建发布的消息
3,以发布的方式调用队列管理器的PUT方法,将消息发布


订阅者相对复杂一些
1,连接队列管理器
2,创建订阅,在创建订阅时选择持久订阅还是非持久订阅
3,创建订阅用的消息
4,通过订阅的GET方法取得消息


相关代码如下:

在使用MQ客户端的情况下,首先初始化MQ环境
 public void initMQ()
        {
            Set up WebSphere MQ environment
            MQEnvironment.Hostname = "10.52.22.24"; // server
            MQEnvironment.Channel = "QCH.ACC.001";  // server channel
            MQEnvironment.Port = 1501;    //port number
 }

发布功能
  public void Publish(String topicName, String qmName, String publishStr)
  {  
             initMQ();
           
            int destType = MQC.MQOT_TOPIC;
            string topicObject = null;
           
            MQQueueManager mqQMgr = null;

            try
            {
                //Create a connection to the queue manager
                mqQMgr = new MQQueueManager(qmName);

                //Publish
                MQMessage messageForPut = new MQMessage();
               
                messageForPut.WriteString(publishStr);
                mqQMgr.Put(destType, topicObject, null, topicName, messageForPut);
                MessageBox.Show("Publish message is: " + publishStr);

                //Disconnect from the queue manager
                mqQMgr.Disconnect();
            }
            catch (MQException mqEx)
            {
                MessageBox.Show("MQException caught. " + mqEx.Message);
            }
            finally
            {
                //Disconnect from the queue manager
                if (mqQMgr != null)
                    mqQMgr.Disconnect();
            }
 }

订阅功能(托管,持久)
        public void Subscribe(String topicName, String qmName, String subName)
        {
            initMQ();
           
            string topicObject = null;
            MQQueueManager mqQMgr = null;
            MQTopic topic = null;

            try
            {
                //Create a connection to the queue manager
                mqQMgr = new MQQueueManager(qmName);

                //Create durable subscribe
                int openOptionsForGet = MQC.MQSO_CREATE | MQC.MQSO_RESUME | MQC.MQSO_FAIL_IF_QUIESCING | MQC.MQSO_MANAGED | MQC.MQSO_DURABLE;
                topic = mqQMgr.AccessTopic(topicName, topicObject, openOptionsForGet, null, subName);

                MQMessage messageForGet = new MQMessage();
                topic.Get(messageForGet);
                String messageDataFromGet = messageForGet.ReadLine();
                MessageBox.Show("Subscribe message is: " + messageDataFromGet);

            }
            catch (MQException mqEx)
            {
                if (mqEx.ReasonCode == 2033)
                    MessageBox.Show("No publish on topic " + topicName);
                else
                    MessageBox.Show("MQException caught. " + mqEx.Message);
            }
            finally
            {
                //Close subscribe
                if (topic != null)
                    topic.Close();

                //Disconnect from the queue manager
                if (mqQMgr != null)
                    mqQMgr.Disconnect();

            }
        }

使用示例:
            String pubStr = "IPHONE5 released at " + DateTime.Now.ToString();
            Publish("APPLE", "QM_001", pubStr);
            Subscribe("APPLE", "QM_001", "SUB_1");

在首先订阅注册后,MQ生成持久的订阅,名称为SUB_1,此后所有的发布通过打开该订阅都可以接收到,无论订阅方断开与否。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 C# WinForm IBM MQ 消息队列监听代码示例: ```csharp using IBM.WMQ; using System; using System.Threading.Tasks; using System.Windows.Forms; namespace IBM_MQ_Listener { public partial class Form1 : Form { private MQQueueManager queueManager; private MQQueue queue; private MQMessage message; private MQGetMessageOptions options; public Form1() { InitializeComponent(); } private async void btnStart_Click(object sender, EventArgs e) { try { // 设置 IBM MQ 连接信息 var properties = new Hashtable(); properties.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED); properties.Add(MQC.HOST_NAME_PROPERTY, "your.mq.server.ip"); properties.Add(MQC.CHANNEL_PROPERTY, "your.mq.channel"); properties.Add(MQC.PORT_PROPERTY, your.mq.server.port); properties.Add(MQC.USER_ID_PROPERTY, "your.mq.user"); properties.Add(MQC.PASSWORD_PROPERTY, "your.mq.password"); // 连接 IBM MQ 队列管理器 queueManager = new MQQueueManager("your.mq.queue.manager.name", properties); // 打开监听的队列 queue = queueManager.AccessQueue("your.mq.queue.name", MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING); // 初始化消息和获取选项 message = new MQMessage(); options = new MQGetMessageOptions(); // 启动监听循环 await Task.Run(() => { while (true) { try { queue.Get(message, options); // 处理接收到的消息 string msgText = message.ReadString(message.MessageLength); Invoke(new Action(() => { // 在 UI 线程中更新 UI txtReceivedMsg.AppendText(msgText + Environment.NewLine); })); } catch (MQException ex) { if (ex.ReasonCode == MQC.MQRC_NO_MSG_AVAILABLE) { // 没有消息可用,继续循环 continue; } else { // 其他错误,退出循环 throw ex; } } } }); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void btnStop_Click(object sender, EventArgs e) { try { // 关闭队列 if (queue != null) { queue.Close(); } // 断开队列管理器连接 if (queueManager != null) { queueManager.Disconnect(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } ``` 在这个示例中,我们使用 IBM.WMQ 库来连接 IBM MQ 队列管理器,打开监听的队列,接收并处理消息。需要注意的是,IBM.WMQ 库需要安装 IBM MQ 客户端才能正常使用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值