批量处理工具BatchUtil

原创
2019/03/31 00:54
阅读数 728

主要为了处理大量查询数据造成的慢sql

package **;

import java.util.List;

/**
 * 批量处理工具
 */
public class BatchUtil{
    public final static int BATCH_GET_ID_SIZE = 500;
    public final static int BATCH_SAVE_SIZE = 1000;
    public final static int BATCH_QUERY_SIZE = 5000;

    public interface OperateCallback<T>{
        void operate(List<T> list) throws Exception;
    }

    public interface OperateCallbackWithProgress<T>{
        /**
         * 操作方法
         *
         * @param list     要处理的list
         * @param progress 进度
         * @throws Exception
         */
        void operate(List<T> list, int progress) throws Exception;
    }

    public interface QueryCallBack{
        void callBack(Integer start, Integer size) throws Exception;
    }

    public static void batchQuery(int count, QueryCallBack queryCallBack) throws Exception{
        for(int i = 0; (count > 0 && i <= count / BATCH_QUERY_SIZE && count % BATCH_QUERY_SIZE != 0)
                || (count > 0 && i < count / BATCH_QUERY_SIZE && count % BATCH_QUERY_SIZE == 0); i++){
            queryCallBack.callBack(i * BATCH_QUERY_SIZE, BATCH_QUERY_SIZE);
        }
    }

    public static void batchQuery(int count, int batchSize, QueryCallBack queryCallBack) throws Exception{
        for(int i = 0; (count > 0 && i <= count / batchSize && count % batchSize != 0)
                || (count > 0 && i < count / batchSize && count % batchSize == 0); i++){
            queryCallBack.callBack(i * batchSize, batchSize);
        }
    }

    public static <T> void batchOperate(List<T> list, int batchSize, OperateCallback<T> callBack) throws Exception{
        if(null == list || list.isEmpty()){
            return;
        }
        int cardCount = list.size();
        if(cardCount > batchSize){
            for(int i = 0;
                (i <= cardCount / batchSize && cardCount % batchSize != 0) || (i < cardCount / batchSize && cardCount % batchSize == 0);
                i++){
                List<T> longs;
                boolean last = (cardCount % batchSize == 0 && cardCount / batchSize == (i + 1))
                        || (cardCount % batchSize != 0 && cardCount / batchSize == i);
                // 最后一次循环
                if(last){
                    longs = list.subList(i * batchSize, cardCount);
                }
                else{
                    longs = list.subList(i * batchSize, (i + 1) * batchSize);
                }
                callBack.operate(longs);
            }
            return;
        }
        callBack.operate(list);
    }

    public static <T> void batchOperate(List<T> list, int batchSize, OperateCallbackWithProgress<T> callBack) throws Exception{
        if(null == list || list.isEmpty()){
            return;
        }
        int cardCount = list.size();
        if(cardCount > batchSize){
            for(int i = 0; (i <= cardCount / batchSize && cardCount % batchSize != 0) || (i < cardCount / batchSize && cardCount % batchSize == 0); i++){
                List<T> longs;
                boolean last = (cardCount % batchSize == 0 && cardCount / batchSize == (i + 1))
                        || (cardCount % batchSize != 0 && cardCount / batchSize == i);
                // 最后一次循环
                if(last){
                    longs = list.subList(i * batchSize, cardCount);
                    callBack.operate(longs, 100);
                }
                else{
                    longs = list.subList(i * batchSize, (i + 1) * batchSize);
                    callBack.operate(longs, (i + 1) * batchSize * 100 / cardCount);
                }
            }
            return;
        }
        callBack.operate(list, 100);
    }
}

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部