KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > component > thread > AbstractThread


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id$
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.component.thread;
23
24 import java.util.concurrent.ArrayBlockingQueue JavaDoc;
25 import java.util.concurrent.BlockingQueue JavaDoc;
26
27 import javax.jbi.JBIException;
28
29 /**
30  * @author Christophe Hamerling - EBM WebSourcing
31  *
32  */

33 public abstract class AbstractThread extends Thread JavaDoc {
34
35     /**
36      *
37      */

38     protected JBIException jbiException;
39
40     /**
41      * The blocking working queue is equal to a FIFO. The worker thread is
42      * waiting for an element to be added in this queue by the caller thread.
43      *
44      * This queue is the synchronization object between caller thread and
45      * execution thread. When an element is added by the caller thread, the
46      * execution thread wakes up and do the task.
47      */

48     protected BlockingQueue JavaDoc<Integer JavaDoc> requestQueue;
49
50     /**
51      * The response queue used to wait for the task to complete. The caller
52      * thread is blocked on this queue while the task is not complted by the
53      * worker thread.
54      *
55      * This queue is the synchronization object between execution thread and
56      * caller thread. When the execution thread put an object in this queue, the
57      * caller thread wakes up.
58      */

59     protected BlockingQueue JavaDoc<Integer JavaDoc> responseQueue;
60
61     /**
62      * Shutdown thread action
63      */

64     public static final int SHUTDOWNTHREAD = -1;
65
66     /**
67      * Creates a new instance of AbstractThread
68      */

69     public AbstractThread() {
70         super();
71         this.requestQueue = new ArrayBlockingQueue JavaDoc<Integer JavaDoc>(1);
72         this.responseQueue = new ArrayBlockingQueue JavaDoc<Integer JavaDoc>(1);
73     }
74
75     /*
76      * (non-Javadoc)
77      *
78      * @see java.lang.Runnable#run()
79      */

80     @Override JavaDoc
81     public void run() {
82         boolean run = true;
83         int ret = 0;
84         while (run) {
85             try {
86                 // waits until there is one element available in the request
87
// queue
88
Integer JavaDoc action = requestQueue.take();
89                 ret = doTask(action.intValue());
90
91                 // if return < 0, stop the while loop and so stop the current
92
// thread
93
if (ret < 0) {
94                     run = false;
95                 }
96
97                 // put element in response element wakes up the caller thread
98
responseQueue.put(action);
99             } catch (Exception JavaDoc e) {
100                 e.printStackTrace();
101             }
102         }
103         return;
104     }
105
106     /**
107      * Excute the task. A JBI exception can occur during the task execution. In
108      * this case, the local jbiException must be filled with this exception and
109      * in shared between client and thread.
110      *
111      * @return -1 if the current thread needs to be shutdowned
112      */

113     protected abstract int doTask(int action);
114
115     /**
116      * Execute the action. If the thread is not alive, throw an exception. On
117      * the other cases, put the action in the FIFO and wait for the task to
118      * complete. This method is the entry point of the task submission, multiple
119      * threads will wait for other threads to complete.
120      *
121      * @param state
122      */

123     protected synchronized void execute(int action) {
124         // thread is alive test
125
if (!this.isAlive()) {
126             jbiException = new JBIException("Thread " + this.getName()
127                 + " is dead");
128             return;
129         }
130
131         try {
132             // put action in request FIFO, this will wait for an available place
133
// in the queue. Since the queue is one element size, block if there
134
// is a current task execution
135
requestQueue.put(new Integer JavaDoc(action));
136
137             // wait response == wait end of execution of the current query,
138
// waiting for the doTask method to complete to be sure that tasks
139
// are not done simultaneously
140
responseQueue.take();
141         } catch (InterruptedException JavaDoc e1) {
142             e1.printStackTrace();
143         }
144     }
145
146     /**
147      * Get the JBIException that has occured during execution. The exception
148      * could be null. Reset the current Exception.
149      *
150      * @return the jbiException or null if no exception occured
151      */

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     /**
162      * Shutdown the current thread
163      *
164      */

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