关于CTS项目的C#+ActiveMQ

关于CTS项目的C#+ActiveMQ

1、Visual Studio 2013、Apache.NMS.ActiveMQ v1.7.2 Released。
下载:http://activemq.apache.org/nms/index.html

引入DLL:Apache.NMS.dll、Apache.NMS.ActiveMQ.dll。

2、界面

3、消息发送至cts.queue队列、消息接收。

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

namespace activemq
{
    public partial class FrmMain : Form
    {
        private IConnectionFactory factory;

        public FrmMain()
        {
            InitializeComponent();

            // 初始化
            Init();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init()
        {
            try
            {
                //初始化工厂
                factory = new ConnectionFactory("tcp://localhost:61616");
            }
            catch
            {
                txtLine.Text = "初始化失败";
            }
        }

        /// <summary>
        /// 发送
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSend_Click(object sender, EventArgs e)
        {
            // 建立工厂连接
            using (IConnection connection = factory.CreateConnection())
            {
                // 通过工厂连接创建Session会话
                using (ISession session = connection.CreateSession())
                {
                    // 创建生产者
                    IMessageProducer prod = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("cts.queue"));
                    // 创建消息对象
                    ITextMessage message = prod.CreateTextMessage();
                    // 赋值
                    message.Text = "hi, jzh";
                    prod.Send(message, MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.MinValue);

                    txtLine.Text = "发送成功";
                }
            }
        }

        /// <summary>
        /// 接收
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGet_Click(object sender, EventArgs e)
        {
            // 创建连接工厂
            //IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616");
            // 通过工厂创建连接
            IConnection connection = factory.CreateConnection();
            // 连接服务器端的标识
            connection.ClientId = "clientid.9001";
            // 启动连接
            connection.Start();
            // 通过连接创建对话
            ISession session = connection.CreateSession();
            // 通过会话创建一个消费者
            IMessageConsumer consumer = session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("cts.queue"));
            // 注册监听事件
            consumer.Listener += new MessageListener(consumer_Listener);
        }

        /// <summary>
        /// 监听事件
        /// </summary>
        /// <param name="message"></param>
        void consumer_Listener(IMessage message)
        {
            ITextMessage msg = (ITextMessage)message;
            txtReceive.Invoke(new DelegateRevMessage(RevMessage), msg);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        public delegate void DelegateRevMessage(ITextMessage message);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        public void RevMessage(ITextMessage message)
        {
            txtReceive.Text += string.Format(@"接收到:{0}{1}", message.Text, Environment.NewLine);
        }
    }
}

发表回复

您的电子邮箱地址不会被公开。