Exemplo n.º 1
0
        protected void RunHandler(int threadCount, object handler)
        {
            if (config.Service == null)
            {
                throw new ZBusException("service required for config");
            }
            if (!(handler is ServiceHandler) && !(handler is WorkerHandler))
            {
                throw new ZBusException("handler invalid");
            }

            ZContext ctx = null;

            if (config.Ctx == null)
            {
                ctx        = new ZContext(1);
                config.Ctx = ctx;
            }

            List <Thread> threads = new List <Thread>();

            foreach (string broker in this.config.Brokers)
            {
                ConnectionConfig connCfg   = new ConnectionConfig();
                WorkerConfig     workerCfg = new WorkerConfig();
                string[]         parts     = broker.Split(':');
                if (parts.Length != 2)
                {
                    string msg = string.Format("broker invalid: {0}", broker);
                    throw new ZBusException(msg);
                }
                connCfg.Host    = parts[0];
                connCfg.Port    = Convert.ToInt32(parts[1]);
                connCfg.Verbose = this.config.Verbose;
                connCfg.Ctx     = ctx;

                workerCfg.Service       = this.config.Service;
                workerCfg.Mode          = this.config.Mode;
                workerCfg.RegisterToken = this.config.RegisterToken;
                workerCfg.AccessToken   = this.config.AccessToken;


                for (int i = 0; i < threadCount; i++)
                {
                    WorkerThreadStart start  = new WorkerThreadStart(connCfg, workerCfg, handler);
                    Thread            thread = new Thread(new ThreadStart(start.Run));
                    threads.Add(thread);
                }
            }

            foreach (Thread thread in threads)
            {
                thread.Start();
            }
            foreach (Thread thread in threads)
            {
                thread.Join();
            }

            if (ctx != null)
            {
                ctx.Dispose();
            }
        }