View Javadoc
1   /*
2    * Copyright 2009-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.springframework.batch.admin.jmx;
17  
18  import java.util.Collection;
19  import java.util.Date;
20  
21  import org.springframework.batch.admin.domain.StepExecutionHistory;
22  import org.springframework.batch.admin.service.JobService;
23  import org.springframework.batch.core.StepExecution;
24  import org.springframework.jmx.export.annotation.ManagedResource;
25  
26  @ManagedResource
27  public class SimpleStepExecutionMetrics implements StepExecutionMetrics {
28  
29  	private final JobService jobService;
30  
31  	private final String stepName;
32  
33  	private final String jobName;
34  
35  	public SimpleStepExecutionMetrics(JobService jobService, String jobName, String stepName) {
36  		this.jobService = jobService;
37  		this.jobName = jobName;
38  		this.stepName = stepName;
39  	}
40  
41  	public int getExecutionCount() {
42  		return jobService.countStepExecutionsForStep(jobName, stepName);
43  	}
44  
45  	public int getFailureCount() {
46  		int count = 0;
47  		int start = 0;
48  		int pageSize = 100;
49  		Collection<StepExecution> stepExecutions;
50  		do {
51  			stepExecutions = jobService.listStepExecutionsForStep(jobName, stepName, start, pageSize);
52  			start += pageSize;
53  			for (StepExecution stepExecution : stepExecutions) {
54  				if (stepExecution.getStatus().isUnsuccessful()) {
55  					count++;
56  				}
57  			}
58  		} while (!stepExecutions.isEmpty());
59  		return count;
60  	}
61  
62  	public double getLatestDuration() {
63  		StepExecution stepExecution = getLatestStepExecution(stepName);
64  		if (stepExecution==null) {
65  			return 0;
66  		}
67  		Date endTime = stepExecution.getEndTime();
68  		return (endTime != null ? endTime.getTime() : System.currentTimeMillis())
69  				- stepExecution.getStartTime().getTime();
70  	}
71  
72  	public double getMeanDuration() {
73  		StepExecutionHistory history = computeHistory(stepName);
74  		return history.getDuration().getMean();
75  	}
76  
77  	public double getMaxDuration() {
78  		StepExecutionHistory history = computeHistory(stepName);
79  		return history.getDuration().getMax();
80  	}
81  
82  	public int getLatestReadCount() {
83  		StepExecution stepExecution = getLatestStepExecution(stepName);
84  		return stepExecution == null ? 0 : stepExecution.getReadCount();
85  	}
86  
87  	public int getLatestWriteCount() {
88  		StepExecution stepExecution = getLatestStepExecution(stepName);
89  		return stepExecution == null ? 0 : stepExecution.getWriteCount();
90  	}
91  
92  	public int getLatestFilterCount() {
93  		StepExecution stepExecution = getLatestStepExecution(stepName);
94  		return stepExecution == null ? 0 : stepExecution.getFilterCount();
95  	}
96  
97  	public int getLatestSkipCount() {
98  		StepExecution stepExecution = getLatestStepExecution(stepName);
99  		return stepExecution == null ? 0 : stepExecution.getSkipCount();
100 	}
101 
102 	public int getLatestCommitCount() {
103 		StepExecution stepExecution = getLatestStepExecution(stepName);
104 		return stepExecution == null ? 0 : stepExecution.getCommitCount();
105 	}
106 
107 	public int getLatestRollbackCount() {
108 		StepExecution stepExecution = getLatestStepExecution(stepName);
109 		return stepExecution == null ? 0 : stepExecution.getRollbackCount();
110 	}
111 
112 	public long getLatestExecutionId() {
113 		StepExecution stepExecution = getLatestStepExecution(stepName);
114 		return stepExecution == null ? -1 : stepExecution.getId();
115 	}
116 
117 	public String getLatestStatus() {
118 		StepExecution stepExecution = getLatestStepExecution(stepName);
119 		return stepExecution == null ? "NON" : stepExecution.getStatus().toString();
120 	}
121 
122 	public String getLatestExitCode() {
123 		StepExecution stepExecution = getLatestStepExecution(stepName);
124 		return stepExecution == null ? "NONE" : stepExecution.getExitStatus().getExitCode();
125 	}
126 
127 	public String getLatestExitDescription() {
128 		StepExecution stepExecution = getLatestStepExecution(stepName);
129 		return stepExecution == null ? "" : stepExecution.getExitStatus().getExitDescription();
130 	}
131 
132 	private StepExecutionHistory computeHistory(String stepName) {
133 		// Running average over last 10 executions...
134 		return computeHistory(stepName, 10);
135 	}
136 
137 	private StepExecution getLatestStepExecution(String stepName) {
138 		// On the cautious side: grab the last 4 executions by ID and look for
139 		// the one that was last started...
140 		Collection<StepExecution> stepExecutions = jobService.listStepExecutionsForStep(jobName, stepName, 0, 4);
141 		if (stepExecutions.isEmpty()) {
142 			return null;
143 		}
144 		long lastUpdated = 0L;
145 		StepExecution result = null;
146 		for (StepExecution stepExecution : stepExecutions) {
147 			long updated = stepExecution.getStartTime().getTime();
148 			if (updated > lastUpdated) {
149 				result = stepExecution;
150 				lastUpdated = updated;
151 			}
152 		}
153 		return result;
154 	}
155 
156 	private StepExecutionHistory computeHistory(String stepName, int total) {
157 		StepExecutionHistory stepExecutionHistory = new StepExecutionHistory(stepName);
158 		for (StepExecution stepExecution : jobService.listStepExecutionsForStep(jobName, stepName, 0, total)) {
159 			stepExecutionHistory.append(stepExecution);
160 		}
161 		return stepExecutionHistory;
162 	}
163 
164 }