/// <summary> /// subscribes to each feed /// </summary> private void Subscribe() { using (IConnection connection = this.GetConnection()) { connection.AcknowledgementMode = AcknowledgementMode.AutoAcknowledge; string clientId = ConfigurationManager.AppSettings["ActiveMQDurableClientId"]; if (!string.IsNullOrEmpty(clientId)) { connection.ClientId = clientId; } // use a connection monitor to check for data every 3 mins // if no data or exception occurs it will cancel the token using (var connectionMonitor = new NMSConnectionMonitor(connection, _cancellationTokenSource, TimeSpan.FromMinutes(3))) { connection.Start(); // create a task to monitor for the various feeds Task tmDataTask = Task.Factory.StartNew(() => GetTrainMovementData(connection, _cancellationTokenSource.Token, connectionMonitor), _cancellationTokenSource.Token); Task tdDataTask = Task.Factory.StartNew(() => GetTrainDescriberData(connection, _cancellationTokenSource.Token, connectionMonitor), _cancellationTokenSource.Token); Task vstpDataTask = Task.Factory.StartNew(() => GetVSTPData(connection, _cancellationTokenSource.Token, connectionMonitor), _cancellationTokenSource.Token); Task rtppmTask = Task.Factory.StartNew(() => GetRtPPMData(connection, _cancellationTokenSource.Token, connectionMonitor), _cancellationTokenSource.Token); try { // wait on all tasks Task.WaitAll(new[] { tmDataTask, tdDataTask, vstpDataTask, rtppmTask }, _cancellationTokenSource.Token); if (!connectionMonitor.QuitOk) { Trace.TraceError("Connection Monitor did not quit OK. Retrying Connection"); TraceHelper.FlushLog(); throw new RetryException(); } _cancellationTokenSource.Cancel(); Trace.TraceInformation("Closing connection to: {0}", connection); } catch (OperationCanceledException) { Trace.TraceError("Connection Monitor cancelled"); TraceHelper.FlushLog(); } } } }
/// <summary> /// open a consumer to the given endpoint. /// this method wont return until the cancellation token is cancelled /// </summary> /// <param name="session">session to connect to</param> /// <param name="topic">topic to get data from</param> /// <param name="appendedText">optional text to append to durable subscriber name</param> /// <param name="connectionMonitor">connection monitor that is monitoring this connection</param> /// <param name="listener">delegat to subecribe to new messages</param> /// <param name="ct">cancellation token to wait on once connected</param> private void OpenAndWaitConsumer(ISession session, ITopic topic, string appendedText, NMSConnectionMonitor connectionMonitor, MessageListener listener, CancellationToken ct) { using (IMessageConsumer consumer = CreateConsumer(session, topic, appendedText)) { Trace.TraceInformation("Created consumer to {0}", topic); consumer.Listener += listener; connectionMonitor.AddMessageConsumer(consumer); ct.WaitHandle.WaitOne(); } }
private void GetVSTPData(IConnection connection, CancellationToken ct, NMSConnectionMonitor connectionMonitor) { string vstpTopic = ConfigurationManager.AppSettings["VSTPFeedName"]; if (!string.IsNullOrEmpty(vstpTopic)) { using (ISession session = connection.CreateSession()) { ITopic topic = session.GetTopic(vstpTopic); OpenAndWaitConsumer(session, topic, "vstp", connectionMonitor, this.vstpConsumer_Listener, ct); } } }
private void GetTrainMovementData(IConnection connection, CancellationToken ct, NMSConnectionMonitor connectionMonitor) { string trainMovementTopic = ConfigurationManager.AppSettings["TrainMovementName"]; if (!string.IsNullOrEmpty(trainMovementTopic)) { using (ISession session = connection.CreateSession()) { ITopic topic = session.GetTopic(trainMovementTopic /*"TRAIN_MVT_ALL_TOC"*/); OpenAndWaitConsumer(session, topic, "tm", connectionMonitor, this.tmConsumer_Listener, ct); } } }
private void GetTrainDescriberData(IConnection connection, CancellationToken ct, NMSConnectionMonitor connectionMonitor) { string trainDescriberTopic = ConfigurationManager.AppSettings["TrainDescriberName"]; if (!string.IsNullOrEmpty(trainDescriberTopic)) { using (ISession session = connection.CreateSession()) { ITopic topic = session.GetTopic(trainDescriberTopic/*"TD_LNW_WMC_SIG_AREA"*/); OpenAndWaitConsumer(session, topic, "td", connectionMonitor, this.tdConsumer_Listener, ct); } } }
private void GetRtPPMData(IConnection connection, CancellationToken cancellationToken, NMSConnectionMonitor connectionMonitor) { string rtppmTopic = ConfigurationManager.AppSettings["RTPPMFeedName"]; if (!string.IsNullOrEmpty(rtppmTopic)) { using (ISession session = connection.CreateSession()) { ITopic topic = session.GetTopic(rtppmTopic); OpenAndWaitConsumer(session, topic, "rtppm", connectionMonitor, this.rtppm_Listener, cancellationToken); } } }