1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.orchestra.conversation;
20
21 import java.util.HashSet;
22 import java.util.Set;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import org.apache.myfaces.orchestra.conversation.basic.LogConversationMessager;
28 import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
29 import org.apache.myfaces.orchestra.frameworkAdapter.local.LocalFrameworkAdapter;
30
31
32
33
34
35
36
37
38 public class ConversationWiperThread extends Thread
39 {
40 private final Log log = LogFactory.getLog(ConversationWiperThread.class);
41
42 private final long checkTime;
43
44 private Set conversationManagers = new HashSet();
45
46
47
48
49
50
51
52 public ConversationWiperThread(long checkTime)
53 {
54 this.checkTime = checkTime;
55
56 setDaemon(true);
57 setName(ConversationWiperThread.class.getName());
58 }
59
60
61
62
63
64
65 public void addConversationManager(ConversationManager cm)
66 {
67 synchronized (conversationManagers)
68 {
69 conversationManagers.add(cm);
70 }
71 }
72
73
74
75
76
77
78 public void removeConversationManager(ConversationManager cm)
79 {
80 synchronized (conversationManagers)
81 {
82 boolean found = conversationManagers.remove(cm);
83 if (!found)
84 {
85
86 log.error("Conversation Manager not found in remove");
87 }
88 }
89 }
90
91 public void run()
92 {
93 log.debug("ConversationWiperThread startup");
94 _run();
95 log.debug("ConversationWiperThread shtudown");
96 }
97
98 private void _run()
99 {
100
101
102 FrameworkAdapter fa = new LocalFrameworkAdapter();
103 ConversationMessager conversationMessager = new LogConversationMessager();
104 fa.setConversationMessager(conversationMessager);
105 FrameworkAdapter.setCurrentInstance(fa);
106
107 while (!isInterrupted())
108 {
109 ConversationManager[] managersArray;
110 synchronized (conversationManagers)
111 {
112 managersArray = new ConversationManager[conversationManagers.size()];
113 conversationManagers.toArray(managersArray);
114 }
115
116 if (log.isDebugEnabled())
117 {
118 log.debug("ConversationWiperThread running against " + managersArray.length + " instances.");
119 }
120
121 for (int i = 0; i<managersArray.length; i++)
122 {
123 ConversationManager conversationManager = managersArray[i];
124 conversationManager.checkTimeouts();
125 }
126
127 try
128 {
129 Thread.sleep(checkTime);
130 }
131 catch (InterruptedException e)
132 {
133 return;
134 }
135 }
136 }
137 }