您的位置:首页 > 博客中心 > 电脑问题 >

C# ACtiveMQ p2p收发数据

时间:2022-03-18 07:55

1.下载ActiveMQ

官方网站下载地址:

2.运行ActiveMQ

解压缩apache-activemq-5.10.0-bin.zip,然后双击apache-activemq-5.10.0\bin\win32\activemq.bat运行ActiveMQ程序。

启动ActiveMQ以后,可以使用浏览器登陆:验证, 默认用户名是:admin  密码是:admin

(前提是配置好好JDK环境,能打开console代表MQ运行成功)
技术分享图片 

同时下载.net版Dll:Apache.NMS-1.6.0-bin.zip和Apache.NMS.ActiveMQ-1.6.4-bin.zip

都从这里下载:

  1. 引用ActiveMQ类库:

    (1)Apache.NMS.dll路径:\Apache.NMS.ActiveMQ-1.7.2-bin\lib\Apache.NMS\net-3.5

    (2)Apache.NMS.ActiveMQ.dll路径:\Apache.NMS.ActiveMQ-1.7.2-bin\build\net-3.5\debug

3.MQ例子(生产者)

     public Form1()         {             InitializeComponent();             InitProducer();         }      private IConnectionFactory factory;      public void InitProducer()         {             try             {                 //初始化工厂                 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();                     XmlDocument Doc = new XmlDocument();                     Doc.LoadXml("<?xml version=‘1.0‘ encoding=‘UTF-8‘?><flightroute><flight><flightinfo><acid>CCA1501</acid><runway>13L</runway><gate>N115</gate><cockpitdirection>180</cockpitdirection><deparr>DEP</deparr></flightinfo></flight</flightroute>");                     message.Text = Doc.InnerXml; //给这个消息对象赋实际的消息                     //设置消息对象的属性,是Queue的过滤条件也是P2P的唯一指定属性                     message.Properties.SetString("filter","demo");                     prod.Send(message, MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.MinValue);                     lbMessage.Text = "发送成功!";                     Text.Text = "";                     Text.Focus();                 }             }         }  
技术分享图片
可以点击按钮发送一些东西

接收端代码(consumer)
 public Form2()         {             InitializeComponent();             InitConsumer();         }         public void InitConsumer()         {             //创建连接工厂             IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616");             //通过工厂创建连接             IConnection connection = factory.CreateConnection();             //连接服务器端的标识             connection.ClientId = "firstQueueListener";               //启动连接             connection.Start();             //通过连接创建对话             ISession session = connection.CreateSession();             //通过会话创建一个消费者             IMessageConsumer consumer = session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("firstQueue"),"filter = ‘demo‘");             //注册监听事件             consumer.Listener += new MessageListener(consumer_Listener);         }         void consumer_Listener(IMessage message)         {             ITextMessage msg = (ITextMessage)message;             ReceiveMessage.Invoke(new DelegateRevMessage(RevMessage), msg);         }         public delegate void DelegateRevMessage(ITextMessage message);           public void RevMessage(ITextMessage message)         {             ReceiveMessage.Text += string.Format(@"接收到:{0}{1}", message.Text, Environment.NewLine);         }        
                  技术分享图片 
        发送的消息如果没接收就被存在Queue里

技术分享图片

热门排行

今日推荐

热门手游