Produced by Araxis Merge on 2/7/2017 12:14:07 PM Eastern 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 | cpss.zip\cpss\src\main\java\gov\va\cpss\job | CbssBaseJob.java | Wed Feb 1 21:07:26 2017 UTC |
2 | cpss.zip\cpss\src\main\java\gov\va\cpss\job | CbssBaseJob.java | Fri Feb 3 20:50:06 2017 UTC |
Description | Between Files 1 and 2 |
|
---|---|---|
Text Blocks | Lines | |
Unchanged | 2 | 800 |
Changed | 1 | 2 |
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.cpss. job; | |
2 | ||
3 | import jav a.util.Lis t; | |
4 | ||
5 | import org .apache.lo g4j.Logger ; | |
6 | import org .quartz.Jo bExecution Context; | |
7 | import org .quartz.Jo bExecution Exception; | |
8 | import org .quartz.Sc heduler; | |
9 | import org .quartz.Sc hedulerExc eption; | |
10 | import org .springfra mework.bat ch.core.Ex itStatus; | |
11 | import org .springfra mework.bat ch.core.Jo bExecution ; | |
12 | import org .springfra mework.bat ch.core.Jo bParameter s; | |
13 | import org .springfra mework.bat ch.core.jo b.flow.Flo wJob; | |
14 | import org .springfra mework.bat ch.core.la unch.JobLa uncher; | |
15 | import org .springfra mework.bea ns.factory .annotatio n.Autowire d; | |
16 | import org .springfra mework.sch eduling.qu artz.Quart zJobBean; | |
17 | ||
18 | import gov .va.cpss.m odel.Batch Job; | |
19 | import gov .va.cpss.m odel.Batch Run; | |
20 | import gov .va.cpss.p erformance .Performan ceAnalyzer ; | |
21 | import gov .va.cpss.s ervice.Bat chService; | |
22 | import gov .va.cpss.s ervice.Ema ilService; | |
23 | ||
24 | /** | |
25 | * Base jo b class fo r CBSS qua rtz jobs. All custom jobs shou ld extend this class | |
26 | * and imp lement the runJob me thod. | |
27 | * | |
28 | * @author D N S | |
29 | */ | |
30 | public abs tract clas s CbssBase Job extend s QuartzJo bBean { | |
31 | ||
32 | pr otected st atic final Logger jo bLogger = Logger.get Logger(Cbs sBaseJob.c lass.getCa nonicalNam e()); | |
33 | ||
34 | pr ivate stat ic final i nt MAX_ERR OR_LENGTH = 2048; | |
35 | ||
36 | /* * | |
37 | * The batch job name that is po pulated fr om the dat abase at s cheduler | |
38 | * initializ ation. | |
39 | * / | |
40 | pr otected St ring name; | |
41 | ||
42 | /* * | |
43 | * The servi ce used to manage ba tch issues . | |
44 | * / | |
45 | @A utowired | |
46 | pr otected Ba tchService batchServ ice; | |
47 | ||
48 | /* * | |
49 | * Spring Ba tch JobLau ncher for starting F lowJobs | |
50 | * / | |
51 | @A utowired | |
52 | pr otected Jo bLauncher jobLaunche r; | |
53 | ||
54 | /* * | |
55 | * The servi ce used to send emai l. | |
56 | * / | |
57 | @A utowired | |
58 | pr otected Em ailService emailServ ice; | |
59 | ||
60 | /* * | |
61 | * Report me mory usage . | |
62 | * / | |
63 | @A utowired | |
64 | pr otected Pe rformanceA nalyzer me moryAnalyz er; | |
65 | ||
66 | /* | |
67 | * The infor mational p ortion of the messag e for the batch run. | |
68 | * / | |
69 | pr ivate Stri ng infoMes sage; | |
70 | ||
71 | /* | |
72 | * The error portion o f the mess age for th e batch ru n. | |
73 | * / | |
74 | pr ivate Stri ng errorMe ssage; | |
75 | ||
76 | pu blic Strin g getName( ) { | |
77 | retu rn name; | |
78 | } | |
79 | ||
80 | pu blic void setName(St ring name) { | |
81 | this .name = na me; | |
82 | } | |
83 | ||
84 | pu blic Strin g getInfoM essage() { | |
85 | retu rn infoMes sage; | |
86 | } | |
87 | ||
88 | pu blic Strin g getError Message() { | |
89 | retu rn errorMe ssage; | |
90 | } | |
91 | ||
92 | pu blic void resetMessa ges() { | |
93 | info Message = null; | |
94 | erro rMessage = null; | |
95 | } | |
96 | ||
97 | /* * | |
98 | * The entry point for job execu tion. | |
99 | * / | |
100 | @O verride | |
101 | pr otected vo id execute Internal(J obExecutio nContext c tx) throws JobExecut ionExcepti on { | |
102 | ||
103 | // E nsure no o ther quart z job is r unning. | |
104 | if ( notAlready Running(ct x)) { | |
105 | ||
106 | // Rep ort Memory Before Pr ocessing | |
107 | memory Analyzer.r eport("at " + name + " Start") ; | |
108 | ||
109 | // Res et the mes sage. | |
110 | resetM essages(); | |
111 | ||
112 | // Get the batch job. | |
113 | BatchJ ob bj = ba tchService .getBatchJ obByName(n ame); | |
114 | ||
115 | // Sta rt a batch run. | |
116 | BatchR un bR = ba tchService .startRun( bj.getId() ); | |
117 | ||
118 | if (bR != null) { | |
119 | ||
120 | jobLogge r.info("Jo b " + name + " Start ed: " + bR .getStartD ate()); | |
121 | ||
122 | if (getJ ob() == nu ll) { | |
123 | jobLogger. error("Job batch is null"); | |
124 | errorRun(b R); | |
125 | } else i f (runJob( bR, getJob ())) { | |
126 | completeRu n(bR); | |
127 | } else { | |
128 | errorRun(b R); | |
129 | } | |
130 | ||
131 | emailSer vice.cpssR eadyToSend Email(bj.g etEmail(), buildEmai lSubject(b j), buildE mailMessag e(bj, bR)) ; | |
132 | ||
133 | } else { | |
134 | final St ring error Message = "Problem S tarting Jo b " + name ; | |
135 | jobLogge r.error(er rorMessage ); | |
136 | emailSer vice.cpssR eadyToSend Email(bj.g etEmail(), buildEmai lSubject(b j), errorM essage); | |
137 | } | |
138 | ||
139 | // Rep ort Memory After Pro cessing | |
140 | memory Analyzer.r eport("at " + name + " End"); | |
141 | } | |
142 | } | |
143 | ||
144 | /* * | |
145 | * Ensure th e quartz j ob is not already ru nning. | |
146 | * | |
147 | * @param ct x | |
148 | * The job execution context fo r the curr ent job. | |
149 | * @return B oolean fla g indicati ng if the job is not already r unning. | |
150 | * / | |
151 | pr ivate bool ean notAlr eadyRunnin g(JobExecu tionContex t ctx) { | |
152 | ||
153 | bool ean runnin g = false; | |
154 | ||
155 | try { | |
156 | Schedu ler schedu ler = ctx. getSchedul er(); | |
157 | List<J obExecutio nContext> jobs = sch eduler.get CurrentlyE xecutingJo bs(); | |
158 | for (J obExecutio nContext j ob : jobs) { | |
159 | // We ar e only con cerned whe re the job detail is the same as | |
160 | // this job. | |
161 | if (job. getJobDeta il().equal s(ctx.getJ obDetail() )) { | |
162 | // If a di fferent tr igger then it is obv iously a d uplicate | |
163 | // so do n ot allow t o run. | |
164 | // If it i s same tri gger but d ifferent f ire instan ce ID then | |
165 | // it is a lso a dupl icate so d o not allo w to run. | |
166 | if (!job.g etTrigger( ).equals(c tx.getTrig ger())) { | |
167 | ru nning = tr ue; | |
168 | jo bLogger.wa rn( | |
169 | "Attem pted to st art job bu t a differ ent contex t is alrea dy running with a di fferent tr igger"); | |
170 | br eak; | |
171 | } else if (!job.getF ireInstanc eId().equa ls(ctx.get FireInstan ceId())) { | |
172 | ru nning = tr ue; | |
173 | jo bLogger.wa rn( | |
174 | "Attem pted to st art job bu t a differ ent contex t is alrea dy running with the same trigg er"); | |
175 | br eak; | |
176 | } | |
177 | } | |
178 | } | |
179 | } ca tch (Sched ulerExcept ion e) { | |
180 | // If scheduler has a prob lem then a ssume fata l error an d do not r un | |
181 | // job . | |
182 | jobLog ger.error( "Could not check if job alread y running: " + e.get Message()) ; | |
183 | runnin g = true; | |
184 | } | |
185 | ||
186 | retu rn !runnin g; | |
187 | } | |
188 | ||
189 | /* * | |
190 | * Complete and end a successful job run. | |
191 | * | |
192 | * @param bR | |
193 | * The batc h run data base entry . | |
194 | * / | |
195 | pr otected vo id complet eRun(Batch Run bR) { | |
196 | ||
197 | setM essage(bR) ; | |
198 | ||
199 | if ( batchServi ce.complet eRun(bR)) { | |
200 | jobLog ger.info(" Job " + na me + " End ed: " + bR .getEndDat e()); | |
201 | } el se { | |
202 | jobLog ger.error( "Problem E nding Job " + name); | |
203 | } | |
204 | } | |
205 | ||
206 | /* * | |
207 | * Error and end a job run. | |
208 | * | |
209 | * @param bR | |
210 | * The batc h run data base entry . | |
211 | * / | |
212 | pr otected vo id errorRu n(BatchRun bR) { | |
213 | ||
214 | setM essage(bR) ; | |
215 | ||
216 | if ( batchServi ce.errorRu n(bR)) { | |
217 | jobLog ger.error( "Job " + n ame + " Co mpleted wi th Error: " + bR.get EndDate()) ; | |
218 | } el se { | |
219 | jobLog ger.error( "Problem E nding Job " + name + " with Er ror"); | |
220 | } | |
221 | } | |
222 | ||
223 | /* * | |
224 | * Get the b atch job a ssociated with this quartz job . | |
225 | * @return T he associa ted job. | |
226 | * / | |
227 | pr otected ab stract Flo wJob getJo b(); | |
228 | ||
229 | /* * | |
230 | * Entry met hod to run the batch job. Subc lasses sho uld implem ent this | |
231 | * method wi th custom business l ogic. | |
232 | * | |
233 | * @param jo b | |
234 | * The batc h job to r un. | |
235 | * @param bR | |
236 | * The batc h run for this job. | |
237 | * @return B oolean val ue that in dicates if the batch job ran s uccessfull y or | |
238 | * n ot. | |
239 | * / | |
240 | pr otected ab stract boo lean runJo b(BatchRun bR, FlowJ ob job); | |
241 | ||
242 | /* * | |
243 | * Execute t he specifi ed batch j ob using t he specifi ed paramet ers. | |
244 | * | |
245 | * @param jo b | |
246 | * The batc h job. | |
247 | * @param pa rameters | |
248 | * The para meters to use for jo b executio n. | |
249 | * @return B oolean val ue indicat ing succes s or failu re of the job run. | |
250 | * / | |
251 | pr otected Jo bExecution executeJo b(FlowJob job, JobPa rameters p arameters) { | |
252 | ||
253 | JobE xecution e xecution = null; | |
254 | ||
255 | try { | |
256 | ||
257 | execut ion = jobL auncher.ru n(job, par ameters); | |
258 | ||
259 | if (!e xecution.g etExitStat us().equal s(ExitStat us.COMPLET ED)) { | |
260 | if (!exe cution.get ExitStatus ().getExit Descriptio n().isEmpt y()) { | |
261 | appendErro rMessage(e xecution.g etExitStat us().getEx itDescript ion()); | |
262 | } else { | |
263 | appendErro rMessage(e xecution.g etExitStat us().getEx itCode().t oString()) ; | |
264 | } | |
265 | } | |
266 | ||
267 | } ca tch (Excep tion e) { | |
268 | ||
269 | logExe cuteJobExc eption("Ca ught excep tion durin g job exec ution: " + e.getMess age()); | |
270 | ||
271 | if (ex ecution == null) { | |
272 | appendEr rorMessage ("Job ende d with nul l executio n"); | |
273 | } | |
274 | } | |
275 | ||
276 | retu rn executi on; | |
277 | } | |
278 | ||
279 | /* * | |
280 | * Append th e informat ional mess age. This allows cap ture of mu ltiple | |
281 | * informati onal messa ges over t he course of multipl e job runs . | |
282 | * / | |
283 | pr otected vo id appendI nfoMessage (final Str ing messag e) { | |
284 | Stri ngBuilder infoBuilde r = new St ringBuilde r(); | |
285 | if ( (infoMessa ge != null ) && !info Message.is Empty()) { | |
286 | infoBu ilder.appe nd(infoMes sage); | |
287 | infoBu ilder.appe nd("\n"); | |
288 | } | |
289 | info Builder.ap pend(messa ge); | |
290 | info Message = infoBuilde r.toString (); | |
291 | } | |
292 | ||
293 | /* * | |
294 | * Append th e error me ssage. Thi s allows c apture of multiple e rrors over the | |
295 | * course of multiple job runs. | |
296 | * / | |
297 | pr otected vo id appendE rrorMessag e(final St ring messa ge) { | |
298 | Stri ngBuilder errorBuild er = new S tringBuild er(); | |
299 | if ( (errorMess age != nul l) && !err orMessage. isEmpty()) { | |
300 | errorB uilder.app end(errorM essage); | |
301 | errorB uilder.app end("\n"); | |
302 | } el se { | |
303 | errorB uilder.app end("\n\nE rror Messa ge:\n"); | |
304 | } | |
305 | erro rBuilder.a ppend(mess age); | |
306 | erro rMessage = errorBuil der.toStri ng(); | |
307 | } | |
308 | ||
309 | /* * | |
310 | * Log excep tion in ex ecuteJob. Moved to s eparate me thod so su bclasses c an | |
311 | * override. | |
312 | * / | |
313 | pr otected vo id logExec uteJobExce ption(fina l String m essage) { | |
314 | jobL ogger.erro r(message) ; | |
315 | } | |
316 | ||
317 | /* * | |
318 | * Set the b atch run m essage to save in th e database . | |
319 | * / | |
320 | pr ivate void setMessag e(BatchRun bR) { | |
321 | Stri ngBuilder messageBui lder = new StringBui lder(); | |
322 | ||
323 | if ( (infoMessa ge != null ) && !info Message.is Empty()) { | |
324 | messag eBuilder.a ppend(info Message); | |
325 | } | |
326 | ||
327 | if ( (errorMess age != nul l) && !err orMessage. isEmpty()) { | |
328 | messag eBuilder.a ppend(erro rMessage); | |
329 | } | |
330 | ||
331 | Stri ng runMess age = mess ageBuilder .toString( ); | |
332 | ||
333 | if ( runMessage .length() > MAX_ERRO R_LENGTH) { | |
334 | jobLog ger.warn(" Error mess age will b e truncate d in the d atabase"); | |
335 | } | |
336 | ||
337 | // I f necessar y, truncat e error me ssage to m ax size al lowed by t able. | |
338 | bR.s etMessage( runMessage .substring (0, Math.m in(MAX_ERR OR_LENGTH, runMessag e.length() ))); | |
339 | } | |
340 | ||
341 | /* * | |
342 | * Build an email subj ect. | |
343 | * | |
344 | * @return | |
345 | * / | |
346 | pr otected St ring build EmailSubje ct(BatchJo b batchJob ) { | |
347 | retu rn batchJo b.getDescr iption(); | |
348 | } | |
349 | ||
350 | /* * | |
351 | * Build an email mess age that c ommunicate s informat ion based on the | |
352 | * specific type of jo b. | |
353 | * / | |
354 | pr otected St ring build EmailMessa ge(BatchJo b batchJob , BatchRun batchRun) { | |
355 | Stri ngBuffer s trBuff = b uildEmailC ommonInfo( batchJob, batchRun); | |
356 | strB uff.append (buildEmai lCustomInf o()); | |
357 | strB uff.append (buildEmai lErrorInfo ()); | |
358 | retu rn strBuff .toString( ); | |
359 | } | |
360 | ||
361 | /* * | |
362 | * Build the first por tion of an email wit h informat ion that e very job h as | |
363 | * in common . | |
364 | * / | |
365 | pr otected St ringBuffer buildEmai lCommonInf o(BatchJob batchJob, BatchRun batchRun) { | |
366 | Stri ngBuffer s trBuff = n ew StringB uffer(); | |
367 | strB uff.append ("\nBatch Process Na me: "); | |
368 | strB uff.append (batchJob. getDescrip tion()); / / Read the Job class running | |
369 | strB uff.append ("\nStart: "); | |
370 | strB uff.append (batchRun. getServerS tartDateSt ring()); / / Read the start | |
371 | // date | |
372 | strB uff.append ("\nEnd: " ); | |
373 | strB uff.append (batchRun. getServerE ndDateStri ng()); // Read the e nd date | |
374 | strB uff.append ("\nStatus : "); | |
375 | strB uff.append (batchRun. getBatchSt atus().get Descriptio n()); // R ead the | |
376 | // s tatus | |
377 | // d escription | |
378 | retu rn strBuff ; | |
379 | } | |
380 | ||
381 | /* * | |
382 | * Build the error por tion of an email. | |
383 | * / | |
384 | pr otected St ring build EmailError Info() { | |
385 | if ( (errorMess age != nul l) && !err orMessage. isEmpty()) { | |
386 | return errorMess age; | |
387 | } | |
388 | retu rn ""; | |
389 | } | |
390 | ||
391 | /* * | |
392 | * Build the second po rtion of a n email th at contain s informat ion specif ic | |
393 | * to the de rived job class. | |
394 | * / | |
395 | pr otected St ring build EmailCusto mInfo() { | |
396 | if ( (infoMessa ge != null ) && !info Message.is Empty()) { | |
397 | return infoMessa ge; | |
398 | } | |
399 | retu rn ""; | |
400 | } | |
401 | } |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.