如果远程调用传播错误,如何让 Spring 致命退出

发布于 2024-09-16 09:30:37 字数 310 浏览 3 评论 0原文

我有一个通过 Spring 的 RMI 代理 机制公开服务的应用程序。存在一个问题,有时存储 JAR 的文件服务器上的“blip”会导致调用将 NoClassDefFoundError 传播回调用者。

到目前为止,还算公平。问题是,如果发生这种情况,我希望我的应用程序崩溃 - 即如果 Error 将传播回调用者。

请注意,我的应用程序中已经有一个 UncaughtExceptionHandler,但它并未被调用(因为该异常并不是真正未捕获

I have an application which exposes a service via Spring's RMI proxy mechanism. There is a problem whereby sometimes a "blip" on the file-server it has its JARs stored on causes an invocation to propagate a NoClassDefFoundError back to the caller.

So far, so fair enough. The thing is, I would like my app to just crash if this happens - i.e. if an Error is going to be propagated back out to the caller.

Note that I already have an UncaughtExceptionHandler in the app and this is not being invoked (because the exception is not really uncaught)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声情话 2024-09-23 09:30:37

如果您通过RmiServiceExporter(或RemoteExporter的任何其他子类)公开RMI服务,那么您可以注入任意拦截器到调用堆栈,每当调用 RMI 服务时都会调用该调用堆栈。

这些拦截器可以捕获任何抛出的 NoClassDefFoundError,并调用 System.exit()

例如:

public class ExitInterceptor implements MethodInterceptor {

 @Override
 public Object invoke(MethodInvocation methodInvocation) throws Throwable {
  try {
   return methodInvocation.proceed();
  } catch (NoClassDefFoundError noClassDefFoundError) {
   noClassDefFoundError.printStackTrace();
   System.exit(1);
   return null;
  }
 }

}

<bean id="exporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
   <!-- existing properties -->
   <property name="interceptors">
      <bean class="com.x.ExitInterceptor"/>
   <property>
</bean>

If you are exposing the RMI service via RmiServiceExporter (or any other subclass of RemoteExporter), then you can inject arbitrary interceptors into the call stack, which will be invoked whenever the RMI service is invoked.

These interceptors could trap any thrown NoClassDefFoundError, and call System.exit().

For example:

public class ExitInterceptor implements MethodInterceptor {

 @Override
 public Object invoke(MethodInvocation methodInvocation) throws Throwable {
  try {
   return methodInvocation.proceed();
  } catch (NoClassDefFoundError noClassDefFoundError) {
   noClassDefFoundError.printStackTrace();
   System.exit(1);
   return null;
  }
 }

}

and

<bean id="exporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
   <!-- existing properties -->
   <property name="interceptors">
      <bean class="com.x.ExitInterceptor"/>
   <property>
</bean>
幸福丶如此 2024-09-23 09:30:37

您可以做的是扩展[RmiServiceExporter][1]并在invoke方法中处理异常。可以这样完成,

public class AutoFailRmiServiceExporter extends RmiProxyFactoryBean implements ApplicationContextAware {

    private ApplicationContext ac;
    @override
    public Object invoke(RemoteInvocation invocation,
                    Object targetObject)
             throws NoSuchMethodException,
                    IllegalAccessException,
                    InvocationTargetException {
        try {
          super.invoke(methodInvocation, target);
        } catch (NoClassDefFoundError e) {
          if {ac instanceof ConfigurableApplicationContext) {
            (ConfigurableApplicationContext)ac).close();
          else {
            //log error
          }
        }
    }
    //application context setter

}

然后在 spring 上下文中定义 rmi 服务时,您将使用 RmiServiceExporter 的 AutoFail 版本。

编辑:原始答案显示了一种在抛出异常时杀死客户端的方法。这是通过子类化 RmiProxyFactorty 并重写 doInvoke 而不是 RmiServiceExporter 来完成的。

What you can do is to extend [RmiServiceExporter][1] and handle the exception in the invoke method. This can be done as so

public class AutoFailRmiServiceExporter extends RmiProxyFactoryBean implements ApplicationContextAware {

    private ApplicationContext ac;
    @override
    public Object invoke(RemoteInvocation invocation,
                    Object targetObject)
             throws NoSuchMethodException,
                    IllegalAccessException,
                    InvocationTargetException {
        try {
          super.invoke(methodInvocation, target);
        } catch (NoClassDefFoundError e) {
          if {ac instanceof ConfigurableApplicationContext) {
            (ConfigurableApplicationContext)ac).close();
          else {
            //log error
          }
        }
    }
    //application context setter

}

You would then use your AutoFail version of the RmiServiceExporterwhen defining your rmi service in your spring context.

EDIT: The original answer showed a way of killing the client when the exception is thrown. This is done by subclassing RmiProxyFactorty and overriding doInvoke instead of RmiServiceExporter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文