假设我有5个请求,并且我想同时发送所有5个请求。我可以使用CompletableFuture.allOf();但是,allOf()仅在所有期货都成功的情况下返回,并且即使一个失败也将失败。如何从n个成功的期货中提取x,而忽略仍然失败的那个,同时仍然并行发送请求?
我要采取的步骤:
1. Send 5 messages in parallel
2. After 10 second timeout, return all successful messages even if any failed
。
答案 0 :(得分:0)
您可以单独捕获异常,并为这些异常情况返回null:
Function GenerateBlendedReturnSeries(AccountID1 As String, Account1Proportion As Double, _
Optional ByVal AccountID2 As String, Optional ByVal Account2Proportion As Double, _
Optional ByVal AccountID3 As String, Optional ByVal Account3Proportion As Double) As Variant 'Vs. As Double()
' CODE IN BETWEEN
Dim BlendedReturnSeriesArray As Variant
ReDim BlendedReturnSeriesArray(ArraySize, 1)
Debug.Print (ArraySize)
On Error Resume Next
For i = 0 To UBound(BlendedReturnSeriesArray)
BlendedReturnSeriesArray(i, 1) = _
Account1PeriodReturnSeriesArray(i, 1) * Account1Proportion _
+ Account2PeriodReturnSeriesArray(i, 1) * Account2Proportion _
+ Account3PeriodReturnSeriesArray(i, 1) * Account3Proportion
'Debug.Print (BlendedReturnSeriesArray(i, 1))
'Debug.Print (i)
Next i
On Error GoTo 0
Debug.Print (BlendedReturnSeriesArray(300, 1))
GenerateBlendedReturnSeries = BlendedReturnSeriesArray 'BlendedReturnSeriesArray
End Function
有关更多信息,请访问以下页面:https://medium.com/@senanayake.kalpa/fantastic-completablefuture-allof-and-how-to-handle-errors-27e8a97144a0