C#:ActiveMQ(二)点对点方式之生产者

同系列文章:

程序界面:
ActiveMQ-P2P-Producer

源代码
FormProducer.cs

/* ----------------------------------------------------------
 * 文件名称:FormProducer.cs
 * 
 * 作者:秦建辉
 * 
 * QQ:36748897 
 * 
 * 博客:http://www.firstsolver.com/wordpress/
 * 
 * 开发环境:
 *      Visual Studio V2015
 *      .NET Framework 4.5.2
 * 
 * 版本历史:
 *      V1.0    2016年06月01日
 *              基于ActiveMQ实现点对点消息传递域上的生产者
 *                   
 * 消息发送流程:
 *      1.创建连接使用的工厂类IConnectionFactory
 *      2.使用管理对象IConnectionFactory建立连接IConnection,并启动
 *      3.使用连接IConnection 建立会话ISession
 *      4.使用会话ISession和管理对象IDestination创建消息生产者IMessageProducer
 *      5.使用消息生产者IMessageProducer发送消息
------------------------------------------------------------ */
using Apache.NMS;
using Apache.NMS.ActiveMQ;
using System;
using System.Windows.Forms;

namespace Com.FirstSolver
{
    public partial class FormProducer : Form
    {
        private const string DESTINATION = "Com.FirstSolver";

        public FormProducer()
        {
            InitializeComponent();
        }

        private void buttonSend_Click(object sender, EventArgs e)
        {
            try
            {
                // 创建公共消息连接工厂
                IConnectionFactory Factory = new ConnectionFactory(textBoxURL.Text);
                using (IConnection Connection = Factory.CreateConnection())
                {
                    using (ISession Session = Connection.CreateSession())
                    {
                        using (IMessageProducer Producer = Session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue(DESTINATION)))
                        {
                            ITextMessage Message = Producer.CreateTextMessage();
                            Message.Text = textBoxMessage.Text;
                            Message.Properties.SetString("filter", "demo");
                            Producer.Send(Message, MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.MinValue);

                            MessageBox.Show("消息发送成功!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            textBoxMessage.Clear();
                        }                            
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonClearAll_Click(object sender, EventArgs e)
        {
            textBoxMessage.Clear();
        }
    }
}

Comments are closed.