JRuby中VertX Future的onComplete方法歧义消除方案咨询
在JRuby中绑定Vert.x Future的Handler以消除方法匹配警告
在VertX应用中使用JRuby开发时,处理Vertx-Future的完成回调会触发JRuby警告:warning: multiple Java methods found. Choosing onComplete(io.vertx.core.Handler),虽然功能正常,但可以通过显式绑定回调到io.vertx.core.Handler类型消除该警告。
原问题代码
def process(connection) # connection is a io.vertx.sqlclient.SqlConnection tx = connection.begin() tx.onComplete( -> succ { if (succ.failed()) msgFailed(result.cause()) # 注:此处原代码存在笔误,应为succ.cause() else processChangeUserLanguage(connection) end }) end
解决方案:显式转换为Handler类型
通过调用lambda的to_java方法,明确指定转换为io.vertx.core.Handler类型,让JRuby精准匹配对应的重载方法:
def process(connection) # connection is a io.vertx.sqlclient.SqlConnection tx = connection.begin() tx.onComplete( -> succ { if succ.failed() msgFailed(succ.cause()) else processChangeUserLanguage(connection) end }.to_java(io.vertx.core.Handler)) end
原理说明
Vert.x的Future类onComplete方法存在多个重载版本,JRuby自动匹配时无法确定最优候选,因此抛出警告。显式指定lambda类型为io.vertx.core.Handler后,JRuby会直接匹配对应方法,不再触发方法选择警告。
内容的提问来源于stack exchange,提问作者Nik
相关产品推荐
相关产品推荐

