spring 任务执行器实例

  @Resource(name = "jobTaskExecutor")
    protected TaskExecutor jobTaskExecutor;

    @Override
    public void execute() throws Exception
    {
        // 初始化执行数据
        String param = XxlJobHelper.getJobParam();
        List<T> datas = this.initExecuteData(param);

        // 执行数据为空则直接返回
        if (CollectionUtils.isEmpty(datas))
        {
            return;
        }

        // 线程同步工具
        CountDownLatch countDownLatch = new CountDownLatch(datas.size());

        for (T data : datas)
        {
            jobTaskExecutor.execute(new Runnable()
            {
                @Override
                public void run()
                {
                    try
                    {
                        // 调用定制执行方法
                        execute(data, param);
                    }
                    catch (Throwable e)
                    {
                        // 获取真正异常信息
                        if (e.getCause() != null)
                        {
                            e = e.getCause();
                        }

                        // 记录异常日志
                        XxlJobHelper.log(e);
                    }
                    finally
                    {
                        // 计数器递减
                        countDownLatch.countDown();
                    }
                }
            });
        }

        // 等待执行完成
        await(countDownLatch);

 

上一篇:Js 之TreeGrid使用


下一篇:Python办公自动化之批量生成文档