1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
package net.admin4j.timer; |
15 | |
|
16 | |
import net.admin4j.util.Admin4jRuntimeException; |
17 | |
import net.admin4j.util.ExpiringCache; |
18 | |
|
19 | |
import org.quartz.JobExecutionContext; |
20 | |
import org.quartz.JobExecutionException; |
21 | |
import org.quartz.JobListener; |
22 | |
import org.slf4j.Logger; |
23 | |
import org.slf4j.LoggerFactory; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | 3 | public class QuartzPerformanceMonitoringJobListener implements JobListener { |
34 | |
|
35 | 3 | private static final ExpiringCache TIMER_CACHE = new ExpiringCache(60000 * 60 * 24, 60000 * 60 * 24); |
36 | 3 | private static final Logger logger = LoggerFactory.getLogger(QuartzPerformanceMonitoringJobListener.class); |
37 | 3 | private String name = "Admin4JBatchJobPerformanceMonitoring"; |
38 | |
|
39 | |
public String getName() { |
40 | 3 | return name; |
41 | |
} |
42 | |
|
43 | |
public void setName(String name) { |
44 | 3 | this.name = name; |
45 | 3 | } |
46 | |
|
47 | |
public void jobExecutionVetoed(JobExecutionContext jobContext) { |
48 | 3 | this.timerStop(jobContext); |
49 | |
|
50 | 3 | } |
51 | |
|
52 | |
public void jobToBeExecuted(JobExecutionContext jobContext) { |
53 | |
try { |
54 | 6 | String contextKey = this.createContextKey(jobContext); |
55 | 6 | String timerLabel = this.createTimerLabel(jobContext); |
56 | 6 | TaskTimer perfMonitor = TaskTimerFactory.start(timerLabel); |
57 | 6 | TIMER_CACHE.put(contextKey, perfMonitor); |
58 | |
} |
59 | 0 | catch (Throwable t) { |
60 | 0 | logger.error("Error starting performance time on Quartz Batch job", |
61 | |
new Admin4jRuntimeException(t).addContextValue("jobContext", jobContext)); |
62 | 6 | } |
63 | 6 | } |
64 | |
|
65 | |
public void jobWasExecuted(JobExecutionContext jobContext, |
66 | |
JobExecutionException jobException) { |
67 | 3 | this.timerStop(jobContext); |
68 | |
|
69 | 3 | } |
70 | |
|
71 | |
private void timerStop(JobExecutionContext jobContext) { |
72 | |
try { |
73 | 6 | String contextKey = this.createContextKey(jobContext); |
74 | 6 | TaskTimer perfMonitor = (TaskTimer)TIMER_CACHE.get(contextKey);; |
75 | 6 | if (perfMonitor != null) { |
76 | 6 | perfMonitor.stop(); |
77 | |
} |
78 | |
} |
79 | 0 | catch (Throwable t) { |
80 | 0 | logger.error("Error starting performance time on Quartz Batch job", |
81 | |
new Admin4jRuntimeException(t).addContextValue("jobContext", jobContext)); |
82 | 6 | } |
83 | 6 | } |
84 | |
|
85 | |
private String createContextKey(JobExecutionContext jobContext) { |
86 | 12 | return createTimerLabel(jobContext) + System.identityHashCode(jobContext); |
87 | |
} |
88 | |
|
89 | |
private String createTimerLabel(JobExecutionContext jobContext) { |
90 | 18 | return "batch-" + jobContext.getJobDetail().getFullName(); |
91 | |
} |
92 | |
|
93 | |
} |