1 22 package org.objectweb.petals.jbi.component.thread; 23 24 import java.util.concurrent.ArrayBlockingQueue ; 25 import java.util.concurrent.BlockingQueue ; 26 27 import javax.jbi.JBIException; 28 29 33 public abstract class AbstractThread extends Thread { 34 35 38 protected JBIException jbiException; 39 40 48 protected BlockingQueue <Integer > requestQueue; 49 50 59 protected BlockingQueue <Integer > responseQueue; 60 61 64 public static final int SHUTDOWNTHREAD = -1; 65 66 69 public AbstractThread() { 70 super(); 71 this.requestQueue = new ArrayBlockingQueue <Integer >(1); 72 this.responseQueue = new ArrayBlockingQueue <Integer >(1); 73 } 74 75 80 @Override 81 public void run() { 82 boolean run = true; 83 int ret = 0; 84 while (run) { 85 try { 86 Integer action = requestQueue.take(); 89 ret = doTask(action.intValue()); 90 91 if (ret < 0) { 94 run = false; 95 } 96 97 responseQueue.put(action); 99 } catch (Exception e) { 100 e.printStackTrace(); 101 } 102 } 103 return; 104 } 105 106 113 protected abstract int doTask(int action); 114 115 123 protected synchronized void execute(int action) { 124 if (!this.isAlive()) { 126 jbiException = new JBIException("Thread " + this.getName() 127 + " is dead"); 128 return; 129 } 130 131 try { 132 requestQueue.put(new Integer (action)); 136 137 responseQueue.take(); 141 } catch (InterruptedException e1) { 142 e1.printStackTrace(); 143 } 144 } 145 146 152 public JBIException getJbiException() { 153 JBIException jbie = null; 154 if (jbiException != null) { 155 jbie = new JBIException(jbiException); 156 jbiException = null; 157 } 158 return jbie; 159 } 160 161 165 public void shutdownThread() throws JBIException { 166 execute(SHUTDOWNTHREAD); 167 168 JBIException jbie = getJbiException(); 169 if (jbie != null) { 170 throw jbie; 171 } 172 } 173 } 174 | Popular Tags |