ActiveMQ提供多种语言支持,如Java, C, C++, C#, Ruby, Perl, Python, PHP等。此处列举C#实例

 下述C#实例代是基于QUEUE的P2P方式,如需要实现TOPIC的广播模式,请参见下方总结第四条。

 

public void ActiveSend()
        {
            IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616");
            //通过工厂建立连接
            using (IConnection connection = factory.CreateConnection())
            {
                try
                {
                    //通过连接创建Session会话
                    using (ISession session = connection.CreateSession())
                    {
                        //通过会话创建生产者,方法里面new出来的是MQ中的Queue
                        using (IMessageProducer prod = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("firstQueue")))
                        {
                            //创建一个发送的消息对象
                            ITextMessage message = prod.CreateTextMessage();
                            while (true)
                            {
                                try
                                {
                                    //给这个对象赋实际的消息
                                    message.Text = Console.ReadLine();
                                    //设置消息对象的属性,这个很重要哦,是Queue的过滤条件,也是P2P消息的唯一指定属性
                                    message.Properties.SetString("filter", "demo");
                                    //生产者把消息发送出去,几个枚举参数MsgDeliveryMode是否长链,MsgPriority消息优先级别,发送最小单位,当然还有其他重载
                                    prod.Send(message, MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.MinValue);
                                    Console.WriteLine("消息发送成功");
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("消息发送失败!" + ex);
                                }
                            }

                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("创建发送MQ失败失败" + e);
                }
            }
        }
View Code

相关文章:

  • 2021-09-30
  • 2021-05-30
  • 2022-12-23
  • 2021-06-18
  • 2021-05-31
  • 2021-05-15
  • 2021-11-20
猜你喜欢
  • 2021-12-25
  • 2022-12-23
  • 2021-08-14
  • 2021-08-30
  • 2021-12-23
  • 2021-07-02
  • 2021-09-18
相关资源
相似解决方案