예제 #1
0
        private bool TryConnect()
        {
            try
            {
                moConnectionFactory             = new NMSConnectionFactory(new Uri(msConnectUrl));
                moConnection                    = moConnectionFactory.CreateConnection(msUser, msPassword);
                moConnection.ClientId           = msUser;
                moConnection.ExceptionListener += new ExceptionListener(OnConnectionException);
                moSession            = moConnection.CreateSession();
                moTopic              = moSession.GetTopic(msTopic);
                moConsumer           = moSession.CreateConsumer(moTopic);
                moConsumer.Listener += new MessageListener(OnMessageReceived);

                LastMessageReceivedAtUtc = DateTime.UtcNow;
                SpinSpreadUntilUtc       = DateTime.UtcNow.AddSeconds(30);

                moConnection.Start();
                Interlocked.Exchange(ref miIsConnected, 1);
                return(true);
            }
            catch (Exception oException)
            {
                moErrorQueue.Enqueue(new OpenRailConnectException("Connection attempt failed: " +
                                                                  OpenRailException.GetShortErrorInfo(oException), oException));
                Disconnect();
                return(false);
            }
        }
 private void OnConnectionException(Exception exception)
 {
     try
     {
         OpenRailConnectionException oConnectionException = new OpenRailConnectionException(
             OpenRailException.GetShortErrorInfo(exception), exception);
         moErrorQueue.Enqueue(oConnectionException);
         LastConnectionExceptionAtUtc = DateTime.UtcNow;
     }
     catch { }
 }
        private bool TryConnect()
        {
            try
            {
                moConnectionFactory             = new NMSConnectionFactory(new Uri(msConnectUrl));
                moConnection                    = moConnectionFactory.CreateConnection(msUser, msPassword);
                moConnection.ClientId           = msUser;
                moConnection.ExceptionListener += new ExceptionListener(OnConnectionException);
                moSession = moConnection.CreateSession(AcknowledgementMode.AutoAcknowledge);
                if (!string.IsNullOrWhiteSpace(msTopic1))
                {
                    moTopic1 = moSession.GetTopic(msTopic1);
                    if (mbUseDurableSubscription)
                    {
                        moConsumer1 = moSession.CreateDurableConsumer(moTopic1, msTopic1, null, false);
                    }
                    else
                    {
                        moConsumer1 = moSession.CreateConsumer(moTopic1);
                    }
                    moConsumer1.Listener += new MessageListener(OnMessageReceived1);
                }
                if (!string.IsNullOrWhiteSpace(msTopic2))
                {
                    moTopic2 = moSession.GetTopic(msTopic2);
                    if (mbUseDurableSubscription)
                    {
                        moConsumer2 = moSession.CreateDurableConsumer(moTopic2, msTopic2, null, false);
                    }
                    else
                    {
                        moConsumer2 = moSession.CreateConsumer(moTopic2);
                    }
                    moConsumer2.Listener += new MessageListener(OnMessageReceived2);
                }

                LastMessageReceivedAtUtc = DateTime.UtcNow;
                SpinSpreadUntilUtc       = DateTime.UtcNow.AddSeconds(30);

                moConnection.Start();
                Interlocked.Exchange(ref miIsConnected, 1);
                return(true);
            }
            catch (Exception oException)
            {
                moErrorQueue.Enqueue(new OpenRailConnectException("Connection attempt failed: " +
                                                                  OpenRailException.GetShortErrorInfo(oException), oException));
                Disconnect();
                return(false);
            }
        }
        private void OnMessageReceived2(IMessage message)
        {
            try
            {
                OpenRailMessage oMessage = null;

                // when the Apache code starts receiving messages, a number of worker threads are fired up (inside the Apache assembly)
                // these threads are all started up at close to the exact same time
                // this can lead to contention within the apache code, i.e.  blocking and slow throughput until the threads spread out
                // so, for the first half minute, inject some spin waits in the different worker threads to spread their activities out
                if (DateTime.UtcNow < SpinSpreadUntilUtc)
                {
                    long iSeed = Thread.CurrentThread.ManagedThreadId + (DateTime.Now.Ticks % Int32.MaxValue);
                    Thread.SpinWait(new Random((int)(iSeed % Int32.MaxValue)).Next(1, 1000000));
                }

                // text message
                ITextMessage msgText = message as ITextMessage;
                if (msgText != null)
                {
                    oMessage = new OpenRailTextMessage(msgText.NMSTimestamp, msgText.Text);
                }

                // bytes message
                IBytesMessage msgBytes = message as IBytesMessage;
                if (msgBytes != null)
                {
                    oMessage = new OpenRailBytesMessage(message.NMSTimestamp, msgBytes.Content);
                }

                // everything else
                if (oMessage == null)
                {
                    oMessage = new OpenRailUnsupportedMessage(message.NMSTimestamp, message.GetType().FullName);
                }

                Interlocked.Increment(ref miMessageReadCount2);
                LastMessageReceivedAtUtc = DateTime.UtcNow;
                moMessageQueue2.Enqueue(oMessage);
            }
            catch (Exception oException)
            {
                moErrorQueue.Enqueue(new OpenRailMessageException("Message receive for topic 2 failed: " +
                                                                  OpenRailException.GetShortErrorInfo(oException), oException));
            }
        }
        private async Task Run()
        {
            CancellationToken oCT = moCTS.Token;

            try
            {
                Interlocked.Exchange(ref miMessageReadCount1, 0);
                Interlocked.Exchange(ref miMessageReadCount2, 0);
                await Connect();

                bool bRefreshRequired = false;
                while (!oCT.IsCancellationRequested)
                {
                    await Task.Delay(50);

                    int iMessageGapToleranceSeconds = DateTime.UtcNow < LastConnectionExceptionAtUtc.AddSeconds(60) ? 30 : 120;
                    bRefreshRequired = (LastMessageReceivedAtUtc.AddSeconds(iMessageGapToleranceSeconds) < DateTime.UtcNow);
                    if (bRefreshRequired)
                    {
                        Disconnect();
                        await Connect();

                        LastMessageReceivedAtUtc = DateTime.UtcNow;
                        bRefreshRequired         = false;
                    }
                }
            }
            catch (Exception oException)
            {
                if (!oCT.IsCancellationRequested)
                {
                    moErrorQueue.Enqueue(new OpenRailFatalException("OpenRailNRODReceiver FAILED due to " +
                                                                    OpenRailException.GetShortErrorInfo(oException), oException));

                    // rethrow the exception:
                    // this sets the Exception property of the Task object
                    // i.e. makes this exception visible in the FatalException property of this class
                    throw oException;
                }
            }
            finally
            {
                Disconnect();
            }
        }