全局JSF异常处理程序不会捕获所有错误

时间:2015-04-20 15:30:48

标签: jsf exception ejb handler

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

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

遗憾的是,我仍然收到一些此解决方案无法处理的错误。你有任何线索如何捕捉所有错误吗?

我的处理程序没有抓住这个:

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

1 个答案:

答案 0 :(得分:3)

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

由于它是异步执行的,所以无法获得任何异常,最终会在返回JSF请求结果的响应中结束。因此,如果不删除@Asynchronous注释以使其同步,则无法让它最终出现在JSF错误页面中。

无论如何,如果将它呈现在错误页面中并不重要,但是您实际上对日志/邮件/其他任何内容更感兴趣,那么您可以获得(异步)EJB服务期间抛出的任何异常使用@AroundInvoke拦截器调用如下:

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注释注册它,

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

或项目范围内的EJB通过/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>