【发布时间】: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 很好