Skip to content

Instantly share code, notes, and snippets.

@Arunshaik2001
Created December 25, 2022 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arunshaik2001/0ee78e2609e1eec80840f1485c46e829 to your computer and use it in GitHub Desktop.
Save Arunshaik2001/0ee78e2609e1eec80840f1485c46e829 to your computer and use it in GitHub Desktop.
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