Produced by Araxis Merge on 12/5/2017 12:06:37 PM Central Standard Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
| # | Location | File | Last Modified |
|---|---|---|---|
| 1 | IV-eHMP_CIF.zip\IMAG_Source\VISA\Java\CoreRouter\main\src\java\gov\va\med\imaging\core\router | AsynchronousCommandExecutor.java | Mon Dec 4 21:34:54 2017 UTC |
| 2 | IV-eHMP_CIF.zip\IMAG_Source\VISA\Java\CoreRouter\main\src\java\gov\va\med\imaging\core\router | AsynchronousCommandExecutor.java | Mon Dec 4 21:58:25 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 3 | 666 |
| Changed | 2 | 6 |
| Inserted | 0 | 0 |
| Removed | 0 | 0 |
| Whitespace | |
|---|---|
| Character case | Differences in character case are significant |
| Line endings | Differences in line endings (CR and LF characters) are ignored |
| CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
| 1 | package go v.va.med.i maging.cor e.router; | |
| 2 | ||
| 3 | import gov .va.med.im aging.core .interface s.router.A synchronou sCommandRe sult; | |
| 4 | import gov .va.med.im aging.core .interface s.router.C ommand; | |
| 5 | import gov .va.med.im aging.core .interface s.router.C ommandStat us; | |
| 6 | import gov .va.med.im aging.core .router.co mmands.con figuration .CommandCo nfiguratio n; | |
| 7 | import gov .va.med.im aging.core .router.qu eue.Asynch ronousComm andProcess orPriority BlockingQu eue; | |
| 8 | ||
| 9 | import jav a.util.Que ue; | |
| 10 | ||
| 11 | import org .apache.lo gging.log4 j.LogManag er; | |
| 12 | import org .apache.lo gging.log4 j.Logger; | |
| 13 | ||
| 14 | /** | |
| 15 | * Thread pool manag er to hand le worker threads an d process them as th ey are req uested. | |
| 16 | * This cl ass is sim ilar to a java.util. ExecutorCo mpletionSe rvice impl ementation | |
| 17 | * but doe s not deri ve or use that class . | |
| 18 | * | |
| 19 | * @author
|
|
| 20 | * @author
|
|
| 21 | * | |
| 22 | */ | |
| 23 | public cla ss Asynchr onousComma ndExecutor | |
| 24 | { | |
| 25 | pr ivate enum Asynchron ousCommand ExecutorTh readState {NEW, WAIT ING, BUSY} ; | |
| 26 | ||
| 27 | pr ivate fina l Asynchro nousComman dProcessor PriorityBl ockingQueu e workQueu e; | |
| 28 | pr ivate fina l ThreadGr oup thread Group; | |
| 29 | pr ivate fina l Queue<As ynchronous CommandRes ult<?>> re sultsQueue ; | |
| 30 | pr ivate fina l Logger l ogger = Lo gManager.g etLogger(t his.getCla ss()); | |
| 31 | pr ivate Inte ger thread SerialNumb er = 0; | |
| 32 | pr ivate bool ean shutti ngDown = f alse; | |
| 33 | ||
| 34 | pr ivate int minimumThr eadCount = -1; // de fault to 1 00 minimum threads | |
| 35 | pr ivate fina l int bare MinimumThr eadCount = 100; // i f it can't read from config fi le, use th is value | |
| 36 | ||
| 37 | /* * | |
| 38 | * Create an instance with no re sults queu e. | |
| 39 | * Results w ill not be available . | |
| 40 | * / | |
| 41 | pu blic Async hronousCom mandExecut or() | |
| 42 | { | |
| 43 | this .workQueue = new Asy nchronousC ommandProc essorPrior ityBlockin gQueue(); | |
| 44 | this .threadGro up = new T hreadGroup ("Asynchro nousComman dExecutor" ); | |
| 45 | this .resultsQu eue = null ; | |
| 46 | } | |
| 47 | ||
| 48 | /* * | |
| 49 | * | |
| 50 | * @param re sultsQueue | |
| 51 | * / | |
| 52 | pu blic Async hronousCom mandExecut or(Queue<A synchronou sCommandRe sult<?>> r esultsQueu e) | |
| 53 | { | |
| 54 | this .workQueue = new Asy nchronousC ommandProc essorPrior ityBlockin gQueue(); | |
| 55 | this .threadGro up = new T hreadGroup ("Asynchro nousComman dExecutor" ); | |
| 56 | this .resultsQu eue = resu ltsQueue; | |
| 57 | } | |
| 58 | ||
| 59 | privat e Asynchro nousComman dProcessor PriorityBl ockingQueu e getWorkQ ueue() | |
| 60 | { | |
| 61 | re turn workQ ueue; | |
| 62 | } | |
| 63 | ||
| 64 | privat e ThreadGr oup getThr eadGroup() | |
| 65 | { | |
| 66 | re turn threa dGroup; | |
| 67 | } | |
| 68 | ||
| 69 | pr ivate Queu e<Asynchro nousComman dResult<?> > getResul tsQueue() | |
| 70 | { | |
| 71 | re turn resul tsQueue; | |
| 72 | } | |
| 73 | ||
| 74 | pr otected Lo gger getLo gger() | |
| 75 | { | |
| 76 | re turn logge r; | |
| 77 | } | |
| 78 | ||
| 79 | pr otected bo olean isSh uttingDown () | |
| 80 | { | |
| 81 | re turn shutt ingDown; | |
| 82 | } | |
| 83 | ||
| 84 | pr ivate int getMinimum ThreadCoun t() | |
| 85 | { | |
| 86 | if(m inimumThre adCount <= 0) | |
| 87 | { | |
| 88 | minimu mThreadCou nt = Comma ndConfigur ation.getC ommandConf iguration( ).getMinim umThreadCo unt(); | |
| 89 | if(min imumThread Count <= 0 ) | |
| 90 | minimumT hreadCount = bareMin imumThread Count; | |
| 91 | } | |
| 92 | return m inimumThre adCount; | |
| 93 | } | |
| 94 | ||
| 95 | pu blic void setMinimum ThreadCoun t(int mini mumThreadC ount) | |
| 96 | { | |
| 97 | this .minimumTh readCount = minimumT hreadCount ; | |
| 98 | } | |
| 99 | ||
| 100 | /* * | |
| 101 | * Get the s tatus of t he given c ommand. | |
| 102 | * NOTE: thi s uses the Command i nterface a nd the .eq uals() as defined in | |
| 103 | * the Comma nd impleme ntations t o determin e which Co mmand refe rence is t he subject | |
| 104 | * of the st atus inqui ry. | |
| 105 | * NOTE: thi s method m akes no at tempt to a ssure the consistenc y of resul ts, nor do es | |
| 106 | * it attemp t to synch ronize the state of the variou s queues i nvolved. Practicall y this | |
| 107 | * means tha t the stat us of a co mmand as r eturned fr om this co mmand may not follow | |
| 108 | * the expec ted lifecy cle, nor w ill a comm and betwee n states b e correctl y reported . | |
| 109 | * | |
| 110 | * The resul t may be e ither WAIT ING, EXECU TING, COMP LETE or UN KNOWN. | |
| 111 | * | |
| 112 | * @param co mmand | |
| 113 | * @return | |
| 114 | * / | |
| 115 | pu blic Comma ndStatus g etCommandS tatus(Comm and<?> com mand) | |
| 116 | { | |
| 117 | // c heck the q ueues in o rder of op eration fo r most con sistent | |
| 118 | // r esults | |
| 119 | if( getWorkQue ue().conta ins(comman d) ) | |
| 120 | return CommandSt atus.WAITI NG; | |
| 121 | ||
| 122 | if(i sCommandEx ecuting(co mmand)) | |
| 123 | return CommandSt atus.EXECU TING; | |
| 124 | ||
| 125 | if( isCommandI nCompleteQ ueue(comma nd) ) | |
| 126 | return CommandSt atus.COMPL ETE; | |
| 127 | ||
| 128 | retu rn Command Status.UNK NOWN; | |
| 129 | } | |
| 130 | ||
| 131 | pu blic boole an isComma ndInComple teQueue(Co mmand<?> c ommand) | |
| 132 | { | |
| 133 | if(c ommand != null && ge tResultsQu eue() != n ull) | |
| 134 | for(As ynchronous CommandRes ult<?> res ult : (Asy nchronousC ommandResu lt<?> [])g etResultsQ ueue().toA rray() ) | |
| 135 | if(comma nd .equals ( result.g etCommand( ) ) ) | |
| 136 | return tru e; | |
| 137 | ||
| 138 | retu rn false; | |
| 139 | } | |
| 140 | ||
| 141 | /* * | |
| 142 | * | |
| 143 | * @param co mmandProce ssor | |
| 144 | * / | |
| 145 | pu blic void execute(Ab stractComm andImpl<?> commandPr ocessor) | |
| 146 | { | |
| 147 | if(i sShuttingD own()) | |
| 148 | return ; | |
| 149 | ||
| 150 | wo rkQueue.of fer(comman dProcessor ); | |
| 151 | ||
| 152 | ad justThread Pool(); | |
| 153 | } | |
| 154 | ||
| 155 | /* * | |
| 156 | * Someday, replace th is method with a Thr ead Manage ment Strat egy | |
| 157 | * / | |
| 158 | pr ivate void adjustThr eadPool() | |
| 159 | { | |
| 160 | // int highPr iorityElem ents = wor kQueue.acc essibleOfP riority(Sc heduledPri orityQueue Element.Pr iority.HIG H); | |
| 161 | // if(highPri orityEleme nts > 0) | |
| 162 | // getL ogger().wa rn("There are curren tly " + hi ghPriority Elements + " high pr iority ele ments wait ing for pr ocesing.") ; | |
| 163 | ||
| 164 | for( int active Count = ge tThreadGro up().activ eCount(); activeCoun t < getMin imumThread Count(); + +activeCou nt) | |
| 165 | startN ewThread() ; | |
| 166 | ||
| 167 | } | |
| 168 | ||
| 169 | pu blic void shutdown() | |
| 170 | { | |
| 171 | this .shuttingD own = true ; | |
| 172 | } | |
| 173 | ||
| 174 | pu blic int g etWaitingT hreadCount () | |
| 175 | { | |
| 176 | retu rn getThre adCountInS tate(Async hronousCom mandExecut orThreadSt ate.WAITIN G); | |
| 177 | } | |
| 178 | ||
| 179 | pu blic int g etBusyThre adCount() | |
| 180 | { | |
| 181 | retu rn getThre adCountInS tate(Async hronousCom mandExecut orThreadSt ate.BUSY); | |
| 182 | } | |
| 183 | ||
| 184 | privat e int getT hreadCount InState(As ynchronous CommandExe cutorThrea dState sta te) | |
| 185 | { | |
| 186 | Th read[] thr eads = new Thread[ge tThreadGro up().activ eCount() + 10]; | |
| 187 | in t countInS tate = 0; | |
| 188 | ge tThreadGro up().enume rate(threa ds); | |
| 189 | ||
| 190 | fo r(Thread t hread : th reads) | |
| 191 | if( ((Asynchro nousComman dExecutorT hread)thre ad).getThr eadState() == state ) | |
| 192 | countI nState++; | |
| 193 | ||
| 194 | re turn count InState; | |
| 195 | } | |
| 196 | ||
| 197 | privat e boolean isCommandE xecuting(C ommand<?> command) | |
| 198 | { | |
| 199 | if (command = = null) | |
| 200 | retu rn false; | |
| 201 | ||
| 202 | Th read[] thr eads = new Thread[ge tThreadGro up().activ eCount() + 10]; | |
| 203 | ge tThreadGro up().enume rate(threa ds); | |
| 204 | ||
| 205 | fo r(Thread t hread : th reads) | |
| 206 | if( command.eq uals( ((As ynchronous CommandExe cutorThrea d)thread). getExecuti ngCommand( ) ) ) | |
| 207 | return true; | |
| 208 | ||
| 209 | re turn false ; | |
| 210 | } | |
| 211 | ||
| 212 | /** | |
| 213 | * | |
| 214 | * @re turn | |
| 215 | */ | |
| 216 | pr ivate void startNewT hread() | |
| 217 | { | |
| 218 | Asyn chronousCo mmandExecu torThread thread = n ull; | |
| 219 | sync hronized ( threadSeri alNumber) | |
| 220 | { | |
| 221 | // thi s will hap pen someti me around the time t he sun col lapses to a brown dw arf | |
| 222 | if(thr eadSerialN umber.intV alue() == Integer.MA X_VALUE) | |
| 223 | threadSe rialNumber = 0; | |
| 224 | ||
| 225 | thread = | |
| 226 | new Asyn chronousCo mmandExecu torThread( threadGrou p, threadG roup.getNa me() + "-" + threadS erialNumbe r); | |
| 227 | thread .setDaemon (true); | |
| 228 | thread .start(); | |
| 229 | ||
| 230 | thread SerialNumb er = threa dSerialNum ber.intVal ue() + 1; | |
| 231 | } | |
| 232 | } | |
| 233 | ||
| 234 | /* * | |
| 235 | * | |
| 236 | * @author
|
|
| 237 | * / | |
| 238 | cl ass Asynch ronousComm andExecuto rThread | |
| 239 | ex tends Thre ad | |
| 240 | { | |
| 241 | priv ate Abstra ctCommandI mpl<?> exe cutingComm and = null ; | |
| 242 | priv ate Asynch ronousComm andExecuto rThreadSta te state; | |
| 243 | priv ate boolea n sepuku = false; | |
| 244 | ||
| 245 | Asyn chronousCo mmandExecu torThread( ThreadGrou p group, S tring name ) | |
| 246 | { | |
| 247 | supe r(group, n ame); | |
| 248 | this .state = Asynchrono usCommandE xecutorThr eadState.N EW; | |
| 249 | } | |
| 250 | ||
| 251 | prot ected Asyn chronousCo mmandExecu torThreadS tate getTh readState( ) | |
| 252 | { | |
| 253 | retu rn state; | |
| 254 | } | |
| 255 | ||
| 256 | void hariKari( ) | |
| 257 | { | |
| 258 | sepuku = true; | |
| 259 | if( th is.getStat e() == Thr ead.State. WAITING && this.getT hreadState () == Asyn chronousCo mmandExecu torThreadS tate.WAITI NG ) | |
| 260 | this.int errupt(); | |
| 261 | } | |
| 262 | ||
| 263 | prot ected bool ean isSepu ku() | |
| 264 | { | |
| 265 | retu rn sepuku; | |
| 266 | } | |
| 267 | ||
| 268 | /** | |
| 269 | * @ return the executing Command or null if t hread is w aiting for a task | |
| 270 | */ | |
| 271 | publ ic Abstrac tCommandIm pl<?> getE xecutingCo mmand() | |
| 272 | { | |
| 273 | return this.exec utingComma nd; | |
| 274 | } | |
| 275 | ||
| 276 | @Ove rride | |
| 277 | pu blic void run() | |
| 278 | { | |
| 279 | while( ! isShutti ngDown() & & !isSepuk u()) | |
| 280 | { | |
| 281 | try | |
| 282 | { | |
| 283 | this.state = Asynchr onousComma ndExecutor ThreadStat e.WAITING; | |
| 284 | // will wa it here fo r the next work queu e item | |
| 285 | executingC ommand = g etWorkQueu e().take() ; | |
| 286 | try | |
| 287 | { | |
| 288 | th is.state = Asynchron ousCommand ExecutorTh readState. BUSY; | |
| 289 | bo olean exec uteCommand = true; | |
| 290 | if (executing Command.is Periodic() ) | |
| 291 | { | |
| 292 | if(e xecutingCo mmand.isPe riodicProc essingTerm inated()) | |
| 293 | { | |
| 294 | // mak e sure thi s is remov ed from th e list of executing commands | |
| 295 | Period icCommandL ist.get(). removeSche duledComma nd(executi ngCommand) ; | |
| 296 | execut eCommand = false; | |
| 297 | logger .warn("Per iodic Comm and '" + e xecutingCo mmand.getC lass().get Name() + " ' has been terminate d and will not execu te."); | |
| 298 | } | |
| 299 | else | |
| 300 | { | |
| 301 | // if the execut ing comman d is not a lready in the list o f schedule d commands , add it t o track it while it runs | |
| 302 | Period icCommandL ist.get(). addIfNotAl readySched uled(execu tingComman d); | |
| 303 | } | |
| 304 | } | |
| 305 | if (executeCo mmand) | |
| 306 | { | |
| 307 | Asyn chronousCo mmandResul t<?> resul t = execut ingCommand .call(); | |
| 308 | if(g etResultsQ ueue() != null) | |
| 309 | getRes ultsQueue( ).add(resu lt); | |
| 310 | } | |
| 311 | } | |
| 312 | catch(Thro wable t) | |
| 313 | { | |
| 314 | ge tLogger(). error( | |
| 315 | t.ge tClass().g etName() + | |
| 316 | " '" + t.getMe ssage() + "'" + | |
| 317 | (t.g etCause() != null ? (" caused by '" + t. getCause() .getClass( ).getName( ) + "'") : "") | |
| 318 | ); | |
| 319 | ||
| 320 | if (t.getCaus e() != nul l) | |
| 321 | t.ge tCause().p rintStackT race(); | |
| 322 | el se | |
| 323 | t.pr intStackTr ace(); | |
| 324 | } | |
| 325 | } | |
| 326 | catch (I nterrupted Exception e) | |
| 327 | { | |
| 328 | if(! isSep uku()) | |
| 329 | lo gger.warn( "Unexpecte d Interrup tedExcepti on, ignori ng ...."); | |
| 330 | } | |
| 331 | ||
| 332 | executin gCommand = null; | |
| 333 | } | |
| 334 | } | |
| 335 | } | |
| 336 | } |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.