Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
Processes given SQL query in parallel in multiple data sources (assuming that all data source
contain the same data). Combined results from multiple queries are exposed to application as
java.util.Iterator. Worker thread use spring's JdbcTemplate with named parameters support.
package com.alexkasko.springjdbc.parallel;
import com.alexkasko.springjdbc.parallel.accessor.DataSourceAccessor;
import com.alexkasko.springjdbc.parallel.accessor.RoundRobinAccessor;
import com.google.common.base.Function;
import com.google.common.collect.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.*;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import static com.alexkasko.springjdbc.parallel.NamedParameterJdbcTemplateFunction.NPJT_FUNCTION;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static org.springframework.util.StringUtils.hasText;
/**
* Executes single SQL query to multiple data sources in parallel using provided executor.
* Provides data to client as iterator, {@link ArrayBlockingQueue} is used for temporary buffering.
* Iteration will block awaiting data loaded from sources.
* Typical usage is to get new instance somewhere (spring prototype bean etc.), provide query params
* with start method and iterate over until end.
* Data source exceptions will be propagated as runtime exceptions thrown on 'next()' or 'hasNext()' call.
* All parallel queries will be cancelled on one query error.
* Iterator takes sql in the same format as {@code NamedParameterJdbcTemplate} does (with {@code :palceholders})
* and map declared parameters to provided values using the same methods as {@code NamedParameterJdbcTemplate}.
* NOT thread-safe (tbd: specify points that break thread safety), instance may be reused calling start method, but only in one thread simultaneously.
*
* @author alexkasko
* Date: 6/8/12
* @see ParallelQueriesListener
* @see com.alexkasko.springjdbc.parallel.accessor.DataSourceAccessor
*/
public class ParallelQueriesIterator extends AbstractIterator {
private static final Log logger = LogFactory.getLog(ParallelQueriesIterator.class);
private final Object endOfDataObject = new Object();
private final ExceptionHolder exceptionHolder = new ExceptionHolder();
private final DataSourceAccessor, ?> sources;
private final String sql;
private final RowMapperFactory mapperFactory;
private final ExecutorService executor;
// was made non-generic to allow endOfDataObject
private final ArrayBlockingQueue