我的代碼
import com.google.common.collect.ImmutableMap; import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; import com.google.gson.Gson; import com.xxx.web.open.service.CacheService; import com.xxx.web.open.util.AudioStreamHelper; import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.tuple.ImmutablePair; import org.java_websocket.enums.ReadyState; import org.java_websocket.client.WebSocketClient; import org.java_websocket.handshake.ServerHandshake; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.DigestUtils; import java.io.File; import java.net.URI; import java.nio.ByteBuffer; import java.util.Map; import java.util.Objects; import java.util.UUID; import java.util.concurrent.locks.ReentrantLock; /*** * 離線語音合成 */ @Slf4j @Component("ttsService") public class OfflineTTSService extends WebSocketClient { public String textToSpeech(String text, String speakerId) throws InterruptedException { /*** * 根據請求的內容計算hash值,防止請求重放,產生不必要的請求,浪費服務器資源(內存、cpu、硬盤空間) * hash值產生算法: * 1、byteArrayOf("tts")+byteArrayOf("待轉換的文字") * 2、byteArrayOf("asr")+音頻流二進制 byte[] * 3、byteArrayOf("merge") + 文件流 byteArray1[] + 文件流byteArray2[] + ... 文件流 byteArrayN[] */ ImmutablePair<Boolean, String> result = findConversion(computeHash(text,speakerId)); Boolean existed = result.getLeft(); if(existed) { String uuid = result.getRight(); log.warn("重復的合成請求:{}",uuid); return uuid; } synchronized (this) { if(getReadyState()==ReadyState.NOT_YET_CONNECTED) { if(isClosed()) { reconnectBlocking(); }else{ connectBlocking(); } }else if(getReadyState() == ReadyState.CLOSED){ reconnectBlocking(); } } }
之前的方法
import com.google.common.collect.ImmutableMap; import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; import com.google.gson.Gson; import com.x.web.open.service.CacheService; import com.x.web.open.util.AudioStreamHelper; import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.tuple.ImmutablePair; import org.java_websocket.enums.ReadyState; import org.java_websocket.client.WebSocketClient; import org.java_websocket.handshake.ServerHandshake; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.DigestUtils; import java.io.File; import java.net.URI; import java.nio.ByteBuffer; import java.util.Map; import java.util.Objects; import java.util.UUID; import java.util.concurrent.locks.ReentrantLock; /*** * 離線語音合成 */ @Slf4j @Component("ttsService") public class OfflineTTSService extends WebSocketClient { public String textToSpeech(String text, String speakerId) throws InterruptedException { /*** * 根據請求的內容計算hash值,防止請求重放,產生不必要的請求,浪費服務器資源(內存、cpu、硬盤空間) * hash值產生算法: * 1、byteArrayOf("tts")+byteArrayOf("待轉換的文字") * 2、byteArrayOf("asr")+音頻流二進制 byte[] * 3、byteArrayOf("merge") + 文件流 byteArray1[] + 文件流byteArray2[] + ... 文件流 byteArrayN[] */ ImmutablePair<Boolean, String> result = findConversion(computeHash(text,speakerId)); Boolean existed = result.getLeft(); if(existed) { String uuid = result.getRight(); log.warn("重復的合成請求:{}",uuid); return uuid; } synchronized (this) { if(!isOpen()) { connectBlocking(); } } }
經過以上修改解決問題
其他地方調用者服務
@Autowired
OfflineTTSService ttsService;
@Configuration public class WebAppConfig extends WebMvcConfigurerAdapter { @Bean public URI serverUri() throws URISyntaxException { return new URI("ws://xxx.yyy.zzz.aaa:8081/test"); } }