• 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; }
        }
    

      

  • 相关阅读:
    HTML表格和列表笔记&练习<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>关于表格的一些练习</title> </head> <body> <p>一个普通基本的表格</p> <table border="5&
    HTML中的CSS类型
    html关于图片和链接的笔记
    实现窗体背景透明
    HTML控件篇 -- input
    AngularJs的$http使用随笔
    Win7启动修复(Ubuntu删除后进入grub rescue的情况)
    2013年9月30日我的博客园开通啦
    将excel表格导入到DataGridView
    c#中,点击一个菜单项后调用exe文件
  • 原文地址:https://www.cnblogs.com/xiangxiong/p/8074563.html
Copyright © 2020-2023  润新知