1. 程式人生 > >MSMQ客戶端程式碼(Oracle service bus)

MSMQ客戶端程式碼(Oracle service bus)

今天做了一個MSMQ客戶端(伺服器是OSB)來幫助專案上找問題,程式碼很簡單,發現MQ協議也很方便呢。

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

using WebLogic.Messaging;

namespace jmsclient
{
    public partial class Form1 : Form
    {
        private string host = "10.18.105.40";
        private int port = 8201;
        private string cfName = "LESJMSCF";
        private string queueName = "VehicleOrderQueueIn";


        public IConnection connection = null;
        public IContext context = null;
        public IQueue queue = null;


        public Form1()
        {
            InitializeComponent();

            /*
            //10秒一次操作執行緒
            Control.CheckForIllegalCrossThreadCalls = false;
            Thread m1Thread = new Thread(new ThreadStart(m1));
            m1Thread.IsBackground = true;
            m1Thread.Start();
            */

            IDictionary<string, Object> paramMap = new Dictionary<string, Object>();
            paramMap[Constants.Context.PROVIDER_URL] = "10.18.105.40://" + this.host + ":" + this.port;
             context = ContextFactory.CreateContext(paramMap);

            IConnectionFactory cf = context.LookupConnectionFactory(this.cfName);
             queue = (IQueue)context.LookupDestination(this.queueName);
             connection = cf.CreateConnection();

            connection.Start();
        }





        private void m1()
        {

            IDictionary<string, Object> paramMap = new Dictionary<string, Object>();
            paramMap[Constants.Context.PROVIDER_URL] = "10.18.105.10://" + this.host + ":" + this.port;
            IContext context = ContextFactory.CreateContext(paramMap);

            IConnectionFactory cf = context.LookupConnectionFactory(this.cfName);
            IQueue queue = (IQueue)context.LookupDestination(this.queueName);
            IConnection connection = cf.CreateConnection();


            connection.Start();

            int total = 0;

            while (true)
            {
                Thread.Sleep(10 * 1000);

                string now = System.DateTime.Now.ToShortTimeString();     //得到現在的時間 

                ISession session = connection.CreateSession(Constants.SessionMode.AUTO_ACKNOWLEDGE);
                IMessageProducer producer = session.CreateProducer(queue);
                producer.DeliveryMode = Constants.DeliveryMode.PERSISTENT;
                ITextMessage sendMessage = session.CreateTextMessage("james's message   " + now );

                producer.Send(sendMessage);
                total++;
                label1.Text = "傳送訊息總數:"+ total.ToString();



            }

           // connection.Close();
           // context.CloseAll();

        }

        private void button1_Click(object sender, EventArgs e)
        { 


            ISession session = connection.CreateSession(Constants.SessionMode.AUTO_ACKNOWLEDGE);
            IMessageConsumer consumer = session.CreateConsumer(queue);         

            IMessage recvMessage = consumer.Receive(500);


            try
            {
                listBox1.Items.Insert(0, ((ITextMessage)recvMessage).Text);
          
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
  

        }



        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            connection.Close();
            context.CloseAll();
        }

        
        


      
    }
}

 

---------2018.11.28-----WebLogic.Messaging.dll使用心得-------------------

和微軟的LES團隊交流,對這個WebLogic.Messaging.dll,做了更進一步的使用。

接收Queeu訊息可以有2種辦法,一種是顯示的方法呼叫:

IMessage recvMessage = consumer.Receive(500);

一種是訊息對映,在訊息響應函式中處理得到的訊息:

 consumer.Message += new MessageEventHandler(this.OnMessage);
 public void OnMessage(IMessageConsumer consumer, MessageEventArgs args)
        {          
        }

如果長時間Q裡沒有資料接收,可能客戶端到Q伺服器的連結會斷掉,所以需要定時的丟一個心跳訊息在Q裡。

再接收這個訊息丟棄掉,這樣可以保證鏈路一直可用。

 

按這個場景,我寫了一個程式,啟動一個工廠例項A-連線-生產者,每隔10秒去丟一個心跳訊息在Q裡,

再啟動一個工廠例項B-連線-消費者用訊息對映,在訊息響應函式方法自動得到訊息。

測試了幾個小時,中間也手動發了好多包。結果非常穩定,1000多個包,一個沒有丟。