Exemplo n.º 1
0
        public static void Init ( int poolSize, string ip, int port, int threshold ) {
            _ip = ip;
            _port = port;
            initialPoolSize = poolSize;
            _threshold = threshold;

            for (int i=0; i<poolSize; i++) {
                QueueClient qc = new QueueClient(ip, port);
                qc.ConnectionClosed += queue_ConnectionClosed;
                qc.Connect();
                clients.Enqueue(qc);
            }
            run = true;

            runner = new Thread(new ThreadStart(Process));
            runner.Start();
        }
Exemplo n.º 2
0
        public static void Process () {
            while (run) {
                // if we don't have any messages le'ts just pause and try again
                if (messages.Count == 0) {
                    Thread.Sleep(1000);
                    continue;
                }
                for (int i=0; i<clients.Count; i++) {
                    MessageEvent me = null;

                    if (messages.Count > 0) {
                        me = (MessageEvent)messages.Dequeue();
                        if ((me != null) && (me.Message != null)) {
                            QueueClient qc = null;
                            qc = (QueueClient)clients.Dequeue();
                            me.Client = qc;
                            me.MessageReceived += new MessageEventHandler(queue_MessageReceived);
                            QueueClient.AsyncSend(me);
                            QueueClient.AsyncRecv(me);
                        }
                    }
                }

                if (!run) {
                    break;
                }

                if ((messages.Count > initialPoolSize) && (clients.Count < _threshold)) {
                    // we may need to increase the queue clients pool
                    QueueClient qc = new QueueClient(_ip, _port);
                    qc.ConnectionClosed += queue_ConnectionClosed;
                    try {
                        qc.Connect();
                        clients.Enqueue(qc);
                    } catch (SocketException se) {
                        // we should find a way to warn that we can't create more connections
                        // but this could be one of only so let's try give it a chance to recover
                        Thread.Sleep(5000);
                    }
                } else if ((messages.Count == 0) && (clients.Count > initialPoolSize)) {
                    // or reduce it
                    QueueClient qc = (QueueClient)clients.Dequeue();
                    qc.Disconnect();
                }
            }
        }