Advertisement
Guest User

Advisory Messages with NMS.ActiveMQ

a guest
Feb 10th, 2011
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.73 KB | None | 0 0
  1. using System;
  2. using Apache.NMS;
  3. using Apache.NMS.Util;
  4. using Apache.NMS.ActiveMQ;
  5. using Apache.NMS.ActiveMQ.Commands;
  6.  
  7. namespace AdvisoryExample
  8. {
  9.     class AdvisoryExample
  10.     {
  11.         private IConnection connection;
  12.         private ISession session;
  13.  
  14.         public const String QUEUE_ADVISORY_DESTINATION = "ActiveMQ.Advisory.Queue";
  15.         public const String TOPIC_ADVISORY_DESTINATION = "ActiveMQ.Advisory.Topic";
  16.         public const String TEMPQUEUE_ADVISORY_DESTINATION = "ActiveMQ.Advisory.TempQueue";
  17.         public const String TEMPTOPIC_ADVISORY_DESTINATION = "ActiveMQ.Advisory.TempTopic";
  18.  
  19.         public const String ALLDEST_ADVISORY_DESTINATION = QUEUE_ADVISORY_DESTINATION + "," +
  20.                                                            TOPIC_ADVISORY_DESTINATION + "," +
  21.                                                            TEMPQUEUE_ADVISORY_DESTINATION + "," +
  22.                                                            TEMPTOPIC_ADVISORY_DESTINATION;
  23.  
  24.         AdvisoryExample()
  25.         {
  26.             IConnectionFactory factory = new ConnectionFactory();
  27.  
  28.             connection = factory.CreateConnection();
  29.             connection.Start();
  30.             session = connection.CreateSession();
  31.         }
  32.  
  33.         void EnumerateQueues()
  34.         {
  35.             Console.WriteLine("Listing all Queues on Broker:");
  36.  
  37.             IDestination dest = session.GetTopic(QUEUE_ADVISORY_DESTINATION);
  38.  
  39.             using(IMessageConsumer consumer = session.CreateConsumer(dest))
  40.             {
  41.                 IMessage advisory;
  42.  
  43.                 while((advisory = consumer.Receive(TimeSpan.FromMilliseconds(2000))) != null)
  44.                 {
  45.                     ActiveMQMessage amqMsg = advisory as ActiveMQMessage;
  46.  
  47.                     if(amqMsg.DataStructure != null)
  48.                     {
  49.                         DestinationInfo info = amqMsg.DataStructure as DestinationInfo;
  50.                         if(info != null)
  51.                         {
  52.                             Console.WriteLine("   Queue: " + info.Destination.ToString() );
  53.                         }
  54.                     }
  55.                 }
  56.             }
  57.             Console.WriteLine("Listing Complete.");
  58.         }
  59.  
  60.         void EnumerateTopics()
  61.         {
  62.             Console.WriteLine("Listing all Topics on Broker:");
  63.  
  64.             IDestination dest = session.GetTopic(TOPIC_ADVISORY_DESTINATION);
  65.  
  66.             using(IMessageConsumer consumer = session.CreateConsumer(dest))
  67.             {
  68.                 IMessage advisory;
  69.  
  70.                 while((advisory = consumer.Receive(TimeSpan.FromMilliseconds(2000))) != null)
  71.                 {
  72.                     ActiveMQMessage amqMsg = advisory as ActiveMQMessage;
  73.  
  74.                     if(amqMsg.DataStructure != null)
  75.                     {
  76.                         DestinationInfo info = amqMsg.DataStructure as DestinationInfo;
  77.                         if(info != null)
  78.                         {
  79.                             Console.WriteLine("   Topic: " + info.Destination.ToString() );
  80.                         }
  81.                     }
  82.                 }
  83.             }
  84.             Console.WriteLine("Listing Complete.");
  85.         }
  86.  
  87.         void EnumerateDestinations()
  88.         {
  89.             Console.WriteLine("Listing all Destinations on Broker:");
  90.  
  91.             IDestination dest = session.GetTopic(ALLDEST_ADVISORY_DESTINATION);
  92.  
  93.             using(IMessageConsumer consumer = session.CreateConsumer(dest))
  94.             {
  95.                 IMessage advisory;
  96.  
  97.                 while((advisory = consumer.Receive(TimeSpan.FromMilliseconds(2000))) != null)
  98.                 {
  99.                     ActiveMQMessage amqMsg = advisory as ActiveMQMessage;
  100.  
  101.                     if(amqMsg.DataStructure != null)
  102.                     {
  103.                         DestinationInfo info = amqMsg.DataStructure as DestinationInfo;
  104.                         if(info != null)
  105.                         {
  106.                             string destType = info.Destination.IsTopic ? "Topic" : "Qeue";
  107.                             destType = info.Destination.IsTemporary ? "Temporary" + destType : destType;
  108.                             Console.WriteLine("   " + destType + ": " + info.Destination.ToString() );
  109.                         }
  110.                     }
  111.                 }
  112.             }
  113.             Console.WriteLine("Listing Complete.");
  114.         }
  115.  
  116.         void ShutDown()
  117.         {
  118.             session.Close();
  119.             connection.Close();
  120.         }
  121.  
  122.         public static void Main (string[] args)
  123.         {
  124.             AdvisoryExample ex = new AdvisoryExample();
  125.  
  126.             ex.EnumerateQueues();
  127.             ex.EnumerateTopics();
  128.             ex.EnumerateDestinations();
  129.             ex.ShutDown();
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement