1 package org.objectweb.celtix.bus.workqueue; 2 3 import java.util.logging.Logger ; 4 5 import org.objectweb.celtix.Bus; 6 import org.objectweb.celtix.bus.busimpl.ComponentCreatedEvent; 7 import org.objectweb.celtix.bus.busimpl.ComponentRemovedEvent; 8 import org.objectweb.celtix.workqueue.AutomaticWorkQueue; 9 import org.objectweb.celtix.workqueue.WorkQueueManager; 10 11 public class WorkQueueManagerImpl implements WorkQueueManager { 12 13 private static final Logger LOG = 14 Logger.getLogger(WorkQueueManagerImpl.class.getName()); 15 16 ThreadingModel threadingModel = ThreadingModel.MULTI_THREADED; 17 AutomaticWorkQueue autoQueue; 18 boolean inShutdown; 19 Bus bus; 20 21 public WorkQueueManagerImpl(Bus b) { 22 bus = b; 23 } 24 25 30 public synchronized AutomaticWorkQueue getAutomaticWorkQueue() { 31 if (autoQueue == null) { 32 autoQueue = createAutomaticWorkQueue(); 33 bus.sendEvent(new ComponentCreatedEvent(this)); 34 } 35 return autoQueue; 36 } 37 38 43 public ThreadingModel getThreadingModel() { 44 return threadingModel; 45 } 46 47 53 public void setThreadingModel(ThreadingModel model) { 54 threadingModel = model; 55 } 56 57 62 public synchronized void shutdown(boolean processRemainingTasks) { 63 inShutdown = true; 64 if (autoQueue != null) { 65 autoQueue.shutdown(processRemainingTasks); 66 } 67 68 70 bus.sendEvent(new ComponentRemovedEvent(this)); 71 72 synchronized (this) { 73 notifyAll(); 74 } 75 } 76 77 public void run() { 78 synchronized (this) { 79 while (!inShutdown) { 80 try { 81 wait(); 82 } catch (InterruptedException ex) { 83 } 85 } 86 while (autoQueue != null && !autoQueue.isShutdown()) { 87 try { 88 Thread.sleep(100); 89 } catch (InterruptedException ex) { 90 } 92 } 93 } 94 for (java.util.logging.Handler h : LOG.getHandlers()) { 95 h.flush(); 96 } 97 98 100 101 } 102 103 private AutomaticWorkQueue createAutomaticWorkQueue() { 104 105 107 int initialThreads = 1; 109 110 int lwm = 5; 112 113 int hwm = 25; 115 116 int maxQueueSize = 10 * hwm; 118 119 long dequeueTimeout = 2 * 60 * 1000L; 121 122 return new AutomaticWorkQueueImpl(maxQueueSize, initialThreads, hwm, lwm, dequeueTimeout); 123 124 } 125 126 } 127 | Popular Tags |