1 46 package net.jforum.util.concurrent.executor; 47 48 import net.jforum.util.concurrent.Task; 49 50 import org.apache.log4j.Logger; 51 52 55 public abstract class AbstractWorker implements Runnable 56 { 57 private static final Logger logger = Logger.getLogger(AbstractWorker.class); 58 protected abstract Object take() throws InterruptedException ; 59 60 protected void cleanup() { } 61 62 public void run() 63 { 64 try { 65 while(true) { 66 Object task = take(); 67 68 if(task == null) { 69 break; 70 } 71 72 if(task instanceof Task) { 73 try { 74 ((Task)task).execute(); 75 } catch(Exception e ) { 76 logger.warn("Exception while executing a task: " + e); 77 } 78 } 79 else { 80 SimpleResult sr = (SimpleResult)task; 81 82 try { 83 Object result = sr.getTask().execute(); 84 sr.setResult(result); 85 } 86 catch(Exception e ) { 87 sr.setException(e); 88 } 89 } 90 } 91 } 92 catch (InterruptedException e) { 93 } 95 finally { 96 cleanup(); 97 } 98 } 99 } 100 | Popular Tags |