X3

RedSky

导航

c# ActiveMQ的使用

/// <sumary>
/// 接收方
/// </sumary>
public
void StartClient() {
  var factory = new NMSConnectionFactory(new Uri("activemq:failover:(tcp://localhost:61616/?wireFormat.maxInactivityDuration=0)"));
  var connection = factory.CreateConnection();
  //ClientId:指定一个唯一值,可接收连线前服务端分配的消息
  connection.ClientId = "XXX";
  connection.ExceptionListener += Connection_ExceptionListener;
  connection.ConnectionInterruptedListener += Connection_ConnectionInterruptedListener;
  connection.ConnectionResumedListener += Connection_ConnectionResumedListener;
  connection.Start();
  var session = connection.CreateSession();
  //Queue,队列模式,队列中哪个接收方先接收到,其他接收方将不再接收,consumer.prefetchSize=1表示每次只接收一条消息
  consumer = session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("MyMQQueueName" + "?consumer.prefetchSize=1"));
  consumer.Listener += new MessageListener(consumer_Listener);
}

/// <sumary>
/// 接收到的消息
/// </summary>
void consumer_Listener(IMessage message)
{
  string txt = HttpUtility.UrlDecode((ITextMessage)message.Text);
}

 

posted on 2020-05-13 14:38  HotSky  阅读(726)  评论(0编辑  收藏  举报