01: package org.objectweb.salome_tmf.server;
02:
03: import java.util.Enumeration;
04:
05: public class TransactionThread extends Thread {
06:
07: private static long TIMER = 1000 * 60; // une minute
08:
09: private static long TIMEOUT = 1000 * 60 * 60 * 2; // 2 heures
10:
11: private boolean stopThread = false;
12:
13: @Override
14: public void run() {
15: boolean end = false;
16:
17: while (!end) {
18: try {
19: Thread.sleep(TIMER);
20:
21: closeTimeoutConnection();
22:
23: synchronized (this ) {
24: Thread.yield();
25:
26: // lecture du boolean
27: end = this .stopThread;
28: }
29: } catch (InterruptedException e) {
30:
31: }
32: }
33:
34: closeAllConnections();
35: }
36:
37: public synchronized void finish() {
38: this .stopThread = true;
39: }
40:
41: private void closeAllConnections() {
42: Enumeration<TransactionClient> tc = ApiFilter
43: .getTransactionClient().elements();
44:
45: while (tc.hasMoreElements()) {
46: TransactionClient transactionClient = tc.nextElement();
47: try {
48: ApiFilter.rollback(transactionClient.getKey());
49: ApiFilter.closeClientConnection(transactionClient
50: .getKey());
51: } catch (Exception e) {
52: e.printStackTrace();
53: }
54: }
55: }
56:
57: private void closeTimeoutConnection() {
58: Enumeration<TransactionClient> tc = ApiFilter
59: .getTransactionClient().elements();
60:
61: while (tc.hasMoreElements()) {
62: TransactionClient transactionClient = tc.nextElement();
63:
64: if ((System.currentTimeMillis() - transactionClient
65: .getTime()) > TIMEOUT) {
66: try {
67: ApiFilter.rollback(transactionClient.getKey());
68: ApiFilter.closeClientConnection(transactionClient
69: .getKey());
70: } catch (Exception e) {
71: e.printStackTrace();
72: }
73: }
74: }
75: }
76: }
|