1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.batch.admin.jmx;
17
18 import javax.management.ObjectName;
19 import javax.management.monitor.GaugeMonitor;
20 import javax.management.monitor.MonitorMBean;
21
22 import org.springframework.beans.factory.FactoryBean;
23 import org.springframework.beans.factory.InitializingBean;
24 import org.springframework.util.Assert;
25
26
27
28
29
30
31
32
33
34
35 public class StepExecutionServiceLevelMonitor implements FactoryBean<GaugeMonitor>, InitializingBean {
36
37 private String defaultDomain = BatchMBeanExporter.DEFAULT_DOMAIN;
38
39 private String stepName;
40
41 private String jobName;
42
43 private int upperThreshold = 0;
44
45 private int lowerThreshold = 0;
46
47 private boolean autoStart = true;
48
49 private String observedAttribute = "LatestDuration";
50
51
52
53
54
55
56
57
58
59
60 public void setObservedAttribute(String observedAttribute) {
61 this.observedAttribute = observedAttribute;
62 }
63
64
65
66
67
68
69 public void setAutoStart(boolean autoStart) {
70 this.autoStart = autoStart;
71 }
72
73
74
75
76
77
78
79
80 public void setDefaultDomain(String defaultDomain) {
81 this.defaultDomain = defaultDomain;
82 }
83
84
85
86
87 public void setStepName(String stepName) {
88 this.stepName = stepName;
89 }
90
91
92
93
94 public void setJobName(String jobName) {
95 this.jobName = jobName;
96 }
97
98
99
100
101
102
103 public void setUpperThreshold(int upperThreshold) {
104 this.upperThreshold = upperThreshold;
105 }
106
107
108
109
110
111
112 public void setLowerThreshold(int lowerThreshold) {
113 this.lowerThreshold = lowerThreshold;
114 }
115
116 public GaugeMonitor getObject() throws Exception {
117 GaugeMonitor monitor = new GaugeMonitor();
118 monitor.setNotifyHigh(true);
119 monitor.addObservedObject(new ObjectName(String.format("%s:type=JobExecution,name=%s,step=%s", defaultDomain,
120 jobName, stepName)));
121 monitor.setObservedAttribute(observedAttribute);
122 if (observedAttribute.endsWith("Duration")) {
123 monitor.setThresholds(new Double(upperThreshold), new Double(lowerThreshold));
124 }
125 else {
126 monitor.setThresholds(new Integer(upperThreshold), new Integer(lowerThreshold));
127 }
128 if (autoStart) {
129 monitor.start();
130 }
131 return monitor;
132 }
133
134 public Class<?> getObjectType() {
135 return GaugeMonitor.class;
136 }
137
138 public boolean isSingleton() {
139 return true;
140 }
141
142 public void afterPropertiesSet() throws Exception {
143 Assert.state(jobName != null, "A Job name must be provided");
144 Assert.state(stepName != null, "A Step name must be provided");
145 Assert.state(upperThreshold > 0, "A threshold must be provided");
146 Assert.state(lowerThreshold < upperThreshold, "A threshold must be provided");
147 if (lowerThreshold == 0) {
148 lowerThreshold = upperThreshold * 8 / 10;
149 }
150 }
151
152 }