码迷,mamicode.com
首页 > 编程语言 > 详细

多线程异步导出

时间:2016-07-14 13:29:17      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

先新建一个执行类

@Service
public class MultiService {
     private static final Logger logger = LoggerFactory.getLogger(MultiService .class);
     //创建一个线程池
     private ExecutorService executorService = Executors.newFixedThreadPool(10);    

    //导出结果列表的service,不是必须
    @Autowired
    private OemAsnyReportResultService oemAsnyReportResultService;

    // 数据来源的service
    @Autowired
    private CouponChannelStatisticService couponChannelStatisticService;

    public void couponStatisticExport(Map<String, Object> params) throws Exception {
        if (!params.containsKey("path") ) {
            //do something
            return;
        }

        //文件上传的路径
        String path = params.get("path").toString()
        // 异步导出
        executorService.execute(new KQChannelStatisticExport(oemAsnyReportResultService, couponChannelStatisticService, logger, params,path));
    }

}

 下面是要执行导出功能的线程

public class KQChannelStatisticExport implements Runnable {

     public KQChannelStatisticExport() {}

     public KQChannelStatisticExport(OemAsnyReportResultService oemAsnyReportResultService,
                    CouponChannelStatisticService couponChannelStatisticService, Logger logger, Map<String, Object> params,
                    String resourceServerPath) {
        this.oemAsnyReportResultService = oemAsnyReportResultService;
        this.couponChannelStatisticService = couponChannelStatisticService;this.logger = logger;
        this.params = params;
        this.resourceServerPath = resourceServerPath;
    }
 
    @Override
    public void run() {

    //执行导出excel的代码

//如果在这里获取session,有可能会报错【shiro报错There is no session with id [xxx] 】

//导出成功后可以在导出记录表新增一条记录
   oemAsnyReportResultService.save(); } }

 

 

创建一个controller

@Controller
public class KQChannelStatisticController{

    @Autowired
    private MultiService multiService ;

    /**
     * 统计导出
     */
    @RequestMapping("/exportKQChannelStatistic")
    @ResponseBody
    public Map<String, Object> exportKQChannelStatistic(HttpServletRequest request) {

    Map<String, Object> result = new HashMap<String, Object>();
        result.put("success", true);

    Map<String, Object> params = new HashMap<String, Object>();

    // 获取参数
    params = getStatParemMap(request);

    //这里是获取session里面的数据,写在controller里面防止报错,如果写在导出的线程中会报错【shiro报错There is no session with id [xxx] 】
    params = AsynExportExcelUtil.joinSonAccount(params);
    try {
       multiService.kqChannelStatisticExport(params);
    } catch (Exception e) {
       logger.error("统计数据导出异常", e);
       result.put("success", false);
    }
       return result;
    }
}

 

大概流程就是这样。。。。

 

多线程异步导出

标签:

原文地址:http://www.cnblogs.com/chn58/p/5670013.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!