如何在Spring Webflux中处理“池连接发现错误”?

时间:2019-02-17 22:00:47

标签: spring proxy request spring-webflux

我正在使用Webflux通过代理向Web服务器发出//Grammar and Responses string[] grammarFile = (File.ReadAllLines(@"C:\Users\jenas\Desktop\USB backup\Brain\Commands.txt")); string[] responseFile = (File.ReadAllLines(@"C:\Users\jenas\Desktop\USB backup\Brain\Responses.txt")); //Speech synth SpeechSynthesizer speakSynth = new SpeechSynthesizer(); //Speech Rec Choices grammarList = new Choices(); SpeechRecognitionEngine speechRecognition = new SpeechRecognitionEngine(); SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine(); #region Main talking public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //Intitiaate Grammar grammarList.Add(grammarFile); Grammar grammer = new Grammar(new GrammarBuilder(grammarList)); try { speechRecognition.RequestRecognizerUpdate(); speechRecognition.LoadGrammar(grammer); speechRecognition.SpeechRecognized += rec_SpeechRecognized; speechRecognition.SetInputToDefaultAudioDevice(); speechRecognition.RecognizeAsync(RecognizeMode.Multiple); _recognizer.SetInputToDefaultAudioDevice(); _recognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"C:\Users\jenas\Desktop\USB backup\Brain\Basic Commands.txt"))))); _recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized); _recognizer.RecognizeAsync(RecognizeMode.Multiple); } catch { return; } speakSynth.SelectVoiceByHints(VoiceGender.Female); } public void say(string text) { speakSynth.SpeakAsync(text); } private void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { string result = e.Result.Text; int resp = Array.IndexOf(grammarFile, result); TMRListen.Start(); //toolStripStatusLabel3.Text = e.Result.Text; if (responseFile[resp].IndexOf('+') == 0) { List<string> responses = responseFile[resp].Replace('+', ' ').Split('|').Reverse().ToList(); Random r = new Random(); say(responses[r.Next(responses.Count)]); //speechRecognition.RecognizeAsyncCancel(); } } private void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { Random rnd = new Random(); bool isValid = false; int ranNum = rnd.Next(1, 10); string speech = e.Result.Text; // label2.Text = e.Result.Text; e.Result.Confidence.CompareTo(10); toolStripStatusLabel1.Text = e.Result.Text; switch (speech) { //FIRST CASE STATEMENT case "Test": // TestPhase(); isValid = true; break; case "Test two": // Greeting(); isValid = true; break; //SECOND CASE STATEMENT case "Thanks": if (isValid) { speakSynth.Speak("No problem"); } isValid = false; break; } } public void TestPhase() { speakSynth.SpeakAsyncCancelAll(); // List of files from directory, sorted by *.wav type. string[] filePaths = Directory.GetFiles(@"C:\Users\jenas\Desktop\USB backup\Brain\RadioVocals\Defence", "*.wav",SearchOption.AllDirectories); // Random number from 0 to the amount of files you have Random rnd = new Random(Guid.NewGuid().GetHashCode()); int choices = rnd.Next(filePaths.Length); // Create a new player with a random filepath from the array SoundPlayer player = new SoundPlayer(filePaths[choices]); player.Play(); } public void Greeting() { speakSynth.SpeakAsyncCancelAll(); // List of files from directory, sorted by *.wav type. string[] filePaths = Directory.GetFiles(@"C:\Users\jenas\Desktop\USB backup\Brain\RadioVocals\Emotions\Greeting", "*.wav", SearchOption.AllDirectories); // Random number from 0 to the amount of files you have Random rnd = new Random(Guid.NewGuid().GetHashCode()); int choices = rnd.Next(filePaths.Length); // Create a new player with a random filepath from the array SoundPlayer player = new SoundPlayer(filePaths[choices]); player.Play(); } private void listenToolStripMenuItem_Click(object sender, SpeechRecognizedEventArgs e) { } private void TMRListen_Tick(object sender, EventArgs e) { speakSynth.Speak(" "); TMRListen.Stop(); } private void button1_Click(object sender, EventArgs e) { } private void restartToolStripMenuItem_Click(object sender, EventArgs e) { Application.Restart(); } private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { } private void teachMeToolStripMenuItem1_Click(object sender, EventArgs e) { TeachMe School = new TeachMe(); School.Show(); } 并发请求。根据代理,有时连接会失败。我想捕获这些错误并进行处理(最好通过返回一个空的Flux对象)

Mono

这些消息很长,如果需要调试,以后将使日志不可读。我尝试通过使用2019-02-17 16:48:19.947 ERROR 32265 --- [or-http-epoll-8] r.n.resources.PooledConnectionProvider : [id: 0xf7c8834b, L:/192.168.2.247:45536 - R:101.248.64.72/101.248.64.72:80] Pooled connection observed an error io.netty.handler.proxy.ProxyConnectException: http, none, /101.248.64.72:80 => domain.com:443, io.netty.channel.unix.Errors$NativeIoException: syscall:read(..) failed: Connection reset by peer at io.netty.handler.proxy.ProxyHandler.setConnectFailure(ProxyHandler.java:344) [netty-handler-proxy-4.1.31.Final.jar:4.1.31.Final] at io.netty.handler.proxy.ProxyHandler.exceptionCaught(ProxyHandler.java:246) [netty-handler-proxy-4.1.31.Final.jar:4.1.31.Final] 来使其与请求代码保持一致,但它似乎并不能阻止错误日志。

下面是我的代码:

.onErrorResume(err -> Mono.empty())

预期结果是,当随后调用return proxiedWebClient .get() .uri(uri) .exchange() .flatMap(clientResponse -> clientResponse.toEntity(String.class)) .onErrorResume(error -> Mono.empty()); 时,连接错误被简单地丢弃而不添加到Flux中。

0 个答案:

没有答案