• activemq


        public Form2()
            {
                InitializeComponent();
                InitProducer();
            }
            private IConnectionFactory factory;
            public void InitProducer()
            {
                try
                {
                    //初始化工厂,这里默认的URL是不需要修改的
                    factory = new ConnectionFactory("tcp://localhost:61616");
    
                }
                catch
                {
                    MessageBox.Show("初始化失败!");
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                //通过工厂建立连接
                using (IConnection connection = factory.CreateConnection())
                {
                    //通过连接创建Session会话
                    using (ISession session = connection.CreateSession())
                    {
                        //通过会话创建生产者,方法里面new出来的是MQ中的Queue
                        IMessageProducer prod = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("dxQueue"));
                        //创建一个发送的消息对象
                        abc v = new abc();
                        v.a = "dfsafds" + DateTime.Now;
                        v.b = "dddd";
                        v.c = "ddddddddd--------";
                        v.id = 55;
                        IObjectMessage message = prod.CreateObjectMessage(v);
    
                        //生产者把消息发送出去,几个枚举参数MsgDeliveryMode是否长链,MsgPriority消息优先级别,发送最小单位,当然还有其他重载
                        prod.Send(message, MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.MinValue);
                        MessageBox.Show("发送成功!");
    
    
                    }
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                //创建连接工厂
                IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616");
                //通过工厂构建连接
                IConnection connection = factory.CreateConnection();
                //这个是连接的客户端名称标识
                connection.ClientId = "firstQueueListener";
                //启动连接,监听的话要主动启动连接
                connection.Start();
                //通过连接创建一个会话
                ISession session = connection.CreateSession();
                //通过会话创建一个消费者,这里就是Queue这种会话类型的监听参数设置
                IMessageConsumer consumer = session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("dxQueue"));
                //注册监听事件
                consumer.Listener += new MessageListener(consumer_Listener);
                //connection.Stop();
                //connection.Close();  
    
            }
    
            void consumer_Listener(IMessage message)
            {
                IObjectMessage msg = (IObjectMessage)message;
                abc v = msg.Body as abc;
                //异步调用下,否则无法回归主线程
                this.Invoke((MethodInvoker)delegate { MessageBox.Show(v.a,v.b); });
    
            }
    

      Apache.NMS.ActiveMQ.dll

    Apache.NMS.dll

      public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                InitProducer();
            }
            private IConnectionFactory factory;
            public void InitProducer()
            {
                try
                {
                    //初始化工厂,这里默认的URL是不需要修改的
                    factory = new ConnectionFactory("tcp://localhost:61616");
    
                }
                catch
                {
                    lbMessage.Text = "初始化失败!!";
                }
            }
    
    
            private void button1_Click(object sender, EventArgs e)
            {
                //通过工厂建立连接
                using (IConnection connection = factory.CreateConnection())
                {
                    //通过连接创建Session会话
                    using (ISession session = connection.CreateSession())
                    {
                        //通过会话创建生产者,方法里面new出来的是MQ中的Queue
                        IMessageProducer prod = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("firstQueue"));
                        //创建一个发送的消息对象
                        ITextMessage message = prod.CreateTextMessage();
                        //给这个对象赋实际的消息
                        message.Text = textBox1.Text;
                        //设置消息对象的属性,这个很重要哦,是Queue的过滤条件,也是P2P消息的唯一指定属性
                        message.Properties.SetString("filter", "demo");
                        //生产者把消息发送出去,几个枚举参数MsgDeliveryMode是否长链,MsgPriority消息优先级别,发送最小单位,当然还有其他重载
                        prod.Send(message, MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.MinValue);
                        lbMessage.Text = "发送成功!!";
                        textBox1.Text = "";
                        textBox1.Focus();
    
    
                    }
                }
    
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                Form2 f2 = new Form2();
                f2.Show();
            }
        }
    
        [Serializable]
        public class abc
        {
            public string a { get; set; }
            public string b { get; set; }
            public string c { get; set; }
            public int id { get; set; }
        }
    

      

  • 相关阅读:
    爬虫-selenium模块
    动画《区块链100问》第4集:第一个比特币诞生啦!
    动画《区块链100问》第5集:谁是中本聪?
    动画《区块链100问》第6集:密码朋克是什么?
    动画《区块链100问》第7集:比特币是怎么发行的?
    动画《区块链100问》第8集:披萨居然卖到3亿元?
    动画《区块链100问》第9集:中本聪的继任者是谁?
    动画《区块链100问》第10集:早期比特币还能白送!
    《区块链100问》第11集:比特币为什么还没挖完?
    《区块链100问》第12集:比特币如何实现总量恒定?
  • 原文地址:https://www.cnblogs.com/xiangxiong/p/8074563.html
Copyright © 2020-2023  润新知