Question

Is it possible to fire CDI events from within a interceptor ? (Using Jboss 7.1.1)

For example, if I have an interceptor PerformanceLogInterceptor

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

. . .

Could it fire an event as such:

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);
        }
    }

}

Note: exceptionEvent is null at runtime in the Above.

Was it helpful?

Solution

I moved it into an Async block of the PerformanceMonitor bean referenced above.... and then it works (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");
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top