【发布时间】:2018-04-11 05:49:30
【问题描述】:
在vertx中,如果我们要执行jdbc操作,不会阻塞主循环,我们使用以下代码,
client.getConnection(res -> {
if (res.succeeded()) {
SQLConnection connection = res.result();
connection.query("SELECT * FROM some_table", res2 -> {
if (res2.succeeded()) {
ResultSet rs = res2.result();
// Do something with results
}
});
} else {
// Failed to get connection - deal with it
}
});
在这里,我们添加了将在我们的操作完成时执行的处理程序。
现在我想使用Spring Data API,但是和上面的方法一样吗
现在我按如下方式使用它
@Override
public void start() throws Exception {
final EventBus eventBus = this.vertx.eventBus();
eventBus.<String>consumer(Addresses.BEGIN_MATCH.asString(), handler-> {
this.vertx.executeBlocking(()-> {
final String body = handler.body();
final JsonObject resJO = this.json.asJson(body);
final int matchId = Integer.parseInt(resJO.getString("matchid"));
this.matchService.beginMatch(matchId);//this service call method of crudrepository
log.info("Match [{}] is started",matchId);
}
},
handler->{});
}
这里我使用了执行阻塞,但它使用工作池中的线程,是否可以替代包装阻塞代码?
【问题讨论】:
-
为什么要避免使用线程工作池?