C-Sharp: Difference between revisions

From Open Rail Data Wiki
No edit summary
Gavco98uk (talk | contribs)
feed url was incorrect. should be .co.uk not .com
Line 17: Line 17:
         static void Main(string[] args)
         static void Main(string[] args)
         {
         {
             IConnectionFactory factory = new NMSConnectionFactory(new Uri("stomp:tcp://datafeeds.networkrail.com:61618"));
             IConnectionFactory factory = new NMSConnectionFactory(new Uri("stomp:tcp://datafeeds.networkrail.co.uk:61618"));


             IConnection connection = factory.CreateConnection("username", "password");
             IConnection connection = factory.CreateConnection("username", "password");

Revision as of 16:05, 3 December 2012

Apache NMS

This code will connect to the TRAIN_MVT_ALL_TOC topic and display all messages. For more details on this client, see Gavin's blog.

 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.co.uk: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);
            }
        }
    }
 }