Kotlin vert.x工作线程中的异常
我对vert.x平台非常陌生。在我的项目中,我有一个通过eventBus进行通信的标准和工人垂直体。worker verticle在循环和数据库访问中执行多个REST API调用 我的问题是,工人垂直完成任务时,在一些运行没有问题,但有时它抛出下面的错误Kotlin vert.x工作线程中的异常,kotlin,vert.x,vertx-verticle,vert.x-webclient,Kotlin,Vert.x,Vertx Verticle,Vert.x Webclient,我对vert.x平台非常陌生。在我的项目中,我有一个通过eventBus进行通信的标准和工人垂直体。worker verticle在循环和数据库访问中执行多个REST API调用 我的问题是,工人垂直完成任务时,在一些运行没有问题,但有时它抛出下面的错误 线程“vert.x-worker-thread-12”io.vertx.core.VertxException中的异常:连接已关闭 我使用kotlin协同程序处理constructDevice(vertx:vertx)函数,该函数执行大多数RE
线程“vert.x-worker-thread-12”io.vertx.core.VertxException中的异常:连接已关闭
我使用kotlin协同程序处理constructDevice(vertx:vertx)
函数,该函数执行大多数RESTAPI调用和数据库访问
有谁能告诉我上述问题的原因吗?还有什么方法可以改进constructDevice(vertx:vertx)
函数以有效地处理多个REST API调用和MongoDB访问
// worker verticle to handle multiple REST API calls and MongoDB database access
class DeviceDiscoverVerticle : CoroutineVerticle() {
override suspend fun start() {
val consumer = vertx.eventBus().localConsumer<String>("listDevice")
consumer.handler { message ->
CoroutineScope(vertx.dispatcher()).launch {
constructDevice(vertx)
}
message.reply("discovered")
}
}
}
// standard verticle to communicate with worker verticle
class ListDeviceVerticle : CoroutineVerticle() {
override suspend fun start() {
val reply = awaitResult<Message<String>> { h ->
vertx.eventBus().request("listDevice", "deviceIPs", h)
}
println("Reply received: ${reply.body()}")
}
}
fun main() {
val vertx = Vertx.vertx()
val workOption = DeploymentOptions().setWorker(true)
vertx.deployVerticle(DeviceDiscoverVerticle(), workOption)
vertx.deployVerticle(ListDeviceVerticle())
}
suspend fun constructDevice(vertx: Vertx) {
val deviceRepository = listOf(
"10.22.0.106",
"10.22.0.120",
"10.22.0.115",
"10.22.0.112"
)
val webClient = WebClient.create(vertx)
val config = json { obj("db_name" to "mnSet", "connection_string" to "mongodb://localhost:27017") }
val mongoClient: MongoClient = MongoClient.create(vertx, config)
val json = Json(JsonConfiguration.Stable.copy(ignoreUnknownKeys = true))
// loop through the IP list and calls REST endpoints
val deviceList = deviceRepository.map { deviceIP ->
val deviceIPconfig: DeviceIPconfig
val deviceType: DeviceType
val requestDeviceIP: HttpRequest<Buffer> = webClient.get(deviceIP, "emsfp/node/v1/self/ipconfig/")
val requestDeviceType: HttpRequest<Buffer> = webClient.get(deviceIP, "emsfp/node/v1/self/information/")
val responseDeviceIP = awaitResult<HttpResponse<Buffer>> { handler ->
requestDeviceIP.send(handler)
}
deviceIPconfig = if (responseDeviceIP.statusCode() == 200) {
json.parse(DeviceIPconfig.serializer(), responseDeviceIP.bodyAsString())
} else {
println("request to device $deviceIP failed with ${responseDeviceIP.statusCode()}")
DeviceIPconfig()
}
val responseDeviceType = awaitResult<HttpResponse<Buffer>> { handler ->
requestDeviceType.send(handler)
}
if (responseDeviceType.statusCode() == 200) {
deviceType = json.parse(DeviceType.serializer(), responseDeviceType.bodyAsString())
val device = DeviceModel(deviceIPconfig, deviceType)
json {
obj(
"_id" to deviceIPconfig.localMac,
"device" to json.stringify(DeviceModel.serializer(), device)
)
}
} else {
println("request to device $deviceIP failed with ${responseDeviceType.statusCode()}")
jsonObjectOf()
}
}.filterNot { it.isEmpty }
// construct data to upload in mongoDB
val activeDeviceIDs = json {
obj("_id" to "activeDeviceIDs",
"activeDeviceIDs" to deviceList.map { it.get<String>("_id") })
}
val activeDevices = json {
obj("_id" to "activeDevices",
"activeDevices" to json { array(deviceList) }
)
}
// save the data in MongoDB
mongoClient.save("devices", activeDeviceIDs) { res ->
if (res.succeeded()) {
println("saved successfully")
} else {
res.cause().printStackTrace()
}
}
mongoClient.save("devices", activeDevices) { res ->
if (res.succeeded()) {
println("saved successfully")
} else {
res.cause().printStackTrace()
}
}
}
因此,我已将CompositeFuture.all(futureList).onComplete
更改为CompositeFuture.join(futureList)。onComplete
根据vert.x文档,join将等待所有未来完成
连接组合将等待,直到所有未来都完成
成功或失败。CompositeFuture.join需要几个步骤
期货参数(最多6个)并返回成功的期货
当所有的期货都成功了,而所有的期货都失败了
已完成,但其中至少有一个失败
但现在很少有期货会失败。这是更改为CompositeFuture.join后的future列表的输出
CompositeFuture.join(futureList).onComplete { asyncResult ->
println(futureList)
if (asyncResult.succeeded()) { res ->
// println(res) this one gets hanged and not printing all response
asyncResult.result().list<HttpResponse<Buffer>>().forEach {
[Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@5e9d3832}, Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@379c326a}]
[Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@51a39962}, Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@edcd528}, Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@293c3e5c}, Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@5f86d3ec}, Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@12a329f7}, Future{cause=Http request failed}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}]
[Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@7abedb1e}, Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@3238d4cb}, Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@5bc868d3}, Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@50af1ecc}, Future{unresolved}, Future{cause=Http request failed}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}]
[Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@5cc549ec}, Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@282f4033}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{cause=Http request failed}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}]
[Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@41a890b3}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{cause=Http request failed}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}]
[Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@147d772a}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{cause=Http request failed}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}]
CompositeFuture.join(futureList).onComplete{asyncResult->
println(未来列表)
如果(asyncResult.succeed()){res->
//println(res)这一个被挂起,而不是打印所有响应
asyncResult.result().list().forEach{
[未来{result=io.vertx.ext.web.client.impl。HttpResponseImpl@5e9d3832},Future{result=io.vertx.ext.web.client.impl。HttpResponseImpl@379c326a}]
[未来{result=io.vertx.ext.web.client.impl。HttpResponseImpl@51a39962},Future{result=io.vertx.ext.web.client.impl。HttpResponseImpl@edcd528},Future{result=io.vertx.ext.web.client.impl。HttpResponseImpl@293c3e5c},Future{result=io.vertx.ext.web.client.impl。HttpResponseImpl@5f86d3ec},未来{result=io.vertx.ext.web.client.impl。HttpResponseImpl@12a329f7},Future{cause=Http请求失败},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved}]
[未来{result=io.vertx.ext.web.client.impl。HttpResponseImpl@7abedb1e},Future{result=io.vertx.ext.web.client.impl。HttpResponseImpl@3238d4cb},Future{result=io.vertx.ext.web.client.impl。HttpResponseImpl@5bc868d3},Future{result=io.vertx.ext.web.client.impl。HttpResponseImpl@50af1ecc},Future{unresolved},Future{cause=Http请求失败},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{{未解决}]
[未来{result=io.vertx.ext.web.client.impl。HttpResponseImpl@5cc549ec},Future{result=io.vertx.ext.web.client.impl。HttpResponseImpl@282f4033},Future{unresolved},Future{unresolved},Future{unresolved},Future{cause=Http请求失败},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved}]
[未来{result=io.vertx.ext.web.client.impl。HttpResponseImpl@41a890b3},Future{unresolved},Future{unresolved},Future{unresolved},Future{cause=Http请求失败},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{{未解决}]
[未来{result=io.vertx.ext.web.client.impl。HttpResponseImpl@147d772a},Future{unresolved},Future{unresolved},Future{unresolved},Future{cause=Http请求失败},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{unresolved},Future{{未解决}]
因为我的设备无法处理并发请求,所以很少有未来会失败?这也是为什么程序执行卡在里面的原因
asyncResult.result().list<HttpResponse<Buffer>>().forEach {
asyncResult.result().list().forEach{
万一是智力问题
CompositeFuture.all(futureList).onComplete { asyncResult ->
if (asyncResult.succeeded()) {
asyncResult.result().list<HttpResponse<Buffer>>().forEach {
[Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@67d2e79}, Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@8bad0c6}, Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@c854509}, Future{unresolved}, Future{unresolved}, Future{cause=Http request failed}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}]
CompositeFuture.join(futureList).onComplete { asyncResult ->
println(futureList)
if (asyncResult.succeeded()) { res ->
// println(res) this one gets hanged and not printing all response
asyncResult.result().list<HttpResponse<Buffer>>().forEach {
[Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@5e9d3832}, Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@379c326a}]
[Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@51a39962}, Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@edcd528}, Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@293c3e5c}, Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@5f86d3ec}, Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@12a329f7}, Future{cause=Http request failed}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}]
[Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@7abedb1e}, Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@3238d4cb}, Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@5bc868d3}, Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@50af1ecc}, Future{unresolved}, Future{cause=Http request failed}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}]
[Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@5cc549ec}, Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@282f4033}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{cause=Http request failed}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}]
[Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@41a890b3}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{cause=Http request failed}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}]
[Future{result=io.vertx.ext.web.client.impl.HttpResponseImpl@147d772a}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{cause=Http request failed}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}, Future{unresolved}]
asyncResult.result().list<HttpResponse<Buffer>>().forEach {
public class HttpVerticle extends AbstractVerticle {
WebClient webClient;
@Override
public void start() throws Exception {
webClient = WebClient.create(vertx);
vertx.eventBus().consumer("run_multiple_requests", event -> {
//When event is received this block is handled by some thread from worker pool, let's call it 'main thread'
Promise<HttpResponse<Buffer>> request1Promise = Promise.promise();
Promise<HttpResponse<Buffer>> request2Promise = Promise.promise();
Promise<HttpResponse<Buffer>> request3Promise = Promise.promise();
//Since webclient is async, all calls will be asynchronous
webClient.get("ip1", "/endpoint")
.send(asyncResult -> {
//async block #1 if it's worker verticle, it's probably picked up by another thread
//here we specify that our promise finished or failed
if (asyncResult.succeeded()) {
request1Promise.complete(asyncResult.result());
} else {
request1Promise.fail("Http request failed");
}
});
//at this point async block #1 is probably still processing
webClient.get("ip2", "/endpoint")
.send(asyncResult -> {
//async block #2 if it's worker verticle, it's probably picked up by another thread
//here we specify that our promise finished or failed
if (asyncResult.succeeded()) {
request2Promise.complete(asyncResult.result());
} else {
request2Promise.fail("Http request failed");
}
});
//at this point async block #1 and #2 are probably still processing
webClient.get("ip3", "/endpoint")
.send(asyncResult -> {
//async block #3 if it's worker verticle, it's probably picked up by another thread
//here we specify that our promise finished or failed
if (asyncResult.succeeded()) {
request3Promise.complete(asyncResult.result());
} else {
request3Promise.fail("Http request failed");
}
});
//retrieving futures from promises
Future<HttpResponse<Buffer>> future1 = request1Promise.future();
Future<HttpResponse<Buffer>> future2 = request2Promise.future();
Future<HttpResponse<Buffer>> future3 = request3Promise.future();
CompositeFuture.all(future1, future2, future3).onComplete(allFuturesResult -> {
//async block #4 this will be executed only when all futures complete, but since it's async it does
// not block our 'main thread'
if (allFuturesResult.succeeded()) {
//all requests succeeded
vertx.eventBus().send("update_mongo", someMessage);
} else {
//some of the requests failed, handle it here
}
});
//at this point async block #1 #2 #3 are probably still processing and #4 is waiting for callback
//but we leave our event handler and free 'main thread' without waiting for anything
});
}
fun constructDeviceDevice(deviceIP: String, device: String, webClient: WebClient): Future<HttpResponse<Buffer>> {
val requestDevices: HttpRequest<Buffer> = webClient.get(deviceIP, "emsfp/node/v1/self/diag/devices/$device")
val deviceDevicePromise: Promise<HttpResponse<Buffer>> = Promise.promise()
requestDevices.send { asyncResult ->
if (asyncResult.succeeded())
deviceDevicePromise.complete(asyncResult.result())
else
deviceDevicePromise.fail("Http request failed");
}
return deviceDevicePromise.future()
}
fun constructDeviceDevices(deviceIP: String, webClient: WebClient): Future<HttpResponse<Buffer>> {
val requestDeviceDevices: HttpRequest<Buffer> = webClient.get(deviceIP, "emsfp/node/v1/self/diag/devices/")
val deviceDevicesPromise: Future<HttpResponse<Buffer>> = Promise.promise()
requestDeviceDevices.send { asyncResult ->
if (asyncResult.succeeded()) {
deviceDevicesPromise.complete(asyncResult.result())
} else
deviceDevicesPromise.fail("Http request failed")
}
return deviceDevicesPromise.future()
}
fun constructDevice(vertx: Vertx, webClient: WebClient, deviceIP: String): Future<SomeDomainObject> {
//Type of below promise depends on what you are mapping responses to. It may also be a list of mapped objects
val constructDevicePromise: Promise<SomeDomainObject> = Promise.promise()
val httpDevicesFuture: Future<HttpResponse<Buffer>> = constructDeviceDevices(deviceIP, webClient)
httpDevicesFuture.onComplete { ar ->
if (ar.succeeded()) {
val futureList: List<Future<HttpResponse<Buffer>>>
//loop through ar.result() and populate deviceDevicesFuture list
CompositeFuture.all(futureList).onComplete { allFuturesResult ->
if (allFuturesResult.succeeded()) {
// here you have access to allFuturesResult.list() method
// at this point you know all futures have finished, you can retrieve result from them (you may need to cast them from Object)
// when you have List<HttpResponse> you map it to whatever you want
val myMappedObject: SomeDomainObject = mappingResult()
constructDevicePromise.complete(myMappedObject)
} else {
constructDevicePromise.fail("failed")
}
}
}
}
return constructDevicePromise.future()
}
class DeviceDiscoverVerticle : AbstractVerticle() {
override fun start() {
val deviceRepository = // list of IP strings
val webClient = WebClient.create(vertx)
vertx.eventBus().localConsumer<String>("listDevice").handler { message ->
deviceRepository.forEach { deviceIP ->
//here dependent on your logic, you handle each future alone or create a list and handle them together
val constructDeviceFuture: Future<SomeDomainObject> = constructDevice(vertx, webClient, deviceIP)
constructDeviceFuture.onComplete(ar -> {
ar.result() // <- this is your mapped object
eventBus.send("SOME_NEXT_LOGIC", serializedDomainObject)
})
}
//if you need to handle all devices at once, once again you need to make CompositeFuture from all responses of constructDevice
}
}
}
vertx.eventBus().localConsumer<String>("listDevice").handler { message ->
deviceRepository.forEach { deviceIP ->
val constructDeviceFuture = constructDevice(webClient, deviceIP)
constructDeviceFuture.onComplete {ar ->
//println(ar.result().toString())
}
}
}