1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.service.daemon;
35
36 import fr.paris.lutece.portal.service.plugin.Plugin;
37 import fr.paris.lutece.portal.service.util.AppPropertiesService;
38
39 import java.util.ArrayDeque;
40 import java.util.ArrayList;
41 import java.util.Deque;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Map.Entry;
46
47
48
49
50
51 public class ThreadLauncherDaemon extends Daemon
52 {
53 private static final String PROPERTY_MAX_NUMBER_THREAD = "daemon.threadLauncherDaemon.maxNumberOfThread";
54 private static Deque<RunnableQueueItem> _stackItems = new ArrayDeque<RunnableQueueItem>( );
55 private Map<String, Thread> _mapThreadByKey = new HashMap<String, Thread>( );
56 private List<Thread> _listThread = new ArrayList<Thread>( );
57
58
59
60
61 @Override
62 public void run( )
63 {
64 int nMaxNumberThread = AppPropertiesService.getPropertyInt( PROPERTY_MAX_NUMBER_THREAD, 5 );
65
66
67 RunnableQueueItem item = null;
68 List<String> listDeadThreadKeys = new ArrayList<String>( );
69
70 for ( Entry<String, Thread> threadEntry : _mapThreadByKey.entrySet( ) )
71 {
72 if ( !threadEntry.getValue( ).isAlive( ) )
73 {
74 listDeadThreadKeys.add( threadEntry.getKey( ) );
75 }
76 }
77
78 for ( String strThreadKey : listDeadThreadKeys )
79 {
80 _mapThreadByKey.remove( strThreadKey );
81 }
82
83 List<Thread> listDeadThreads = new ArrayList<Thread>( );
84
85 for ( Thread thread : _listThread )
86 {
87 if ( !thread.isAlive( ) )
88 {
89 listDeadThreads.add( thread );
90 }
91 }
92
93 for ( Thread thread : listDeadThreads )
94 {
95 _listThread.remove( thread );
96 }
97
98 int nCurrentNumberRunningThreads = _mapThreadByKey.size( ) + _listThread.size( );
99
100 List<RunnableQueueItem> listLockedItems = new ArrayList<RunnableQueueItem>( );
101
102 while ( ( nCurrentNumberRunningThreads < nMaxNumberThread ) && ( ( item = popItemFromQueue( ) ) != null ) )
103 {
104
105 if ( ( item.getKey( ) != null ) && ( item.getPlugin( ) != null ) )
106 {
107 Thread thread = _mapThreadByKey.get( item.computeKey( ) );
108
109 if ( thread != null )
110 {
111 if ( thread.isAlive( ) )
112 {
113
114 listLockedItems.add( item );
115 }
116 else
117 {
118
119
120 thread = new Thread( item.getRunnable( ) );
121 thread.start( );
122 _mapThreadByKey.put( item.computeKey( ), thread );
123
124
125 }
126 }
127 else
128 {
129
130 thread = new Thread( item.getRunnable( ) );
131 thread.start( );
132 _mapThreadByKey.put( item.computeKey( ), thread );
133 nCurrentNumberRunningThreads++;
134 }
135 }
136 else
137 {
138
139 Thread thread = new Thread( item.getRunnable( ) );
140 thread.start( );
141 _mapThreadByKey.put( item.computeKey( ), thread );
142 nCurrentNumberRunningThreads++;
143 }
144 }
145
146
147 for ( RunnableQueueItem itemQueue : listLockedItems )
148 {
149 addItemToQueue( itemQueue );
150 }
151
152
153 if ( nCurrentNumberRunningThreads >= nMaxNumberThread )
154 {
155 setLastRunLogs( "Every threads are running. Daemon execution ending." );
156
157 return;
158 }
159
160 setLastRunLogs( "There is no more runnable to launch." );
161 }
162
163
164
165
166
167
168
169
170
171
172 public static void addItemToQueue( Runnable runnable, String strKey, Plugin plugin )
173 {
174 RunnableQueueItem runnableItem = new RunnableQueueItem( runnable, strKey, plugin );
175
176 synchronized ( ThreadLauncherDaemon.class )
177 {
178 _stackItems.addLast( runnableItem );
179 }
180 }
181
182
183
184
185
186 private static synchronized void addItemToQueue( RunnableQueueItem runnableItem )
187 {
188 _stackItems.addLast( runnableItem );
189 }
190
191
192
193
194
195 private static synchronized RunnableQueueItem popItemFromQueue( )
196 {
197 if ( _stackItems.size( ) == 0 )
198 {
199 return null;
200 }
201
202 return _stackItems.pop( );
203 }
204
205
206
207
208
209 public static synchronized Integer countItemsInQueue( )
210 {
211 return _stackItems.size( );
212 }
213 }