1 21 package org.jsmtpd.core.send; 22 23 import org.jsmtpd.config.ReadConfig; 24 import org.jsmtpd.core.mail.Email; 25 import org.jsmtpd.generic.threadpool.BusyThreadPoolException; 26 import org.jsmtpd.generic.threadpool.GrowingThreadPool; 27 import org.jsmtpd.generic.threadpool.ThreadPool; 28 29 33 public class DeliveryPicker extends Thread { 34 35 private ThreadPool pool = null; 36 private boolean running = true; 37 private QueueService queueService; 38 39 public DeliveryPicker() throws InstantiationException , IllegalAccessException , ClassNotFoundException { 40 ReadConfig cfg = ReadConfig.getInstance(); 41 queueService= QueueService.getInstance(); 42 int numThreads = cfg.getDMaxInstances(); 43 pool = new GrowingThreadPool(numThreads, "org.jsmtpd.core.send.DeliveryWorker","S"); 44 this.start(); 45 } 46 47 public void run() { 48 while (running) { 49 if (pool.hasFreeThread()) { 51 Email e = null; 52 e = queueService.getEmail(); 53 if (e != null) { 54 try { 55 pool.assignFreeThread(e); 56 } catch (BusyThreadPoolException e1) { 57 queueService.requeueMail(e); 58 } 59 } else { 60 try { 61 Thread.sleep(100); 62 } catch (InterruptedException e2) { 63 } 64 } 65 } else { 66 try { 67 Thread.sleep(100); 68 } catch (InterruptedException e) { 69 } 70 } 71 } 72 } 73 74 public void shutdown() { 75 running = false; 76 wake(); 77 78 80 try { 81 this.join(); 82 } catch (InterruptedException e) { 83 e.printStackTrace(); 84 } 85 86 pool.forceShutdown(); 87 queueService.shutdownService(); 88 89 } 90 91 public void wake() { 92 this.interrupt(); 93 } 94 } | Popular Tags |