use of org.sagacity.sqltoy.dialect.executor.ParallQueryExecutor in project sagacity-sqltoy by chenrenfei.
the class SqlToyDaoSupport method parallQuery.
/**
* @TODO 并行查询并返回一维List,有几个查询List中就包含几个结果对象,paramNames和paramValues是全部sql的条件参数的合集
* @param parallQueryList
* @param paramNames
* @param paramValues
* @param parallelConfig
* @return
*/
protected <T> List<QueryResult<T>> parallQuery(List<ParallQuery> parallQueryList, String[] paramNames, Object[] paramValues, ParallelConfig parallelConfig) {
if (parallQueryList == null || parallQueryList.isEmpty()) {
return null;
}
ParallelConfig parallConfig = parallelConfig;
if (parallConfig == null) {
parallConfig = new ParallelConfig();
}
// 并行线程数量(默认最大十个)
if (parallConfig.getMaxThreads() == null) {
parallConfig.maxThreads(10);
}
int thread = parallConfig.getMaxThreads();
if (parallQueryList.size() < thread) {
thread = parallQueryList.size();
}
List<QueryResult<T>> results = new ArrayList<QueryResult<T>>();
ExecutorService pool = null;
try {
pool = Executors.newFixedThreadPool(thread);
List<Future<ParallQueryResult>> futureResult = new ArrayList<Future<ParallQueryResult>>();
SqlToyConfig sqlToyConfig;
Future<ParallQueryResult> future;
for (ParallQuery query : parallQueryList) {
sqlToyConfig = sqlToyContext.getSqlToyConfig(new QueryExecutor(query.getExtend().sql).resultType(query.getExtend().resultType), SqlType.search, getDialect(query.getExtend().dataSource));
// 自定义条件参数
if (query.getExtend().selfCondition) {
future = pool.submit(new ParallQueryExecutor(sqlToyContext, dialectFactory, sqlToyConfig, query, query.getExtend().names, query.getExtend().values, getDataSource(query.getExtend().dataSource, sqlToyConfig)));
} else {
future = pool.submit(new ParallQueryExecutor(sqlToyContext, dialectFactory, sqlToyConfig, query, paramNames, paramValues, getDataSource(query.getExtend().dataSource, sqlToyConfig)));
}
futureResult.add(future);
}
pool.shutdown();
// 设置最大等待时长
if (parallConfig.getMaxWaitSeconds() != null) {
pool.awaitTermination(parallConfig.getMaxWaitSeconds(), TimeUnit.SECONDS);
} else {
pool.awaitTermination(SqlToyConstants.PARALLEL_MAXWAIT_SECONDS, TimeUnit.SECONDS);
}
ParallQueryResult item;
int index = 0;
for (Future<ParallQueryResult> result : futureResult) {
index++;
item = result.get();
// 存在执行异常则整体抛出
if (item != null && !item.isSuccess()) {
throw new DataAccessException("第:{} 个sql执行异常:{}!", index, item.getMessage());
}
results.add(item.getResult());
}
} catch (Exception e) {
e.printStackTrace();
throw new DataAccessException("并行查询执行错误:" + e.getMessage(), e);
} finally {
if (pool != null) {
pool.shutdownNow();
}
}
return results;
}
Aggregations