Future解决多线程有返回值

package com.cmos.nomsapp.web.controller.audiometry;

import java.io.File;
import java.util.UUID;
import java.util.concurrent.Callable;

import org.apache.commons.lang.StringUtils;

import com.cmos.core.logger.Logger;
import com.cmos.core.logger.LoggerFactory;
import com.cmos.nomsapp.beans.prompt.PromptFileConfig;
import com.cmos.onest.ONestUtil;

public class OnestCallable implements Callable<String>{

    static Logger logger = LoggerFactory.getLogger(OnestCallable.class);
    
    private static String  onext_sys;
    
    private static String  onext_file;
    
    public static void setOnext_sys(String onext_sys) {
        OnestCallable.onext_sys = onext_sys;
    }
    public static void setOnext_file(String onext_file) {
        OnestCallable.onext_file = onext_file;
    }
    public static String getOnext_sys() {
        return onext_sys;
    }
    public static String getOnext_file() {
        return onext_file;
    }

    private String warAddr;
    private String audioFile;

    public OnestCallable(String warAddr) {
        super();
        this.warAddr = warAddr;
    }

    public OnestCallable() {
        super();
    }

    public String getWarAddr() {
        return warAddr;
    }

    public void setWarAddr(String warAddr) {
        this.warAddr = warAddr;
    }

    public String getAudioFile() {
        return audioFile;
    }

    public void setAudioFile(String audioFile) {
        this.audioFile = audioFile;
    }
    @Override
    public String call() throws Exception {
        
        return getAudioNameByPath();
    }

    public  String  getAudioNameByPath(){
        String audioName=null;
        try {
            if( null != warAddr && StringUtils.isNotBlank(warAddr) && !"null".equals(warAddr)){
                String cannonicalPath = PromptFileConfig.voiceFilePath + File.separator;
                audioFile = cannonicalPath + UUID.randomUUID().toString() + ".wav";
                audioName = warAddr.substring(warAddr.lastIndexOf("/")+1,warAddr.indexOf(".wav")+4);
                ONestUtil.download(onext_sys,onext_file+"/"+audioName,audioFile);
                return audioFile;
            }
        } catch (Exception e) {
            logger.error("试听音频下载失败>>>"+audioName+"   "+e);
            audioFile = null;
            return null;
        }
        return null;
    }
}






public String[] getWarPathByCallable(String[] arrText, String[] arrWar) {


        ArrayList<Future<String>> ttsFu = new ArrayList<>();
        ArrayList<Future<String>> onestFu = new ArrayList<>();
        String cannonicalPath = PromptFileConfig.voiceFilePath + File.separator;
        // TTS合成提示音文本
        for (String promptText : arrText) {
            String path = cannonicalPath + UUID.randomUUID().toString() + ".pcm";
            TTSCallable tc = new TTSCallable(promptText, path);
            Future<String> future = ExecutorUtile.executor.submit(tc);
            ttsFu.add(future);
        }
        // Onest下载用户语音音频文件
        for (String warPath : arrWar) {
            OnestCallable oc = new OnestCallable(warPath);
            Future<String> future = ExecutorUtile.executor2.submit(oc);
            onestFu.add(future);
        }
        ArrayList<String> ttsResult = new ArrayList<>();
        // 获取future结果(TTS音频文件合成的地址)
        for (Future<String> futureItem : ttsFu) {
            // ss.get(timeout, unit)
            try {
                ttsResult.add(futureItem.get());
            } catch (Exception e) {
                logger.info("tts的future获取音频文件地址失败", e);
            }
        }
        ArrayList<String> onestResult = new ArrayList<>();
        for (Future<String> futureItem : onestFu) {
            try {
                onestResult.add(futureItem.get());
            } catch (Exception e) {
                logger.info("onest下载音频文件失败", e);
            }
        }

        // 进程结束获取音频文件的地址
        // 存放音频文件的地址
        ArrayList<String> paths = new ArrayList<String>();
        for (int i = 0; i < ttsResult.size(); i++) {
            String ttsPath = ttsResult.get(i);
            if (StringUtils.isNotBlank(ttsPath)) {
                paths.add(ttsPath);
            }
            String onestPath = onestResult.get(i);
            if (StringUtils.isNotBlank(onestPath)) {
                paths.add(onestPath);
            }
        }
        String[] a = new String[paths.size()];
        paths.toArray(a);
        return a;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值