KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > archive > cumulus > ImageConvertQueue


1 /*
2  * Created on Sep 1, 2006
3  */

4 package com.openedit.archive.cumulus;
5
6 import java.util.Timer JavaDoc;
7 import java.util.TimerTask JavaDoc;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11
12 public class ImageConvertQueue
13 {
14     protected Timer JavaDoc fieldQueue;
15     private static final Log log = LogFactory.getLog(ImageConvertQueue.class);
16     
17     protected int fieldCount;
18     protected boolean fieldHasHitMax;
19     
20     
21     public Timer JavaDoc getQueue()
22     {
23         if( fieldQueue == null)
24         {
25             fieldQueue = new Timer JavaDoc(true);
26         }
27         return fieldQueue;
28     }
29
30     public void setQueue(Timer JavaDoc inQueue)
31     {
32         fieldQueue = inQueue;
33     }
34     protected void reduce()
35     {
36         fieldCount--;
37         if( fieldHasHitMax && fieldCount == 0)
38         {
39             log.info("Image Queue is now empty");
40             fieldHasHitMax = false;
41         }
42
43     }
44     protected void increase()
45     {
46         fieldCount++;
47     }
48     
49     public void add(final Runnable JavaDoc inTask)
50     {
51         if(( fieldCount > 300)) //We dont want this queue to get too big in case they cancel
52
{
53             fieldHasHitMax = true;
54             inTask.run();
55             return;
56         }
57         increase();
58         log.debug("Adding " + inTask);
59         TimerTask JavaDoc task = new TimerTask JavaDoc()
60         {
61             public void run()
62             {
63                 try
64                 {
65                     reduce();
66                     log.debug("Running " + inTask);
67                     inTask.run();
68                 }
69                 catch ( Throwable JavaDoc ex)
70                 {
71                     log.error("Image convert failed " + ex.getMessage() + " on " + inTask );
72                 }
73             }
74         };
75         getQueue().schedule(task, 0);
76     }
77 }
78
Popular Tags