public void OpenSubscription_OK()
 {
     using (var broker = MQQueueManager.Connect(QueueManagerName, 0, Channel, ConnectionInfo))
     {
         var subs = new MQSubscription(broker);
         subs.Subscribe(
             "MY.SUBS.T1",
             MQC.MQSO_CREATE + MQC.MQSO_RESUME + MQC.MQSO_NON_DURABLE + MQC.MQSO_MANAGED,
             TopicName);
         Assert.AreEqual(true, subs.IsOpen);
         subs.Close(MQC.MQCO_REMOVE_SUB, closeSubQueue: false, closeSubQueueOptions: MQC.MQCO_NONE);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 订阅主题。订阅一次并获取消息
        /// </summary>
        /// <returns></returns>
        private string SubTopic(string business)
        {
            string         message = string.Empty;
            MQSubscription subs    = null;

            try
            {
                using (var mqmgr = MQQueueManager.Connect(IBMWMQConfig.QUEUE_MGR_NAME, MQC.MQCO_NONE, IBMWMQConfig.CHANNEL, IBMWMQConfig.CONNECTION_INFO))
                {
                    subs = new MQSubscription(mqmgr);
                    if (mqmgr.IsConnected)
                    {
                        int    option    = MQC.MQSO_CREATE + MQC.MQSO_RESUME + MQC.MQSO_NON_DURABLE + MQC.MQSO_MANAGED + MQC.MQSO_FAIL_IF_QUIESCING;
                        string subName   = string.Format(IBMWMQConfig.SUBSCRIPTION_TEMPLATE, business);
                        string topicName = string.Format(IBMWMQConfig.TOPIC_TEMPLATE, business);

                        subs.Subscribe(subName, option, topicName);

                        MQMessage           incoming = new MQMessage();
                        MQGetMessageOptions gmo      = new MQGetMessageOptions();
                        gmo.WaitInterval = 10 * 1000; //MQC.MQWI_UNLIMITED;
                        gmo.Options     |= MQC.MQGMO_WAIT;
                        gmo.Options     |= MQC.MQGMO_SYNCPOINT;

                        subs.Get(incoming, gmo);
                        message = incoming.ReadAll();

                        this.TryAdd(business, subs);
                    }
                }
            }
            catch (MQException e)
            {
                message = e.Reason.ToString();
            }
            finally
            {
                if (subs != null)
                {
                    subs.Close(MQC.MQCO_REMOVE_SUB, closeSubQueue: true, closeSubQueueOptions: MQC.MQCO_NONE);
                }
            }
            return(message);
        }
        public void SubscriptionGet_OK()
        {
            var message = "PublishInSubscription";

            using (var broker = MQQueueManager.Connect(QueueManagerName, 0, Channel, ConnectionInfo))
            {
                var subs = new MQSubscription(broker);
                subs.Subscribe(
                    "MY.SUBS.T1",
                    MQC.MQSO_CREATE + MQC.MQSO_RESUME + MQC.MQSO_NON_DURABLE + MQC.MQSO_MANAGED,
                    SubscriptionTopicName);


                using (var topic = new MQTopic(broker, SubscriptionTopicName, string.Empty, MQC.MQTOPIC_OPEN_AS_PUBLICATION, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING))
                {
                    var outgoing = new MQMessage()
                    {
                        CharacterSet = MQC.CODESET_UTF,
                        Encoding     = MQC.MQENC_NORMAL
                    };
                    outgoing.WriteString(message);
                    topic.Publish(outgoing, new MQPutMessageOptions());
                }


                var incoming            = new MQMessage();
                MQGetMessageOptions gmo = new MQGetMessageOptions();
                gmo.WaitInterval = (int)TimeSpan.FromSeconds(30).TotalMilliseconds; //MQC.MQWI_UNLIMITED;
                gmo.Options     |= MQC.MQGMO_WAIT;
                gmo.Options     |= MQC.MQGMO_SYNCPOINT;

                subs.Get(incoming, gmo);
                Assert.AreEqual(message, incoming.ReadAll());

                subs.Close(MQC.MQCO_REMOVE_SUB, closeSubQueue: true, closeSubQueueOptions: MQC.MQCO_NONE);
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// 添加或者更新
 /// </summary>
 /// <param name="key"></param>
 /// <param name="subscription"></param>
 private void TryAdd(string key, MQSubscription subscription)
 {
     mqSubscriptions.AddOrUpdate(key, subscription, (k, v) => subscription);
 }
Exemplo n.º 5
0
        /// <summary>
        /// 订阅主题。订阅一次并尝试一直获取消息
        /// </summary>
        public void SubTopic1(string business, bool isGetMsg = true)
        {
            IBMWMQEventSource      eventSource      = new IBMWMQEventSource();
            IBMWMQMsgEventListener msgEventListener = new IBMWMQMsgEventListener(_msgHandler);
            MQSubscription         subs             = null;

            try
            {
                //订阅事件
                msgEventListener.Subscribe(eventSource);
                //MQEnvironment.CCSID = 1381;

                using (var mqmgr = MQQueueManager.Connect(IBMWMQConfig.QUEUE_MGR_NAME, MQC.MQCO_NONE, IBMWMQConfig.CHANNEL, IBMWMQConfig.CONNECTION_INFO))
                {
                    subs = new MQSubscription(mqmgr);
                    if (mqmgr.IsConnected)
                    {
                        this.TryAdd(business, subs);

                        int    option    = MQC.MQSO_CREATE + MQC.MQSO_RESUME + MQC.MQSO_NON_DURABLE + MQC.MQSO_MANAGED + MQC.MQSO_FAIL_IF_QUIESCING;
                        string subName   = string.Format(IBMWMQConfig.SUBSCRIPTION_TEMPLATE, business);
                        string topicName = string.Format(IBMWMQConfig.TOPIC_TEMPLATE, business);

                        try
                        {
                            subs.Subscribe(subName, option, topicName);
                        }
                        catch (MQException ex)
                        {
                            string code = ex.Reason.ToString();
                            //引发事件
                            eventSource.RaiseErroeMsgEvent(business, code);
                        }

                        while (isGetMsg)
                        {
                            eventSource.RaiseErroeMsgEvent(business, string.Format("开始尝试获取 {0} 消息...", business));
                            try
                            {
                                MQMessage incoming = new MQMessage()
                                {
                                    CharacterSet = MQC.CODESET_UTF,
                                    Encoding     = MQC.MQENC_NORMAL
                                };
                                MQGetMessageOptions gmo = new MQGetMessageOptions();
                                gmo.WaitInterval = 10 * 1000; //MQC.MQWI_UNLIMITED;
                                gmo.Options     |= MQC.MQGMO_WAIT;
                                gmo.Options     |= MQC.MQGMO_SYNCPOINT;

                                subs.Get(incoming, gmo);
                                string message = incoming.ReadAll();

                                if (!string.IsNullOrEmpty(message))
                                {
                                    //引发事件
                                    eventSource.RaiseNewMsgEvent(business, message);
                                }
                            }
                            catch (MQException ex)
                            {
                                string code = ex.Reason.ToString();
                                //引发事件
                                eventSource.RaiseErroeMsgEvent(business, code);
                            }
                        }
                    }
                }
            }
            catch (MQException e)
            {
                string code = e.Reason.ToString();
                //引发事件
                eventSource.RaiseErroeMsgEvent(business, code);
            }
            finally
            {
                //if (subs != null)
                //{
                //    subs.Close(MQC.MQCO_REMOVE_SUB, closeSubQueue: true, closeSubQueueOptions: MQC.MQCO_NONE);
                //}
            }
        }