Difference between revisions of "C-Sharp"

From Open Rail Data Wiki
Jump to navigation Jump to search
(Created page with "<code> using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using Apache.NMS; namespace ApacheNMS { class Program { …")
 
Line 54: Line 54:
 
</code>
 
</code>
  
For a full explanation of the code and how to use it, please see [http://gavincoates.com/c-activemq-stomp Gavins blog]. This code will connect to the Train Movememnts (All) data feed, and display the messages as they are displayed.
+
For a full explanation of the code and how to use it, please see [http://gavincoates.com/c-activemq-stomp Gavins blog]. This code will connect to the Train Movements (All) data feed, and display the messages as they are displayed.

Revision as of 14:36, 26 July 2012

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using Apache.NMS;

namespace ApacheNMS {

   class Program
   {
       static void Main(string[] args)
       {
           IConnectionFactory factory = new NMSConnectionFactory(new Uri("stomp:tcp://datafeeds.networkrail.com:61618"));
           IConnection connection = factory.CreateConnection("username", "password");
           ISession session = connection.CreateSession();
           IDestination destination = session.GetDestination("topic://" + "TRAIN_MVT_ALL_TOC");
           IMessageConsumer consumer = session.CreateConsumer(destination);


           connection.Start();
           consumer.Listener += new MessageListener(OnMessage);
           Console.WriteLine("Consumer started, waiting for messages... (Press ENTER to stop.)");
           Console.ReadLine();
           connection.Close();
       }
       private static void OnMessage(IMessage message)
       {
           try
           {
               Console.WriteLine("Median-Server (.NET): Message received");
               ITextMessage msg = (ITextMessage)message;
               message.Acknowledge();
               Console.WriteLine(msg.Text);
           }
           catch (Exception ex)
           {
               Console.WriteLine(ex.Message);
               Console.WriteLine("---");
               Console.WriteLine(ex.InnerException);
               Console.WriteLine("---");
               Console.WriteLine(ex.InnerException.Message);
           }
       }
   }

}

For a full explanation of the code and how to use it, please see Gavins blog. This code will connect to the Train Movements (All) data feed, and display the messages as they are displayed.