分批查询

package com.xxxpiao.common.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.commons.lang.ArrayUtils;

public class CollectionUtils {
	
	
	
    /**
     * 分批处理数组的工具类
     * 
     * @param collections
     *            需要处理的数组集合
     * @param callBack
     *            处理数组的回调函数
     * @param size
     *            每批处理的个数
     */
    @SuppressWarnings("unchecked")
    public static <E extends Object> void splitCollectionHandle(List<E> collections,
            ListCollectionCallback<E> callBack, int size) {
        if (collections == null || collections.isEmpty())
            return;
        int collectionSize = collections.size();
        if (collectionSize < size) {
            callBack.call(collections);
            return;
        }
        int forCount = collectionSize / size;
        int mode = collectionSize % size;
        Object[] objectArr = collections.toArray();
        for (int i = 0; i < forCount; i++) {
			Object[] tempArr = ArrayUtils.subarray(objectArr, i * size, (i + 1) * size);
            List<E> objectList = (List<E>) Arrays.asList(tempArr);
            callBack.call(objectList);
        }
        if (mode > 0) {
			Object[] tempArr = ArrayUtils.subarray(objectArr, size * forCount, size * forCount + mode);
            List<E> objectList = (List<E>) Arrays.asList(tempArr);
            callBack.call(objectList);
        }
    }
    /**
     * 分组调用callBack里的业务,异步调用,全部组调用完后同步
     * 
     * @param collections
     *            需要处理的数组集合
     * @param callBack
     *            处理数组的回调函数
     * @param size
     *            每批处理的个数
     */
    @SuppressWarnings("unchecked")
    public static <E extends Object> void splitCollectionAsyncCall(List<E> collections,
            ListCollectionCallback<E> callBack, int size) throws InterruptedException, ExecutionException,Exception{
        if (collections == null || collections.isEmpty())
            return;
        int collectionSize = collections.size();
        if (collectionSize < size) {
            callBack.call(collections);
            return;
        }
        
        class AsyncCall implements Runnable{
        	ListCollectionCallback<E> callBack=null;
        	List<E> objectList = null;
        	
        	public AsyncCall(ListCollectionCallback<E> callBack,List<E> objectList){
        		this.callBack =callBack;
        		this.objectList = objectList;
        	}
        		
        	public void run(){
        		 callBack.call(objectList);
        	}
        }
        
        int forCount = collectionSize / size; 
        int mode = collectionSize % size;
        final ExecutorService exePool = Executors.newFixedThreadPool(forCount+(mode>0?1:0));//每个组一个线程
        final List<Future<?>> dataList = new ArrayList<Future<?>>();
        
        
        Object[] objectArr = collections.toArray();
        for (int i = 0; i < forCount; i++) {
			Object[] tempArr = ArrayUtils.subarray(objectArr, i * size, (i + 1) * size);
            List<E> objectList = (List<E>) Arrays.asList(tempArr);
            
            Future<?> future = (Future<?>) exePool.submit(new AsyncCall(callBack,objectList)) ;
           
            
            dataList.add(future);
        }
        if (mode > 0) {
			Object[] tempArr = ArrayUtils.subarray(objectArr, size * forCount, size * forCount + mode);
            List<E> objectList = (List<E>) Arrays.asList(tempArr);
            Future<?> future = (Future<?>) exePool.submit(new AsyncCall(callBack,objectList)) ;
            dataList.add(future);
        }
        List<Exception> exceptionlist = new ArrayList<Exception>();
        for (Future<?> future : dataList) {//同步
			try {
				future.get();
			} catch (InterruptedException e) {
				exceptionlist.add(e);
			} catch (ExecutionException e) {
				exceptionlist.add(e);
			} catch (Exception e) {
				exceptionlist.add(e);
			}
		}
        if(!exceptionlist.isEmpty()){
        	exePool.shutdown();
        	throw exceptionlist.get(0);
        }
		exePool.shutdown();
        
    }
    public static void main(String[] args){
    	
    	return;
    }
    public static <E extends Object> void splitCollectionHandle(Collection<E> collections,
            CollectionCallback<E> callBack, int size) {
        if (collections == null || collections.isEmpty())
            return;
        int collectionSize = collections.size();
        if (collectionSize < size) {
            callBack.call(collections);
            return;
        }
        int forCount = collectionSize / size;
        int mode = collectionSize % size;
        Object[] objectArr = collections.toArray();
        for (int i = 0; i < forCount; i++) {
			Object[] tempArr = ArrayUtils.subarray(objectArr, i * size, (i + 1) * size);
            List<E> objectList = (List<E>) Arrays.asList(tempArr);
            callBack.call(objectList);
        }
        if (mode > 0) {
			Object[] tempArr = ArrayUtils.subarray(objectArr, size * forCount, size * forCount + mode);
            List<E> objectList = (List<E>) Arrays.asList(tempArr);
            callBack.call(objectList);
        }
    }

    public static Map<String, Object> changeDbStyleKeyToJavaStyleKey(Map<String, Object> sourceMap) {
        if (null == sourceMap) {
            return sourceMap;
        }
        Map<String, Object> targetMap = new HashMap<String, Object>();
        for (Entry<String, Object> entry : sourceMap.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            targetMap.put(StringUtil.changeDbcloumStyleToJavaStyle(key), value);
        }
        return targetMap;
    }
    /**
     * 把LIST中类型为简单对象类型INTERGER,LONG ...里的值转为逗号分隔的串 1,2,3
     * 满足IN查询需要
     * @param list
     * @return NULL或SIZE<1时返回 ''
     * @create_time 2012-9-22 下午04:43:39
     */
    public static String listToString(List<?> list){
        if(list==null || list.size()<1){
            return "";
        }
        StringBuffer s = new StringBuffer(512);
        for(Object o :list){
            s.append(o.toString().trim()+",");
        }
        if(s.length()>0) s.deleteCharAt(s.length()-1);
        return s.toString();
    }
    
    public static String collectionToString(Collection<?> list){
        if(list==null || list.size()<1){
            return "";
        }
        StringBuffer s = new StringBuffer(512);
        for(Object o :list){
            s.append(o.toString().trim()+",");
        }
        if(s.length()>0) s.deleteCharAt(s.length()-1);
        return s.toString();
    }
}



package com.xxxpiao.common.utils;

import java.util.Collection;

public interface CollectionCallback<E extends Object> {
	public void call(Collection<E> collection);
}



@Override
	public Map<String, BetPlan> queryBetPlanMapByPlanNoList(Collection<String> planNoSet) {
		final Map<String, BetPlan> resultMap = new HashMap<String, BetPlan>();
		if (planNoSet == null || planNoSet.isEmpty()) {
			return resultMap;
		}
		CollectionCallback<String> callback = new CollectionCallback<String>() {
			@Override
			public void call(Collection<String> collection) {
				Map<String, Object> paramMap = new HashMap<String, Object>();
				String planNoList = StringUtils.join(collection, ",");
				paramMap.put("planNoList", planNoList);
				List<BetPlan> betPlanList = betDbDao.queryList("BetPlanDao.queryBetPlanByPlanNoSet", paramMap);
				for (BetPlan betPlan : betPlanList) {
					resultMap.put(betPlan.getPlanNo(), betPlan);
				}
			}
		};
		com.xxxpiao.common.utils.CollectionUtils.splitCollectionHandle(planNoSet, callback, 50);
		return resultMap;
	}

猜你喜欢

转载自jin8000608172.iteye.com/blog/2085666
今日推荐