- public staticPagedList> queryAsyn(ITemplateService service, Identity tenantId, Identity userId, String entityName,
- Map params, String columns, TCondition cond) {
- intpage = webHelper.getPageNo(params);
- intpageSize = WebHelper.getPageSize(params);
- String orderColumn = (String)params.get(JqgridConstant.ORDER_COLUMN);// 排序字段String orderSord = (String)params.get(JqgridConstant.ORDER_METHOD);// 排序方式,desc or ascExecutorService slaver = Executors.newSingleThreadExecutor();
- FutureTask totalCountFuture = newFutureTask<>(new TotalCountJob(service, tenantId, entityName, cond));
- slaver.execute(totalCountFuture);
- handleOrderByColumn(cond, orderColumn, orderSord);
- slaver.shutdown();
- //主线程来取数据
- longtime1 = System.nanoTime();
- List<Map<String, Object>> list = service.query(columns, entityName, cond, tenantId, userId, pageSize, page);
- longtime2 = System.nanoTime();
- longdiff = time2 - time1;
- logger.debug("查询方案统计-----查询分页list部分,用时:{}s,条件:{}", translateToSecond(diff), cond);
- Integer totalCount =null;
- intlistSize = list.size();
- if(listSize < pageSize) {
- logger.info("本次查询不需要sql进行count操作");
- totalCount = listSize + (page -1) * pageSize;
- slaver.shutdownNow();
- } else {
- try {
- //没做完就等着totalCount = totalCountFuture.get();
- } catch (Exception e) {
- totalCountFuture.cancel(true);
- logger.error("totalCount发生异常", e);
- }
- }
- Translator.prepare(list, tenantId, service);
- return newPagedList<>(page, pageSize, totalCount, list);
- }
- private static doubletranslateToSecond(long diff) {
- returndiff *0.000000001;
- }
- static classTotalCountJob implements Callable {
- private String tableName;
- private TCondition condition;
- private Identity tenantId;
- private ITemplateService service;
- public TotalCountJob(ITemplateService service, Identity tenantId, String tableName, TCondition condition) {
- this.service = service;
- this.tableName = tableName;
- this.condition = condition;
- this.tenantId = tenantId;
- }
- @Override
- public Integer call() throws Exception {
- longtime1 = System.nanoTime();
- Integer totalCount = service.getBaseService().query4Count(tenantId, tableName, condition);
- longtime2 = System.nanoTime();
- longdiff = time2 - time1;
- logger.debug("查询方案统计-----查询分页count部分,用时:{}s,条件:{}", translateToSecond(diff), condition);
- return totalCount;
- }
- }
来源: http://www.cnblogs.com/victor2302/p/6414917.html