版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ot512csdn/article/details/84553131
今天做了一个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多个包,一个没有丢。