全局JSF异常处理程序不能捕获所有错误 [英] Global JSF exception handler doesn't catch all errors

查看:154
本文介绍了全局JSF异常处理程序不能捕获所有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个异常处理程序,如本页所述:

I implemented an exception handler like described on this page:

https://wmarkito.wordpress.com/2012/04/05/adding-global-exception-handling-using -jsf-2-x-exceptionhandler /

可惜的是,我仍然收到一些这个解决方案无法处理的错误。你有什么线索如何捕获所有错误?

Sadly I still receive some errors which are not handled by this solution. Do you have any clue how to catch all errors?

这没有被我的处理程序捕获:

This was not catched by my handler:

Warnung:   A system exception occurred during an invocation on EJB EmailController, method: public void com.x.tool.EmailController.sendAdminNotificationReportRegistration(com.x.JPA.Entity.User) throws java.lang.Exception
Warnung:   javax.ejb.EJBException
    at com.sun.ejb.containers.EJBContainerTransactionManager.processSystemException(EJBContainerTransactionManager.java:748)
    at com.sun.ejb.containers.EJBContainerTransactionManager.completeNewTx(EJBContainerTransactionManager.java:698)
    at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:503)
    at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4566)
    at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2074)
    at com.sun.ejb.containers.EjbAsyncTask.call(EjbAsyncTask.java:114)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at com.x.tool.EmailController.sendAdminNotificationReportRegistration(EmailController.java:189)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4786)
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:656)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:55)
    at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:52)
    at sun.reflect.GeneratedMethodAccessor96.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
    at sun.reflect.GeneratedMethodAccessor472.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4758)
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4746)
    at com.sun.ejb.containers.EjbAsyncTask.call(EjbAsyncTask.java:101)
    ... 4 more


推荐答案

在提供JSF请求时没有抛出此异常。在执行异步 EJB服务调用时抛出它。即运行在与JSF请求服务器不同的线程中!

This exception was not thrown while serving a JSF request. It was thrown while executing an asynchronous EJB service call. I.e. running in a different thread than the one serving the JSF request!

由于它是异步执行的,所以没有办法得到任何异常,最终在响应中返回结果一个JSF请求。所以,它不可能得到它最终在JSF错误页面,而不删除 @Asynchronous 注释使其同步。

As it's executed asynchronously, there's no way to get any exception to end up in the response returning the result of a JSF request. So, it's impossible to get it to end up in a JSF error page without removing the @Asynchronous annotation to make it synchronous.

以任何方式,如果将其显示在错误页面中并不重要,但实际上对日志/邮件/任何事情更感兴趣,那么可以在异步EJB中抛出任何异常服务电话使用 @AroundInvoke 拦截器如下:

In any way, if presenting it into an error page is not that important, but you're actually more interested in logging/mailing/whatever it, then you can get hold of any exception thrown during an (asynchronous) EJB service call using an @AroundInvoke interceptor as below:

public class ExceptionHandlerInterceptor {

    @AroundInvoke
    public Object logExceptions(InvocationContext context) throws Exception {
        try {
            return context.proceed();
        }
        catch (Exception e) {
            // Log/mail/whatever it here.

            throw e; // Don't forget to rethrow!
        }
    }

}

该类在EJB项目中的某个地方,并通过EJB类上的 @Interceptors 注释来注册,

Just put the class somewhere in the EJB project and register it via either @Interceptors annotation on the EJB class,

@Stateless
@Interceptors(ExceptionHandlerInterceptor.class)
public class FooService {}

或EJB项目范围内的 /META-INF/ejb-jar.xml 中的以下条目:

or EJB project-wide via the below entries in /META-INF/ejb-jar.xml:

<interceptors>
    <interceptor>
        <interceptor-class>com.example.ExceptionHandlerInterceptor</interceptor-class>
    </interceptor>
</interceptors>
<assembly-descriptor>
    <interceptor-binding>
        <ejb-name>*</ejb-name>
        <interceptor-class>com.example.ExceptionHandlerInterceptor</interceptor-class>
    </interceptor-binding>
</assembly-descriptor>

这篇关于全局JSF异常处理程序不能捕获所有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