Ejemplo n.º 1
0
        public Messenger(AppSettingDelegate appSettingHandler)
        {
            mAppSettingHandler = appSettingHandler;

            initLogger();

            //zeromq opening connections
            outgoingMessageQueue = new BlockingQueue <String>();
            incomingMessageQueue = new BlockingQueue <String>();
            brokerQueue          = new BlockingQueue <String>();
            fileQueue            = new BlockingQueue <String>();
            loggerQueue          = new BlockingQueue <String>();
            zeromqContext        = new Context(1);
            //reading parameters from the configuration file
            MESSAGING_TYPE        = Convert.ToInt32(GetAppSetting("MessagingType"));
            MAX_QUEUE_SIZE        = Convert.ToInt32(GetAppSetting("MAX_QUEUE_SIZE"));
            MIN_QUEUE_SIZE        = Convert.ToInt32(GetAppSetting("MIN_QUEUE_SIZE"));
            IGNORE_QUEUE_OVERFLOW = Convert.ToInt32(GetAppSetting("IGNORE_QUEUE_OVERFLOW"));
            BROKER = Convert.ToInt32(GetAppSetting("Broker"));
            String addressSend    = GetAppSetting("MessageSendAddress");
            String addressReceive = GetAppSetting("MessageReceiveAddress");

            inFileStorageAddress  = GetAppSetting("InFileStorageAddress");
            outFileStorageAddress = GetAppSetting("OutFileStorageAddress");
            maxFileStorageNum     = Convert.ToInt32(GetAppSetting("MAX_FILE_STORAGE_SIZE"));
            WAIT_COMMAND          = GetAppSetting("WAIT_COMMAND");
            FINISH_COMMAND        = GetAppSetting("FINISH_COMMAND");
            CONTINUE_COMMAND      = GetAppSetting("CONTINUE_COMMAND");
            MESSAGE_REQUEST       = GetAppSetting("MESSAGE_REQUEST");

            ID          = Convert.ToInt32(GetAppSetting("ID"));
            receiverNum = Convert.ToInt32(GetAppSetting("ReceiverNumber"));
            producerNum = Convert.ToInt32(GetAppSetting("ProducerNumber"));
            ratio       = (float)producerNum / receiverNum;

            DB_LOGGING = Convert.ToBoolean(GetAppSetting("DB_LOGGING"));
            //logging receiving socket
            if (DB_LOGGING == true)
            {
                String loggingDestinationPort = GetAppSetting("DBLoggingReceiver");
                loggerEmitter = zeromqContext.Socket(SocketType.PUSH);
                loggerEmitter.Bind(loggingDestinationPort);
                loggerThread = new Thread(new ThreadStart(this.LoggerThreadRun));
                loggerThread.Start();
            }

            switch (MESSAGING_TYPE)
            {
            case PIPELINE:
                //  Socket to send messages on
                if (addressSend != null)
                {
                    sender = zeromqContext.Socket(SocketType.PUSH);
                    sender.Bind(addressSend);

                    // to receive continue and wait messages from message consumer
                    lbReceiver = zeromqContext.Socket(SocketType.SUB);
                    String lbReceiverAddress = GetAppSetting("ReceiveLoadBalancingAdress");
                    //load balancing messages can be received from multiple consumers
                    string[] addresses = lbReceiverAddress.Split(' ');
                    foreach (string address in addresses)
                    {
                        lbReceiver.Connect(address);
                        lbReceiver.Subscribe(GetAppSetting("RECEIVE_COMMAND_FILTER"), Encoding.UTF8);
                    }
                    //starts zeromq messaging thread
                    zerommqSendThread = new Thread(new ThreadStart(this.ZeromqSendThreadRun));
                    zerommqSendThread.Start();
                }


                if (addressReceive != null)
                {
                    receiver = zeromqContext.Socket(SocketType.PULL);

                    string[] addresses = addressReceive.Split(' ');
                    foreach (string address in addresses)
                    {
                        receiver.Connect(address);
                    }
                    // to send continue and wait messages to message producers
                    String lbSenderAddress = GetAppSetting("SendLoadBalancingAddress");
                    lbSender = zeromqContext.Socket(SocketType.PUB);
                    lbSender.Bind(lbSenderAddress);

                    //starts zeromq receive messaging thread
                    zerommqReceiveThread = new Thread(new ThreadStart(this.ZeromqReceiveThreadRun));
                    zerommqReceiveThread.Start();
                }

                break;

            case REQ_REP:
                if (addressSend != null)
                {
                    sender = zeromqContext.Socket(SocketType.REP);
                    sender.Bind(addressSend);

                    //starts zeromq messaging thread
                    zerommqSendThread = new Thread(new ThreadStart(this.ZeromqSendThreadRun));
                    zerommqSendThread.Start();
                }
                break;
            }

            String finishPublishAddress = GetAppSetting("FinishPublish");
            String finishReceiveAddress = GetAppSetting("FinishReceive");

            if (finishPublishAddress != null)
            {
                finishSender = zeromqContext.Socket(SocketType.PUB);
                finishSender.Bind(finishPublishAddress);
            }
            else
            {
                finishReceiver = zeromqContext.Socket(SocketType.SUB);
                finishReceiver.Connect(finishReceiveAddress);
                finishReceiver.Subscribe(GetAppSetting("RECEIVE_COMMAND_FILTER"), Encoding.UTF8);
                finishThread = new Thread(new ThreadStart(this.FinishThreadRun));
                finishThread.Start();
            }

            //Thread for reading message files
            if (IGNORE_QUEUE_OVERFLOW == OFF)
            {
                //Enables file storage for handling data peaks
                fileThread = new Thread(new ThreadStart(this.FileThreadRun));
                //Reads previously written messages if they are not sent to WP4
                readOldMessageFiles();
                fileThread.Start();
            }
            //if (BROKER == ACTIVEMQ)
            //{
            //    try
            //    {
            //        //activemq opening connections
            //        Apache.NMS.ActiveMQ.ConnectionFactory factory = new Apache.NMS.ActiveMQ.ConnectionFactory(GetAppSetting("ACTIVEMQ"));
            //        activeMQconnection = factory.CreateConnection();
            //        Session session = activeMQconnection.CreateSession(AcknowledgementMode.AutoAcknowledge) as Session;
            //        IDestination bqueue = session.GetQueue(GetAppSetting("QueueName"));
            //        activemqSender = session.CreateProducer(bqueue);

            //        brokerThread = new Thread(new ThreadStart(this.ActiveMQBrokerThreadRun));
            //        brokerThread.Start();
            //    }
            //    catch (System.Exception e)
            //    {
            //        //     IGNORE_QUEUE_OVERFLOW = 1;
            //        BROKER = NONE;
            //        logger.Error(e);
            //    }
            //}
        }
