1 | /* |
2 | * Copyright 2006-2013 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 | |
17 | package org.springframework.batch.core.repository.dao; |
18 | |
19 | import java.sql.ResultSet; |
20 | import java.sql.SQLException; |
21 | import java.sql.Types; |
22 | import java.util.ArrayList; |
23 | import java.util.List; |
24 | |
25 | import org.springframework.batch.core.DefaultJobKeyGenerator; |
26 | import org.springframework.batch.core.JobExecution; |
27 | import org.springframework.batch.core.JobInstance; |
28 | import org.springframework.batch.core.JobKeyGenerator; |
29 | import org.springframework.batch.core.JobParameters; |
30 | import org.springframework.beans.factory.InitializingBean; |
31 | import org.springframework.dao.DataAccessException; |
32 | import org.springframework.dao.EmptyResultDataAccessException; |
33 | import org.springframework.jdbc.core.ResultSetExtractor; |
34 | import org.springframework.jdbc.core.simple.ParameterizedRowMapper; |
35 | import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer; |
36 | import org.springframework.util.Assert; |
37 | import org.springframework.util.StringUtils; |
38 | |
39 | /** |
40 | * JDBC implementation of {@link JobInstanceDao}. Uses sequences (via Spring's |
41 | * {@link DataFieldMaxValueIncrementer} abstraction) to create all primary keys |
42 | * before inserting a new row. Objects are checked to ensure all mandatory |
43 | * fields to be stored are not null. If any are found to be null, an |
44 | * IllegalArgumentException will be thrown. This could be left to JdbcTemplate, |
45 | * however, the exception will be fairly vague, and fails to highlight which |
46 | * field caused the exception. |
47 | * |
48 | * @author Lucas Ward |
49 | * @author Dave Syer |
50 | * @author Robert Kasanicky |
51 | * @author Michael Minella |
52 | */ |
53 | public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements |
54 | JobInstanceDao, InitializingBean { |
55 | |
56 | private static final String CREATE_JOB_INSTANCE = "INSERT into %PREFIX%JOB_INSTANCE(JOB_INSTANCE_ID, JOB_NAME, JOB_KEY, VERSION)" |
57 | + " values (?, ?, ?, ?)"; |
58 | |
59 | private static final String FIND_JOBS_WITH_NAME = "SELECT JOB_INSTANCE_ID, JOB_NAME from %PREFIX%JOB_INSTANCE where JOB_NAME = ?"; |
60 | |
61 | private static final String FIND_JOBS_WITH_KEY = FIND_JOBS_WITH_NAME |
62 | + " and JOB_KEY = ?"; |
63 | |
64 | private static final String FIND_JOBS_WITH_EMPTY_KEY = "SELECT JOB_INSTANCE_ID, JOB_NAME from %PREFIX%JOB_INSTANCE where JOB_NAME = ? and (JOB_KEY = ? OR JOB_KEY is NULL)"; |
65 | |
66 | private static final String GET_JOB_FROM_ID = "SELECT JOB_INSTANCE_ID, JOB_NAME, JOB_KEY, VERSION from %PREFIX%JOB_INSTANCE where JOB_INSTANCE_ID = ?"; |
67 | |
68 | private static final String GET_JOB_FROM_EXECUTION_ID = "SELECT ji.JOB_INSTANCE_ID, JOB_NAME, JOB_KEY, ji.VERSION from %PREFIX%JOB_INSTANCE ji, " |
69 | + "%PREFIX%JOB_EXECUTION je where JOB_EXECUTION_ID = ? and ji.JOB_INSTANCE_ID = je.JOB_INSTANCE_ID"; |
70 | |
71 | private static final String FIND_JOB_NAMES = "SELECT distinct JOB_NAME from %PREFIX%JOB_INSTANCE order by JOB_NAME"; |
72 | |
73 | private static final String FIND_LAST_JOBS_BY_NAME = "SELECT JOB_INSTANCE_ID, JOB_NAME from %PREFIX%JOB_INSTANCE where JOB_NAME = ? order by JOB_INSTANCE_ID desc"; |
74 | |
75 | private DataFieldMaxValueIncrementer jobIncrementer; |
76 | |
77 | private JobKeyGenerator<JobParameters> jobKeyGenerator = new DefaultJobKeyGenerator(); |
78 | |
79 | /** |
80 | * In this jdbc implementation a job id is obtained by asking the |
81 | * jobIncrementer (which is likely a sequence) for the next long value, and |
82 | * then passing the Id and parameter values into an INSERT statement. |
83 | * |
84 | * @see JobInstanceDao#createJobInstance(String, JobParameters) |
85 | * @throws IllegalArgumentException |
86 | * if any {@link JobParameters} fields are null. |
87 | */ |
88 | @Override |
89 | public JobInstance createJobInstance(String jobName, |
90 | JobParameters jobParameters) { |
91 | |
92 | Assert.notNull(jobName, "Job name must not be null."); |
93 | Assert.notNull(jobParameters, "JobParameters must not be null."); |
94 | |
95 | Assert.state(getJobInstance(jobName, jobParameters) == null, |
96 | "JobInstance must not already exist"); |
97 | |
98 | Long jobId = jobIncrementer.nextLongValue(); |
99 | |
100 | JobInstance jobInstance = new JobInstance(jobId, jobName); |
101 | jobInstance.incrementVersion(); |
102 | |
103 | Object[] parameters = new Object[] { jobId, jobName, |
104 | jobKeyGenerator.generateKey(jobParameters), jobInstance.getVersion() }; |
105 | getJdbcTemplate().update( |
106 | getQuery(CREATE_JOB_INSTANCE), |
107 | parameters, |
108 | new int[] { Types.BIGINT, Types.VARCHAR, Types.VARCHAR, |
109 | Types.INTEGER }); |
110 | |
111 | return jobInstance; |
112 | } |
113 | |
114 | /** |
115 | * The job table is queried for <strong>any</strong> jobs that match the |
116 | * given identifier, adding them to a list via the RowMapper callback. |
117 | * |
118 | * @see JobInstanceDao#getJobInstance(String, JobParameters) |
119 | * @throws IllegalArgumentException |
120 | * if any {@link JobParameters} fields are null. |
121 | */ |
122 | @Override |
123 | public JobInstance getJobInstance(final String jobName, |
124 | final JobParameters jobParameters) { |
125 | |
126 | Assert.notNull(jobName, "Job name must not be null."); |
127 | Assert.notNull(jobParameters, "JobParameters must not be null."); |
128 | |
129 | String jobKey = jobKeyGenerator.generateKey(jobParameters); |
130 | |
131 | ParameterizedRowMapper<JobInstance> rowMapper = new JobInstanceRowMapper(); |
132 | |
133 | List<JobInstance> instances; |
134 | if (StringUtils.hasLength(jobKey)) { |
135 | instances = getJdbcTemplate().query(getQuery(FIND_JOBS_WITH_KEY), |
136 | rowMapper, jobName, jobKey); |
137 | } else { |
138 | instances = getJdbcTemplate().query( |
139 | getQuery(FIND_JOBS_WITH_EMPTY_KEY), rowMapper, jobName, |
140 | jobKey); |
141 | } |
142 | |
143 | if (instances.isEmpty()) { |
144 | return null; |
145 | } else { |
146 | Assert.state(instances.size() == 1); |
147 | return instances.get(0); |
148 | } |
149 | } |
150 | |
151 | /* |
152 | * (non-Javadoc) |
153 | * |
154 | * @see |
155 | * org.springframework.batch.core.repository.dao.JobInstanceDao#getJobInstance |
156 | * (java.lang.Long) |
157 | */ |
158 | @Override |
159 | public JobInstance getJobInstance(Long instanceId) { |
160 | |
161 | try { |
162 | return getJdbcTemplate().queryForObject(getQuery(GET_JOB_FROM_ID), |
163 | new JobInstanceRowMapper(), instanceId); |
164 | } catch (EmptyResultDataAccessException e) { |
165 | return null; |
166 | } |
167 | |
168 | } |
169 | |
170 | /* |
171 | * (non-Javadoc) |
172 | * |
173 | * @see |
174 | * org.springframework.batch.core.repository.dao.JobInstanceDao#getJobNames |
175 | * () |
176 | */ |
177 | @Override |
178 | public List<String> getJobNames() { |
179 | return getJdbcTemplate().query(getQuery(FIND_JOB_NAMES), |
180 | new ParameterizedRowMapper<String>() { |
181 | @Override |
182 | public String mapRow(ResultSet rs, int rowNum) |
183 | throws SQLException { |
184 | return rs.getString(1); |
185 | } |
186 | }); |
187 | } |
188 | |
189 | /* |
190 | * (non-Javadoc) |
191 | * |
192 | * @seeorg.springframework.batch.core.repository.dao.JobInstanceDao# |
193 | * getLastJobInstances(java.lang.String, int) |
194 | */ |
195 | @Override |
196 | @SuppressWarnings("rawtypes") |
197 | public List<JobInstance> getJobInstances(String jobName, final int start, |
198 | final int count) { |
199 | |
200 | ResultSetExtractor extractor = new ResultSetExtractor() { |
201 | |
202 | private List<JobInstance> list = new ArrayList<JobInstance>(); |
203 | |
204 | @Override |
205 | public Object extractData(ResultSet rs) throws SQLException, |
206 | DataAccessException { |
207 | int rowNum = 0; |
208 | while (rowNum < start && rs.next()) { |
209 | rowNum++; |
210 | } |
211 | while (rowNum < start + count && rs.next()) { |
212 | ParameterizedRowMapper<JobInstance> rowMapper = new JobInstanceRowMapper(); |
213 | list.add(rowMapper.mapRow(rs, rowNum)); |
214 | rowNum++; |
215 | } |
216 | return list; |
217 | } |
218 | |
219 | }; |
220 | |
221 | @SuppressWarnings("unchecked") |
222 | List<JobInstance> result = (List<JobInstance>) getJdbcTemplate().query(getQuery(FIND_LAST_JOBS_BY_NAME), |
223 | new Object[] { jobName }, extractor); |
224 | |
225 | return result; |
226 | } |
227 | |
228 | /* |
229 | * (non-Javadoc) |
230 | * |
231 | * @see |
232 | * org.springframework.batch.core.repository.dao.JobInstanceDao#getJobInstance |
233 | * (org.springframework.batch.core.JobExecution) |
234 | */ |
235 | @Override |
236 | public JobInstance getJobInstance(JobExecution jobExecution) { |
237 | |
238 | try { |
239 | return getJdbcTemplate().queryForObject( |
240 | getQuery(GET_JOB_FROM_EXECUTION_ID), |
241 | new JobInstanceRowMapper(), jobExecution.getId()); |
242 | } catch (EmptyResultDataAccessException e) { |
243 | return null; |
244 | } |
245 | } |
246 | |
247 | /** |
248 | * Setter for {@link DataFieldMaxValueIncrementer} to be used when |
249 | * generating primary keys for {@link JobInstance} instances. |
250 | * |
251 | * @param jobIncrementer |
252 | * the {@link DataFieldMaxValueIncrementer} |
253 | */ |
254 | public void setJobIncrementer(DataFieldMaxValueIncrementer jobIncrementer) { |
255 | this.jobIncrementer = jobIncrementer; |
256 | } |
257 | |
258 | @Override |
259 | public void afterPropertiesSet() throws Exception { |
260 | super.afterPropertiesSet(); |
261 | Assert.notNull(jobIncrementer); |
262 | } |
263 | |
264 | /** |
265 | * @author Dave Syer |
266 | * |
267 | */ |
268 | private final class JobInstanceRowMapper implements |
269 | ParameterizedRowMapper<JobInstance> { |
270 | |
271 | public JobInstanceRowMapper() { |
272 | } |
273 | |
274 | @Override |
275 | public JobInstance mapRow(ResultSet rs, int rowNum) throws SQLException { |
276 | JobInstance jobInstance = new JobInstance(rs.getLong(1), rs.getString(2)); |
277 | // should always be at version=0 because they never get updated |
278 | jobInstance.incrementVersion(); |
279 | return jobInstance; |
280 | } |
281 | } |
282 | } |