/// <summary>
 /// Close event Transmission 
 /// </summary>
 void CloseTransmission()
 {
     try
     {
        if (_IsTransmitting)
         {
             Logger.Info("EventTransmitter: Closing Event Transmitter");
             if (_Sender != null)
             { 
                 _Sender.CloseSender();
                 _Sender = null;
             }
             if (_Message != null)
             {
                 _Message.CloseQueue();
                 _Message = null;
             }
             _IsTransmitting = false;
         
         }
         else
         {
             Logger.Info("EventTransmitter", "CloseTransmission", "Transmission Already Stopped");
         }
     }
     catch (Exception Ex)
     {
         Logger.Error("EventTransmitter", "CloseTransmission", Ex);
     }
     finally
     {
         //m_evntRefreshApp.Set(); //Trigger for app refresh 
         _IsTransmitting = false;
         Logger.Info("EventTransmitter: STOPPED.");
     }
 }
Пример #2
0
 public WriteOnlyQueue(string strQueuePath)
 {
     _QueueManager = new QueueManager(strQueuePath);
     _QueueManager.OpenQueue();
 }
        /// <summary>
        /// Transmit Messages from queue to Server
        /// </summary>
        void Transmit()
        {
            try
            {
                UInt32 iSequenceNo = 0;
                Byte bSequenceNo;
                Logger.Info("EventTransmitter: STARTING");
                _Message = new QueueManager(Settings.EventTransmitterSourceQ);
    
                //Getting mode of transfer
               _Sender = SenderFactory.GetSender();

                if (this.InitializeWithRetry())
                {
                    _Message.OpenQueue();

                    while (!_evtTransmitShutDown.WaitOne(10))
                    {

                        Int32 iResponse;
                            try
                            {
                                _IsTransmitting = true;

                                if (iSequenceNo >= 255)
                                {
                                    iSequenceNo = 1;
                                }
                                else
                                {
                                    iSequenceNo++;
                                }
                                bSequenceNo = Convert.ToByte(iSequenceNo);
                                Logger.Info("Reading queue");
                                
                                if (!_Message.IsQueueOpen)
                                {
                                    _Message.OpenQueue();
                                }

                                strXML = _Message.ReadMessage();
                                Logger.Info("Read Complete");
                                SendToExternalLog("SENDING: " + strXML);
                                // parse return message 
                                //Construct STM format message 

                                // Refresh or empty message
                                if (string.IsNullOrEmpty(strXML))
                                {
                                    Logger.Debug("Either Queue refresh or no message received.");
                                    continue;
                                }

                                _MessageFormatter = FormatFactory.GetFormatter(strXML, true);

                                Logger.Debug("Event Request SeqNo[" + iSequenceNo.ToString() + "]" + _MessageFormatter.MessageStream);
                            }
                            catch (MessageFilteredException MesEx)
                            {
                                Logger.Error("EventTransmitter", "Transmit", MesEx.Message);
                                Logger.Info("EventTransmitter", "Transmit", "Committing Queue[Filtered Message]");
                                _Message.Commit();
                                continue;
                            }
                            catch (Exception Ex)
                            {
                                Logger.Error("EventTransmitter", "Transmit", Ex);
                                Logger.Info("EventTransmitter", "Transmit", "Error in message:" + strXML);

                                if (Settings.DeleteMessageOnParseErr == 1)
                                {
                                    Logger.Error("EventTransmitter", "Transmit", "Committing Queue[DeleteMessageOnParseErr]");
                                    _Message.Commit();
                                    continue;
                                }
                                _evtTransmitShutDown.WaitOne(5000);
                            }
                            int iRetryOnNWErr = 0;
                            do
                            {
                                if (_evtTransmitShutDown.WaitOne(50))
                                {
                                    break;
                                }

                                try
                                {
                                    if (iRetryOnNWErr >= Settings.iMaxTransmissionRetry)
                                    {
                                        //Close transmission when maximum retry reached 
                                        throw new Exception(String.Format("Stopping Transmision after Retrying [{0}] times. ", iRetryOnNWErr.ToString()));
                                    }
                                    else
                                    {

                                        //transmit message to server 
                                        iResponse = _Sender.Send(_MessageFormatter);
                                         Logger.Debug("Response from Server: " +  iResponse.ToString());
                                        if (iResponse > 0)// to be checked && msg.SequenceNo == bSequenceNo)// ACK
                                        {
                                             
                                            //Set retry value to MAX+1 to quit loop 
                                            iRetryOnNWErr = Settings.iMaxTransmissionRetry + 1;
                                            //Commiting Queue read After succesfull Transaction 
                                            Logger.Info("Committing Queue");
                                            _Message.Commit();
                                        }
                                            
                                        else if (iResponse==0)
                                        {
                                            Logger.Info("Response from Server [0] Message not processed at server");
                                            //Set retry value to MAX+1 to quit loop 
                                            iRetryOnNWErr = Settings.iMaxTransmissionRetry + 1;
                                            //Commiting Queue read After succesfull Transaction 
                                            Logger.Info("Committing Queue");
                                            _Message.Commit();
                                        }
                                        else
                                        {
                                           
                                            if (Settings.EventTransmit_RetryRepeat == 0)
                                            {
                                                iRetryOnNWErr++;
                                                Logger.Info(string.Format("Retrying to send message (Attempt[{0}]) ", iRetryOnNWErr.ToString()));
                                            }
                                            else
                                            {
                                                Logger.Info("Retrying to send message");
                                                iRetryOnNWErr = 0;
                                            }
                                        }
                                    }
                                }
                                catch (Exception Ex1)
                                {
                                    _evtTransmitShutDown.WaitOne(5000);
                                    Logger.Error("EventTransmitter", "Transmit", Ex1);
                                    //refresh connection and retry till configured number of times 
                                    iRetryOnNWErr++;
                                    _Sender.CloseSender();
                                    _IsTransmitting = false;
                                    _Sender.Initialize(Settings.STMServerIP, Settings.STMServerPort);
                                }
                            } while (iRetryOnNWErr <= Settings.iMaxTransmissionRetry);
                        

                    }
                }
            }
            catch (Exception Ex)
            {
                Logger.Error("EventTransmitter", "Transmit", Ex.Message);
                if (Settings.EventTransmit_RecoverOnError != 0)
                {
                    _evtTransmitShutDown.WaitOne(2000);
                    this.Transmit();
                }
            }
            finally
            {
                this.CloseTransmission(); 
            }
           
        }