java 使用countdownlatch 异步导出excel并merge打包

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;
public class ThreadTest extends AbstractJUnit4SpringContextTests {

    public CountDownLatch main = new CountDownLatch(1);
    public CountDownLatch sub = null;
    public static final int maxRow = 60000;
    public static List<Method> getMethod = new ArrayList<Method>();
    public static List<File> fileList = new ArrayList<File>();

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public class Exporter implements Runnable{
        private final CountDownLatch main;
        private final CountDownLatch sub;
        private final int beginIndex;

        public Exporter(CountDownLatch main,CountDownLatch sub,int beginIndex){
            this.main = main;
            this.sub = sub;
            this.beginIndex = beginIndex;
        }
        @Override
        public void run() {
            try {
                main.await();
                List<ExcelTest.Winning> target = jdbcTemplate.query(
                        "select ID,PrizeId,PrizePackageId,CreateTime from xxxxx where PrizePackageId=1121 AND id > 221711322 LIMIT " + beginIndex + "," + maxRow,
                        new BeanPropertyRowMapper(ExcelTest.Winning.class));
                generateExcel(target,beginIndex);
            }catch (InterruptedException  e){

            }finally {
                sub.countDown();
            }
        }
    }

    @org.junit.Test
    public void test() throws InterruptedException {
        String sql = "select count(*) from xxxxx where PrizePackageId=1121 AND id > 221711322";
        int count = jdbcTemplate.queryForInt(sql);
        int totalPage = count%maxRow == 0 ? (count/maxRow):(count/maxRow)+1;
        sub = new CountDownLatch(totalPage);
        try {
            getReflectInfo(ExcelTest.Winning.class,getMethod);
        } catch (IntrospectionException e) {
            logger.info(e);
        }
        for(int i =1;i<=totalPage;i++){
            int beginIndex = (i - 1) * maxRow;
            new Thread(new Exporter(main,sub,beginIndex)).start();
        }
        main.countDown();
        sub.await();

        //merge
        String zipName = "ZipExport.zip";
        try {
            int len = 0;
            byte[] buffers = new byte[1024];
            File file = new File("D:\\"+zipName);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
            try {
                //zos = new ZipOutputStream(bos);
                for (File f : fileList) {
                    ZipEntry entry = new ZipEntry(f.getName());
                    zos.putNextEntry(entry);
                    InputStream in = new FileInputStream(f);
                    while ((len = in.read(buffers)) != -1) {
                        zos.write(buffers, 0, len);
                    }
                    zos.setEncoding("gbk");//解决中文
                    zos.closeEntry();
                    in.close();
                }

                zos.flush();
                zos.close();
            } catch (Exception localException) {
            }
        }catch(Exception e){}


        System.out.println("Done");
    }


    private static void generateExcel(List target,int beginIndex){
        HSSFWorkbook workbook2003 = new HSSFWorkbook();
        // 创建工作表对象并命名
        HSSFSheet sheet = workbook2003.createSheet("学生信息统计表");
        // 遍历集合对象创建行和单元格
        for (int i = 0; i < target.size(); i++) {
            // 创建行
            HSSFRow row = sheet.createRow(i);
            Object object = target.get(i);
            for (int j=0;j< getMethod.size();j++) {
                try {
                    Object result = getMethod.get(j).invoke(object);
                    if (result instanceof Date) {
                        result = DateUtils.format((Date) result, DateUtils.getSecondFormatter());
                    }
                    HSSFCell cell = row.createCell(j);
                    cell.setCellValue(result.toString());
                } catch (Exception e) {
                }
            }
        }
        // 生成文件
        File file = new File("D:\\excelTest_"+beginIndex+ ".xls");
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            workbook2003.write(fos);
            fileList.add(file);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    private static void getReflectInfo(Class<?> c,List<Method> getMethod) throws IntrospectionException {
        Field[] fields = c.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            if (!fields[i].getName().equalsIgnoreCase("serialVersionUID") && !fields[i].getName().equalsIgnoreCase("$jacocoData")) {
                getMethod.add(new PropertyDescriptor(fields[i].getName(), c).getReadMethod());
            }
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值