KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > core > send > DeliveryPicker


1 /*
2  *
3  * Jsmtpd, Java SMTP daemon
4  * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */

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 /**
30  * checks for mail on mqueue to be delivered, then passes them to a delivery thread
31  * @author Jean-Francois POUX
32  */

33 public class DeliveryPicker extends Thread JavaDoc {
34
35     private ThreadPool pool = null;
36     private boolean running = true;
37     private QueueService queueService;
38
39     public DeliveryPicker() throws InstantiationException JavaDoc, IllegalAccessException JavaDoc, ClassNotFoundException JavaDoc {
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()) //TODO: Also check if memory is available ?
50
{
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 JavaDoc e2) {
63                     }
64                 }
65             } else {
66                 try {
67                     Thread.sleep(100);
68                 } catch (InterruptedException JavaDoc e) {
69                 }
70             }
71         }
72     }
73
74     public void shutdown() {
75         running = false;
76         wake();
77
78         // Wait for the running loop to end ?
79

80         try {
81             this.join();
82         } catch (InterruptedException JavaDoc 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