Ejemplo n.º 2
0
        public Messenger()
        {
            initLogger();

            //zeromq opening connections
            zeromqQueue   = new BlockingQueue <String>();
            brokerQueue   = new BlockingQueue <String>();
            fileQueue     = new BlockingQueue <String>();
            loggerQueue   = new BlockingQueue <String>();
            zeromqContext = new Context(1);
            //reading parameters from the configuration file
            MESSAGING_TYPE        = Convert.ToInt32(ConfigurationManager.AppSettings.Get("MessagingType"));
            MAX_QUEUE_SIZE        = Convert.ToInt32(ConfigurationManager.AppSettings.Get("MAX_QUEUE_SIZE"));
            IGNORE_QUEUE_OVERFLOW = Convert.ToInt32(ConfigurationManager.AppSettings.Get("IGNORE_QUEUE_OVERFLOW"));
            BROKER = Convert.ToInt32(ConfigurationManager.AppSettings.Get("Broker"));
            String addressSend = ConfigurationManager.AppSettings.Get("WP4MessageAddress");

            fileStorageAddress = ConfigurationManager.AppSettings.Get("FileStorageAddress");
            maxFileStorageNum  = Convert.ToInt32(ConfigurationManager.AppSettings.Get("MAX_FILE_STORAGE_SIZE"));
            WAIT_COMMAND       = ConfigurationManager.AppSettings.Get("WAIT_COMMAND");
            FINISH_COMMAND     = ConfigurationManager.AppSettings.Get("FINISH_COMMAND");
            CONTINUE_COMMAND   = ConfigurationManager.AppSettings.Get("CONTINUE_COMMAND");
            MESSAGE_REQUEST    = ConfigurationManager.AppSettings.Get("MESSAGE_REQUEST");

            DB_LOGGING = Convert.ToBoolean(ConfigurationManager.AppSettings.Get("DB_LOGGING"));
            //logging receiving socket
            if (DB_LOGGING == true)
            {
                String loggingDestinationPort = ConfigurationManager.AppSettings.Get("DBLoggingReceiver");
                loggerEmitter = zeromqContext.Socket(SocketType.PUSH);
                loggerEmitter.Bind(loggingDestinationPort);
                loggerThread = new Thread(new ThreadStart(this.LoggerThreadRun));
                loggerThread.Start();
            }

            switch (MESSAGING_TYPE)
            {
            case PIPELINE:
                //  Socket to send messages on
                zeromqSender = zeromqContext.Socket(SocketType.PUSH);
                //HWM doesnt work to limit zeromq queue size
                //sender.SetSockOpt(SocketOpt.HWM, 10);
                zeromqSender.Bind(addressSend);
                // to receive continue and wait messages from WP4
                String wp4SubscriberAddress = ConfigurationManager.AppSettings.Get("WP4SubscriberAddress");
                zeromqWP4Subscriber = zeromqContext.Socket(SocketType.SUB);
                zeromqWP4Subscriber.Bind(wp4SubscriberAddress);
                zeromqWP4Subscriber.Subscribe(ConfigurationManager.AppSettings.Get("WP4_COMMAND_FILTER"), Encoding.UTF8);
                break;

            case REQ_REP:
                //  Socket to send messages on
                zeromqSender = zeromqContext.Socket(SocketType.REP);
                zeromqSender.Bind(addressSend);
                break;
            }

            //starts zeromq messaging thread
            zerommqThread = new Thread(new ThreadStart(this.ZeromqThreadRun));
            zerommqThread.Start();

            if (IGNORE_QUEUE_OVERFLOW == OFF)
            {
                //Enables file storage for handling data peaks
                fileThread = new Thread(new ThreadStart(this.FileThreadRun));
                //Reads previously written messages if they are not sent to WP4
                readOldMessageFiles();
                fileThread.Start();
            }
            if (BROKER == ACTIVEMQ)
            {
                try
                {
                    //activemq opening connections
                    Apache.NMS.ActiveMQ.ConnectionFactory factory = new Apache.NMS.ActiveMQ.ConnectionFactory(ConfigurationManager.AppSettings.Get("ACTIVEMQ"));
                    activeMQconnection = factory.CreateConnection();
                    Session      session = activeMQconnection.CreateSession(AcknowledgementMode.AutoAcknowledge) as Session;
                    IDestination bqueue  = session.GetQueue(ConfigurationManager.AppSettings.Get("QueueName"));
                    activemqSender = session.CreateProducer(bqueue);

                    brokerThread = new Thread(new ThreadStart(this.ActiveMQBrokerThreadRun));
                    brokerThread.Start();
                }
                catch (System.Exception e)
                {
                    //     IGNORE_QUEUE_OVERFLOW = 1;
                    BROKER = NONE;
                    logger.Error(e);
                }
            }
        }