zoukankan      html  css  js  c++  java
  • 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; }
        }
    

      

  • 相关阅读:
    表单
    HTML5新特性
    Bash中的特殊字符
    网站商务通链接快速标识v1.0.js
    js实现中文简繁切换效果
    input 表单点击消失离开出现
    canonical 标签介绍
    商务通对话窗口出现验证码
    织梦dedecms后台发布文章不自动更新首页与栏目列表页
    dedecms修改templets为别的名字
  • 原文地址:https://www.cnblogs.com/xiangxiong/p/8074563.html
Copyright © 2011-2022 走看看