同系列文章:
源代码
FormRequester.cs
/* ---------------------------------------------------------- * 文件名称:FormRequester.cs * * 作者:秦建辉 * * QQ:36748897 * * 博客:http://www.firstsolver.com/wordpress/ * * 开发环境: * Visual Studio V2015 * .NET Framework 4.5.2 * * 版本历史: * V1.0 2016年06月03日 * 基于ActiveMQ实现请求者 * * 参考资料: * http://activemq.apache.org/how-should-i-implement-request-response-with-jms.html ------------------------------------------------------------ */ using Apache.NMS; using Apache.NMS.ActiveMQ; using System; using System.Windows.Forms; namespace Com.FirstSolver { public partial class FormRequester : Form { private bool IsServerRunning = false; private IConnection Connection = null; private ISession Session = null; private IMessageProducer Producer = null; private IMessageConsumer Consumer = null; private IDestination NMSDestination = new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("Com.FirstSolver.Hotline"); private IDestination NMSReplyTo = null; public FormRequester() { InitializeComponent(); buttonSend.Enabled = false; } private void buttonSend_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(textBoxMessage.Text)) { try { ITextMessage Message = Producer.CreateTextMessage(); Message.Text = textBoxMessage.Text; Message.NMSReplyTo = NMSReplyTo; Message.NMSCorrelationID = Guid.NewGuid().ToString("D"); Producer.Send(Message, MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.MinValue); textBoxMessage.Clear(); // 显示发送内容 textBoxReponse.AppendText("请求-消息编号:" + Message.NMSMessageId + "\r\n"); textBoxReponse.AppendText("请求-关联编号:" + Message.NMSCorrelationID + "\r\n"); textBoxReponse.AppendText("请求-消息内容:" + Message.Text + "\r\n"); } catch (Exception ex) { MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void buttonClearAll_Click(object sender, EventArgs e) { textBoxReponse.Clear(); } private void buttonStart_Click(object sender, EventArgs e) { if (IsServerRunning) { MQClose(); } else { try { // 创建公共消息连接工厂 IConnectionFactory Factory = new ConnectionFactory(textBoxURL.Text); Connection = Factory.CreateConnection(); Connection.ClientId = "Com.FirstSolver.ActiveMQ.Requester"; Connection.Start(); // 开启侦听 Session = Connection.CreateSession(); // 创建生产者 Producer = Session.CreateProducer(NMSDestination); // 创建消费者 NMSReplyTo = Session.CreateTemporaryQueue(); Consumer = Session.CreateConsumer(NMSReplyTo); Consumer.Listener += OnMessageReceived; } catch (Exception ex) { MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error); MQClose(); return; } IsServerRunning = true; buttonStart.Text = "Stop Listener"; buttonSend.Enabled = true; } } private void OnMessageReceived(IMessage message) { if (message is ITextMessage) { this.Invoke(new Action<ITextMessage>((msg) => { textBoxReponse.AppendText("应答-消息编号:" + msg.NMSMessageId + "\r\n"); textBoxReponse.AppendText("应答-关联编号:" + msg.NMSCorrelationID + "\r\n"); textBoxReponse.AppendText("应答-消息内容:" + msg.Text + "\r\n\r\n"); }), message as ITextMessage); } } private void FormRequester_FormClosing(object sender, FormClosingEventArgs e) { MQClose(); } private void MQClose() { this.Invoke(new Action(() => { if (IsServerRunning) { if (Consumer != null) { Consumer.Close(); Consumer = null; } if (Producer != null) { Producer.Close(); Producer = null; } if (Session != null) { Session.Close(); Session = null; } if (Connection != null) { Connection.Stop(); Connection.Close(); Connection = null; } IsServerRunning = false; buttonStart.Text = "Start Listener"; buttonSend.Enabled = false; // 禁止消息发送 } })); } } }