///////////////////////////////////////////////
        /// work is done here
        ///

        public void doWork()
        {
            Random        rand   = new Random();
            InputDTO      dto    = null;
            ServiceResult res    = null;
            OutputDTO     outDTO = null;

            running = true;
            Console.WriteLine(CommonConstants.currentTimeExtended() + ": Running " + threadName);
            try {
                currentTime = CommonConstants.currentTimeMillis();
                while (!stopping)
                {
                    int pauseTime = rand.Next(71) + 170; // random web request time 170..240
                    if (!workerQueue.IsEmpty)
                    {
                        // prevent blocking loop by checking Q length first
                        dto = getFromWorkerQ();   // may return null
                        if (dto != null)
                        {
                            // simulate processing = Let the thread sleep for a while.
                            Console.WriteLine(CommonConstants.currentTimeExtended() + ": Thread: " + threadName + ", row " + dto.RowNbr + ", Qlen = " + getQLength() +
                                              ", working for " + pauseTime);
                            Thread.Sleep(pauseTime);
                            // send back the results
                            outDTO = new OutputDTO(dto);
                            res    = new ServiceResult(CommonConstants.statusSuccess, outDTO);
                            putToResultQ(res);
                        }
                    }
                    if (getQLength() == 0)
                    {
                        long endTime = CommonConstants.currentTimeMillis();
                        int  deltaT  = (int)(endTime - currentTime);
                        if (deltaT < 100)   // less than a tenth second has elapsed
                        {
                            Console.WriteLine(CommonConstants.currentTimeExtended() + ": Thread: " + threadName + ", Qlen = " + getQLength() + ", sleeping for " + sleepAdj);
                            Thread.Sleep(sleepAdj);
                        }
                        else if (deltaT < almost1Second)     // less than a second has elapsed
                        {
                            sleepTime = 1000 - deltaT - sleepAdj;
                            if (sleepTime < 0)
                            {
                                sleepTime = smallSleepTime;
                            }
                            Console.WriteLine(CommonConstants.currentTimeExtended() + ": Thread: " + threadName + ", Qlen = " + getQLength() + ", sleeping for " + sleepTime);
                            Thread.Sleep(sleepTime);
                        }
                        else
                        {
                            Console.WriteLine(CommonConstants.currentTimeExtended() + ": Thread: " + threadName + ", Qlen = " + getQLength() + ", elapsed " + deltaT + ", sleeping for " + smallSleepTime);
                            Thread.Sleep(smallSleepTime);
                        }
                        //currentTime = System.currentTimeMillis();
                    }
                }
            } catch (ThreadInterruptedException e) {
                Console.WriteLine(CommonConstants.currentTimeExtended() + ": Thread " + threadName + " interrupted.");
            }
            Console.WriteLine(CommonConstants.currentTimeExtended() + ": Thread " + threadName + " exiting.");
            running = false;
        }
Ejemplo n.º 2
0
 public ServiceResult(String result, OutputDTO dto)
 {
     this.result = result;
     this.dto    = dto;
 }
Ejemplo n.º 3
0
 public void setDto(OutputDTO dto)
 {
     this.dto = dto;
 }
Ejemplo n.º 4
0
 public ServiceResult()
 {
     result = "";
     dto    = null;
 }