KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > util > concurrent > CronThreadPoolExecutor


1 /*
2  * HA-JDBC: High-Availability JDBC
3  * Copyright (c) 2004-2006 Paul Ferraro
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2.1 of the License, or (at your
8  * option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: ferraro@users.sourceforge.net
20  */

21 package net.sf.hajdbc.util.concurrent;
22
23 import java.text.ParseException JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.concurrent.CancellationException JavaDoc;
26 import java.util.concurrent.ExecutionException JavaDoc;
27 import java.util.concurrent.RejectedExecutionException JavaDoc;
28 import java.util.concurrent.RejectedExecutionHandler JavaDoc;
29 import java.util.concurrent.ScheduledThreadPoolExecutor JavaDoc;
30 import java.util.concurrent.TimeUnit JavaDoc;
31
32 import org.quartz.CronTrigger;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Scheduled thread-pool executor implementation that leverages a Quartz cron trigger to calculate future execution times for scheduled tasks.
38  *
39  * @author Paul Ferraro
40  * @since 1.1
41  */

42 public class CronThreadPoolExecutor extends ScheduledThreadPoolExecutor JavaDoc implements CronExecutorService
43 {
44     protected static Logger logger = LoggerFactory.getLogger(CronThreadPoolExecutor.class);
45     
46     /**
47      * Constructs a new CronThreadPoolExecutor.
48      * @param corePoolSize
49      * @param handler
50      */

51     public CronThreadPoolExecutor(int corePoolSize, RejectedExecutionHandler JavaDoc handler)
52     {
53         super(corePoolSize, DaemonThreadFactory.getInstance(), handler);
54     }
55
56     /**
57      * Constructs a new CronThreadPoolExecutor.
58      * @param corePoolSize
59      */

60     public CronThreadPoolExecutor(int corePoolSize)
61     {
62         super(corePoolSize, DaemonThreadFactory.getInstance());
63     }
64     
65     /**
66      * @see net.sf.hajdbc.util.concurrent.CronExecutorService#schedule(java.lang.Runnable, java.lang.String)
67      */

68     public void schedule(final Runnable JavaDoc task, String JavaDoc expression)
69     {
70         if (task == null) throw new NullPointerException JavaDoc();
71         
72         final CronTrigger trigger = new CronTrigger();
73         
74         try
75         {
76             trigger.setCronExpression(expression);
77         }
78         catch (ParseException JavaDoc e)
79         {
80             throw new RejectedExecutionException JavaDoc(e);
81         }
82         
83         this.setCorePoolSize(this.getCorePoolSize() + 1);
84         
85         Runnable JavaDoc scheduleTask = new Runnable JavaDoc()
86         {
87             /**
88              * @see java.lang.Runnable#run()
89              */

90             public void run()
91             {
92                 Date JavaDoc fireTime = trigger.getFireTimeAfter(new Date JavaDoc());
93             
94                 try
95                 {
96                     while (fireTime != null)
97                     {
98                         long delay = Math.max(fireTime.getTime() - System.currentTimeMillis(), 0);
99                         
100                         try
101                         {
102                             CronThreadPoolExecutor.this.schedule(task, delay, TimeUnit.MILLISECONDS).get();
103                             
104                             fireTime = trigger.getFireTimeAfter(new Date JavaDoc());
105                         }
106                         catch (ExecutionException JavaDoc e)
107                         {
108                             logger.warn(e.toString(), e.getCause());
109                         }
110                     }
111                 }
112                 catch (RejectedExecutionException JavaDoc e)
113                 {
114                     // Occurs if executor was already shutdown when schedule() is called
115
}
116                 catch (CancellationException JavaDoc e)
117                 {
118                     // Occurs when scheduled, but not yet executed tasks are cancelled during shutdown
119
}
120                 catch (InterruptedException JavaDoc e)
121                 {
122                     // Occurs when executing tasks are interrupted during shutdownNow()
123
}
124             }
125         };
126         
127         this.execute(scheduleTask);
128     }
129 }
130
Popular Tags