线程池创建+带返回值的线程使用

  /**
     * Set the ThreadPoolExecutor's core pool size.
     */
    private int corePoolSize = 100;
    /**
     * Set the ThreadPoolExecutor's maximum pool size.
     */
    private int maxPoolSize = 200;
    /**
     * Set the capacity for the ThreadPoolExecutor's BlockingQueue.
     */
    private int queueCapacity = 600;
 
  /**
     * @description 初始化异步线程池
     * @return RestTemplate请求实体类
     */
    @Bean("threadPoolTaskExecutor")
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setThreadNamePrefix("DownExecutor-");
        executor.setKeepAliveSeconds(100);
        // rejection-policy:当pool已经达到max size的时候,如何处理新任务
        // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                new Thread(r).start();
            }
        });
        executor.initialize();
        return executor;
    }




   @Autowired
    ThreadPoolExecutor threadPoolExecutor;

    /**
     * 所有通道异步上传信息
     * @param p 实体类
     */
    @Async("threadPoolTaskExecutor")
    public void allResourceUpload(ProcessorFileUpload p) {
        try {
            String chatbotApp=p.getAppId();
            List<RcsMaapapp> maapapps=rcsMaapappDao.list("RcsMaapapp.getResourceChatBotId",chatbotApp) ;
            if(null==maapapps||maapapps.size()<1){
                //没有任何可以提交的通道 不管
               /* RcsResource rcsResource = new RcsResource();
                rcsResource.setTid(p.getTid());
                rcsResource.setUploadStatus(0L);
                rcsResourceDao.update("RcsResource.updateUploadStatus", rcsResource);*/
               log.info("----没有可提交通道id{}",p.getId());
                return;
            }
            List<RcsResourceCl> rcsResourceCls=new ArrayList<>();
            for(RcsMaapapp m: maapapps){
                RcsResourceCl c1= RcsResourceCl
                        .builder()
                        .chatbotId(m.getChatbot_id())
                        .appId(p.getAppId())
                        .resourceId(p.getId())
                        .fileRemoteUrl(p.getFileRemoteUrl())
                        .thumbnailRemoteUrl(p.getThumbnailRemoteUrl())
                        .uploadStatus((long)p.getUploadStatus())
                        .tid(p.getTid())
                        .fileSize(p.getFileSize())
                        .thumbnailSize(p.getThumbnailSize())
                        .commitCount(0)
                        .build();
                rcsResourceCls.add(c1);
            }

          /*  if(p.getStatus()==5){
                rcsResourceClMapper.updateRcsResourceClBatch(rcsResourceCls);
            }else{*/
                //先删除所有的资源信息
                rcsResourceClMapper.deleteRcsResourceCl(new RcsResourceCl(p.getAppId(),p.getId()));
                rcsResourceClMapper.insertRcsResourceClBatch(rcsResourceCls);
           // }
            //插入redis
           /* JSONObject json=new JSONObject();
            json.put("fileName",p.getFileName());
            json.put("fileSize",p.getFileSize());
            json.put("fileType",p.getFileType());
            json.put("fileUrl",p.getFileLocalUrl());
            json.put("thumbnailSize",p.getThumbnailSize());
            json.put("thumbnailType",p.getThumbnailType());
            json.put("thumbnailUrl",p.getThumbnailLocalUrl());
            json.put("yd","0");
            json.put("lt","0");
            json.put("dx","0");
            String key="RCS:RESOURCE:"+p.getAppId()+":"+p.getTid().substring(0,1);
            stringRedisTemplate.opsForHash().put(key,p.getTid(), JSON.toJSONString(json));*/

            //提交各个通道信息
            List<Future<Boolean>> futureList = new LinkedList<>();
            for(RcsMaapapp m: maapapps){
                ProcessorFileUpload pf=new ProcessorFileUpload();
                BeanUtils.copyProperties(p,pf );
                pf.setChatbotId(m.getChatbot_id());
                Future<Boolean> future = threadPoolExecutor.submit(new UploadResourceThread(pf,config,mediaManageService,mediaManageTelecomService,mediaManageGxMobileService));
                futureList.add(future);
            }
            //判断是不是全部已提交成功
           /* int number=0;
            for (Future<Boolean> future : futureList) {
                try {
                    Boolean result = future.get();
                    if (result) {
                        number++;
                    }
                } catch (Exception e) {
                    log.error("获取线程结果异常");
                }
            }
            if(number==futureList.size()){
                //全部成功 修改rcsResource表的uploadStatus为已提交
                rcsResourceDao.update("RcsResource.updateUploadStatus",new RcsResource(p.getTid(),1L));
            }else{
                rcsResourceDao.update("RcsResource.updateUploadStatus",new RcsResource(p.getTid(),0L));
            }*/
        }catch (Exception e){
            log.error("执行所有通道下发出错 error={}",e);
        }
    }






/**
 * 文件上传的线程
 * @author chenjunqiang
 * @since 2020/9/15 13:38
 */
@Slf4j
public class UploadResourceThread implements Callable<Boolean> {

    private Config config;
    private MediaManageService mediaManageService;
    private MediaManageTelecomService mediaManageTelecomService;
    private MediaManageGxMobileService mediaManageGxMobileService;

    private ProcessorFileUpload pf;

    public UploadResourceThread( ProcessorFileUpload pf,Config config,MediaManageService mediaManageService,MediaManageTelecomService mediaManageTelecomService,MediaManageGxMobileService mediaManageGxMobileService) {
        this.pf = pf;
        this.config=config;
        this.mediaManageService=mediaManageService;
        this.mediaManageTelecomService=mediaManageTelecomService;
        this.mediaManageGxMobileService=mediaManageGxMobileService;
    }

    @Override
    public Boolean call() {
        try {
            int oprator=MediaManageService.appMap.get(pf.getChatbotId()).getOperator();
            if(oprator==0||oprator==3||oprator==5){//移动,北向接入层,电信csp接入层
                if(pf.getThumbnailSize()>config.getThumbnailSize()){
                    return false;
                }
                return mediaManageService.uploadFile2(pf);
            }else if(oprator==1){//电信
                return mediaManageTelecomService.uploadFileTelecom2(pf);
            } else if(oprator==2){//移动北向
                if(pf.getThumbnailSize()>config.getThumbnailSize()){
                    return false;
                }
                return mediaManageService.uploadFileBx2(pf);
            }else if(oprator==4){//广西移动
                if(pf.getThumbnailSize()>config.getThumbnailSize()){
                    return false;
                }
                return mediaManageGxMobileService.uploadFileGxYd2(pf);
            }
        } catch (Exception e) {
            log.error("线程池执行资源上传失败");
        }
        return false;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值