KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > io > sql > DataSourceWorker


1 package prefuse.data.io.sql;
2
3 import java.util.logging.Logger JavaDoc;
4
5 import prefuse.data.Table;
6 import prefuse.data.io.DataIOException;
7 import prefuse.util.PrefuseConfig;
8 import prefuse.util.StringLib;
9 import prefuse.util.collections.CopyOnWriteArrayList;
10
11 /**
12  * Worker thread that asynchronously handles a queue of jobs, with each job
13  * responsible for issuing a query and processing the results. Currently
14  * involves just a single thread, in the future this may be expanded to
15  * thread pool for greater concurrency.
16  *
17  * @author <a HREF="http://jheer.org">jeffrey heer</a>
18  * @see DatabaseDataSource
19  */

20 public class DataSourceWorker extends Thread JavaDoc {
21
22     private static Logger JavaDoc s_logger
23         = Logger.getLogger(DataSourceWorker.class.getName());
24     
25     // TODO: in future, may want to expand this to a thread pool
26
private static DataSourceWorker s_instance;
27     
28     private static CopyOnWriteArrayList s_queue;
29     
30     /**
31      * Submit a job to the worker thread.
32      * @param e an {@link DataSourceWorker.Entry} instance that contains
33      * the parameters of the job.
34      */

35     public synchronized static void submit(Entry e)
36     {
37         // perform lazily initialization as needed
38
if ( s_queue == null )
39             s_queue = new CopyOnWriteArrayList();
40         if ( s_instance == null )
41             s_instance = new DataSourceWorker();
42         
43         // queue it up
44
s_queue.add(e);
45         
46         // wake up a sleepy thread
47
synchronized ( s_instance ) {
48             s_instance.notify();
49         }
50     }
51     
52     // ------------------------------------------------------------------------
53

54     /**
55      * Create a new DataSourceWorker.
56      */

57     private DataSourceWorker() {
58         super("prefuse_DatabaseWorker");
59         
60         int priority = PrefuseConfig.getInt("data.io.worker.threadPriority");
61         if ( priority >= Thread.MIN_PRIORITY &&
62              priority <= Thread.MAX_PRIORITY )
63         {
64             this.setPriority(priority);
65         }
66         this.setDaemon(true);
67         this.start();
68     }
69     
70     /**
71      * @see java.lang.Runnable#run()
72      */

73     public void run() {
74         while ( true ) {
75             Entry e = null;
76             synchronized ( s_queue ) {
77                 if ( s_queue.size() > 0 )
78                     e = (Entry)s_queue.remove(0);
79             }
80             
81             if ( e != null ) {
82                 try {
83                     if ( e.listener != null ) e.listener.preQuery(e);
84                     e.ds.getData(e.table, e.query, e.keyField, e.lock);
85                     if ( e.listener != null ) e.listener.postQuery(e);
86                 } catch ( DataIOException dre ) {
87                     s_logger.warning(dre.getMessage() + "\n"
88                         + StringLib.getStackTrace(dre));
89                 }
90             } else {
91                 // nothing to do, chill out until notified
92
try {
93                     synchronized (this) { wait(); }
94                 } catch (InterruptedException JavaDoc ex) { }
95             }
96         }
97     }
98     
99     /**
100      * Stores the parameters of a data query and processing job.
101      * @author <a HREF="http://jheer.org">jeffrey heer</a>
102      */

103     public static class Entry {
104         /**
105          * Create a new Entry.
106          * @param ds the DatabaseDataSource to query
107          * @param table the Table for storing the results
108          * @param query the query to issue
109          * @param keyField the key field that should be used to identify
110          * when duplicate results occur
111          * @param lock an optional lock to synchronize on when processing
112          * data and adding it to the Table
113          * @param listener an optional callback listener that allows
114          * notifications to be issued before and after query processing
115          */

116         public Entry(DatabaseDataSource ds, Table table, String JavaDoc query,
117                      String JavaDoc keyField, Object JavaDoc lock, Listener listener)
118         {
119             this.ds = ds;
120             this.table = table;
121             this.query = query;
122             this.keyField = keyField;
123             this.lock = lock;
124             this.listener = listener;
125         }
126         
127         /** The DatabaseDataSource to query. */
128         DatabaseDataSource ds;
129         /** An optional callback listener that allows
130          * notifications to be issued before and after query processing. */

131         Listener listener;
132         /** The Table for storing the results. */
133         Table table;
134         /** The query to issue. */
135         String JavaDoc query;
136         /** The key field that should be used to identify
137          * when duplicate results occur. */

138         String JavaDoc keyField;
139         /** An optional lock to synchronize on when processing
140          * data and adding it to the Table. */

141         Object JavaDoc lock;
142     }
143     
144     /**
145      * Listener interface for receiving notifications about the status of
146      * a submitted data query and processing job.
147      * @author <a HREF="http://jheer.org">jeffrey heer</a>
148      */

149     public static interface Listener {
150         /**
151          * Notification that the query is about to be issued.
152          * @param job the current job being processed
153          */

154         public void preQuery(DataSourceWorker.Entry job);
155         /**
156          * Notification that the query processing has just completed.
157          * @param job the current job being processed
158          */

159         public void postQuery(DataSourceWorker.Entry job);
160     }
161     
162 } // end of class DataSourceWorker
163
Popular Tags