Search in sources :

Example 1 with ParallQuery

use of org.sagacity.sqltoy.model.ParallQuery 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;
}
Also used : ParallQueryExecutor(org.sagacity.sqltoy.dialect.executor.ParallQueryExecutor) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) ParallelConfig(org.sagacity.sqltoy.model.ParallelConfig) ParallQuery(org.sagacity.sqltoy.model.ParallQuery) ArrayList(java.util.ArrayList) ParallQueryResult(org.sagacity.sqltoy.model.ParallQueryResult) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException) QueryResult(org.sagacity.sqltoy.model.QueryResult) ParallQueryResult(org.sagacity.sqltoy.model.ParallQueryResult) ParallQueryExecutor(org.sagacity.sqltoy.dialect.executor.ParallQueryExecutor) QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException)

Aggregations

ArrayList (java.util.ArrayList)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 SqlToyConfig (org.sagacity.sqltoy.config.model.SqlToyConfig)1 ParallQueryExecutor (org.sagacity.sqltoy.dialect.executor.ParallQueryExecutor)1 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)1 ParallQuery (org.sagacity.sqltoy.model.ParallQuery)1 ParallQueryResult (org.sagacity.sqltoy.model.ParallQueryResult)1 ParallelConfig (org.sagacity.sqltoy.model.ParallelConfig)1 QueryExecutor (org.sagacity.sqltoy.model.QueryExecutor)1 QueryResult (org.sagacity.sqltoy.model.QueryResult)1