【问题标题】:Firing CDI event from interceptor class从拦截器类触发 CDI 事件
【发布时间】:2014-04-19 21:29:51
【问题描述】:

是否可以从拦截器中触发 CDI 事件? (使用 Jboss 7.1.1)

例如,如果我有一个拦截器 PerformanceLogInterceptor

@Interceptors({PerformanceLogInterceptor.class})
public class ProcessHandler extends HandlerBase {

。 . .

它会触发这样的事件吗:

public class PerformanceLogInterceptor {

    private Logger LOG = LoggerFactory.getLogger("PerformanceLog");

    @EJB
    PerformanceMonitor performanceMonitor;

    @Inject
    Event<ExceptionEvent> exceptionEvent;


    @AroundInvoke
    @AroundTimeout
    public Object performanceLog( InvocationContext invocationContext ) throws Exception {
        String methodName = invocationContext.getMethod().toString();
        long start = System.currentTimeMillis();
        try {
            return invocationContext.proceed();
        } catch( Exception e ) {
            LOG.warn( "During invocation of: {} exception occured: {}", methodName, Throwables.getRootCause(e).getMessage() );
            performanceMonitor.addException( methodName, e );

            Exception toSend;
            if(e instanceof EfsobExceptionInformation ){
                toSend = e;
            } else {
                LOG.debug("Wrapping exception");
                EfsobExceptionWrapper wrapped = new EfsobExceptionWrapper(e);
                toSend = wrapped;
            }

            if(exceptionEvent != null) {
                LOG.debug("sending exceptionEvent");
                exceptionEvent.fire(new ExceptionEventBuilder()
                                .setExceptionName(toSend)
                                .setEfsobExceptionType(toSend)
                                .setId(toSend)
                                .setStacktrace(toSend)
                                .build()
                );
            } else {
                LOG.debug("exceptionEvent was null");
            }

            LOG.debug("rethrowing");
            throw toSend;
        } finally {
            long total = System.currentTimeMillis() - start;
            performanceMonitor.addPerformanceMetrics(methodName, total);
        }
    }

}

注意:上面的 exceptionEvent 在运行时为空。

【问题讨论】:

  • 您使用的是什么容器?拦截器应该启用注入功能,这让我认为这会起作用。
  • performanceMonitor 在这里也为空吗?
  • performanceMonitor 很好

标签: ejb ejb-3.0 cdi


【解决方案1】:

我将它移到上面引用的 PerformanceMonitor bean 的 Async 块中......然后它就可以工作了(WAT?)

@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
public class PerformanceMonitor {

    @Inject
    Event<ExceptionEvent> exceptionEvent;

    private Logger LOG = LoggerFactory.getLogger("PerformanceMonitor");

    @Asynchronous
    public void addException(String methodName, Exception e) {

        if(exceptionEvent != null) {
            LOG.debug("sending exceptionEvent");
            exceptionEvent.fire(new ExceptionEventBuilder()
                            .setExceptionName(e)
                            .setEfsobExceptionType(e)
                            .setId(e)
                            .setStacktrace(e)
                            .build()
            );
        } else {
            LOG.debug("exceptionEvent was null");
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 2014-03-10
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    相关资源
    最近更新 更多