-
-
Save Arunshaik2001/0ee78e2609e1eec80840f1485c46e829 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun sendMessageWithRequests( | |
channel: ManagedChannel? | |
):Any { | |
return try { | |
val stub = GreeterGrpc.newStub(channel) | |
var failed: Throwable? = null | |
val finishLatch = CountDownLatch(1) | |
val responseList = mutableListOf<HelloResponse>() | |
val requestObserver = stub.lotsOfRequests(object : StreamObserver<HelloResponse> { | |
override fun onNext(response: HelloResponse) { | |
responseList.add(response) | |
} | |
override fun onError(t: Throwable) { | |
failed = t | |
finishLatch.countDown() | |
} | |
override fun onCompleted() { | |
finishLatch.countDown() | |
} | |
}) | |
try { | |
val requests = arrayOf( | |
newHelloResponse("TOM"), | |
newHelloResponse("ANDY"), | |
newHelloResponse("MANDY"), | |
newHelloResponse("John") | |
) | |
for (request in requests) { | |
requestObserver.onNext(request) | |
} | |
} catch (e: java.lang.RuntimeException) { | |
requestObserver.onError(e) | |
return e.message?:"" | |
} | |
requestObserver.onCompleted() | |
if (!finishLatch.await(1, TimeUnit.MINUTES)) { | |
return "Timeout error" | |
} | |
if (failed != null) { | |
return failed?.message?:"" | |
} | |
return responseList.map { it.message } | |
} catch (e: Exception) { | |
e | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment