Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Groovy 如何为大型接口创建包装器_Groovy_Proxy Classes - Fatal编程技术网

Groovy 如何为大型接口创建包装器

Groovy 如何为大型接口创建包装器,groovy,proxy-classes,Groovy,Proxy Classes,我想创建一个包装器,用于捕获一个特定的异常,并在一个大型(100多个方法)接口中重试所有方法。我的重试代码可以正常工作,但我不知道如何在不将“n”粘贴到所有方法中的情况下连接接口的实现 我试图使用缺少的方法处理程序,但这意味着我无法让它实现接口。抽象显然是错误的,因为我无法实例化它 我希望有一个比动态创建类作为模板更好的解决方案,但我愿意这样做。您是否尝试过为接口重写invokeMethod YourInterface.metaClass.invokeMethod = {String name,

我想创建一个包装器,用于捕获一个特定的异常,并在一个大型(100多个方法)接口中重试所有方法。我的重试代码可以正常工作,但我不知道如何在不将“n”粘贴到所有方法中的情况下连接接口的实现

我试图使用缺少的方法处理程序,但这意味着我无法让它实现接口。抽象显然是错误的,因为我无法实例化它


我希望有一个比动态创建类作为模板更好的解决方案,但我愿意这样做。

您是否尝试过为接口重写
invokeMethod

YourInterface.metaClass.invokeMethod = {String name, args ->
   def result
   println "Calling method $name"
   try{
      result = metaClass.getMetaMethod(name, args).invoke(delegate, args)
   }catch(YourException | AnyOtherException | Exception e){
       println "Handling exception for method $name"
       result = //Call retry code here
   }
   println "Called method $name"

   result
}

重写
invokeMethod
充当接口中所有方法调用的拦截器。处理每个方法的异常并返回成功结果。

我试图使用@dmahapatro的示例,但我一直得到IllegalArgumentException。我最终意识到这只发生在mixin方法中(该方法显示mixin的签名)。我需要使用doMethodInvoke()来获得适当的类型协同,而不是invoke()

errorProneInstance.metaClass.invokeMethod = { String name, args -> 
    def result

    def method = delegate.metaClass.getMetaMethod(name, args)

    while(true) {
        try {
            result = method.doMethodInvoke(delegate, args)

            break

        } catch (AnnoyingIntermittentButRetryableException e) {
            print "ignoring exception"
        }
    }

    result
}

我一直在使用这个解决方案获取IllegalArgumentException。参考我的自我回答了解工作版本(+1了解正确方向)。