001/*002 * Copyright 2009-2014 the original author or authors.003 *004 * Licensed under the Apache License, Version 2.0 (the "License");005 * you may not use this file except in compliance with the License.006 * You may obtain a copy of the License at007 *008 * https://www.apache.org/licenses/LICENSE-2.0009 *010 * Unless required by applicable law or agreed to in writing, software011 * distributed under the License is distributed on an "AS IS" BASIS,012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.013 * See the License for the specific language governing permissions and014 * limitations under the License.015 */016package org.springframework.batch.sample.common;017018import java.util.HashMap;019import java.util.Map;020021import javax.sql.DataSource;022023import org.springframework.batch.core.partition.support.Partitioner;024import org.springframework.batch.item.ExecutionContext;025import org.springframework.jdbc.core.JdbcOperations;026import org.springframework.jdbc.core.JdbcTemplate;027028/**029 * Simple minded partitioner for a range of values of a column in a database030 * table. Works best if the values are uniformly distributed (e.g.031 * auto-generated primary key values).032 *033 * @author Dave Syer034 *035 */036public class ColumnRangePartitioner implements Partitioner {037038 private JdbcOperations jdbcTemplate;039040 private String table;041042 private String column;043044 /**045 * The name of the SQL table the data are in.046 *047 * @param table the name of the table048 */049 public void setTable(String table) {050 this.table = table;051 }052053 /**054 * The name of the column to partition.055 *056 * @param column the column name.057 */058 public void setColumn(String column) {059 this.column = column;060 }061062 /**063 * The data source for connecting to the database.064 *065 * @param dataSource a {@link DataSource}066 */067 public void setDataSource(DataSource dataSource) {068 jdbcTemplate = new JdbcTemplate(dataSource);069 }070071 /**072 * Partition a database table assuming that the data in the column specified073 * are uniformly distributed. The execution context values will have keys074 * <code>minValue</code> and <code>maxValue</code> specifying the range of075 * values to consider in each partition.076 *077 * @see Partitioner#partition(int)078 */079 @Override080 public Map<String, ExecutionContext> partition(int gridSize) {