1.编写拦截器,主要是把进入的类和方法写入日志中
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HpTracingInterceptor {
private static final Logger LOG = LoggerFactory.getLogger(HpTracingInterceptor.class);
/**
* this method is wrapped around a method call and measures the time the
* call needs to run.
*
* @param invocationContext
* invocation context
* @return callback of context.proceed()
* @throws Exception
* if the method call raises an exception
*/
@AroundInvoke
private Object measurePerformance(InvocationContext _invocationContext) throws Exception {
String className = _invocationContext.getTarget().getClass().getSimpleName();
String methodName = _invocationContext.getMethod().getName();
Object _obj = null;
try {
LOG.debug("Invoking method: {} from class: {}", methodName, className);
_obj = _invocationContext.proceed();
} catch (Exception e) {
LOG.error("!!!During invocation of: {} exception occured: {}", methodName, e);
throw e;
}
LOG.debug("Finished method :{}", methodName);
return _obj;
}
}
2.在jboss-ejb3.xml中配置那些类拦截器哪些排除该拦截器.
<?xml version="1.1" encoding="UTF-8"?> <jboss:ejb-jar xmlns:jboss="http://www.jboss.com/xml/ns/javaee" xmlns:p="urn:ejb-pool:1.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-ejb3-2_0.xsd http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd" version="3.1" impl-version="2.0"> <enterprise-beans> </enterprise-beans> <interceptors> <!-- 定义该拦截器 --> <interceptor> <interceptor-class>com.ufis_as.ufisapp.server.interception.HpTracingInterceptor</interceptor-class> </interceptor> </interceptors> <assembly-descriptor> <!-- 配置默认的拦截器--> <interceptor-binding> <ejb-name>*</ejb-name> <interceptor-class>com.ufis_as.ufisapp.server.interception.HpTracingInterceptor</interceptor-class> </interceptor-binding> <!-- 排除默认的拦截器 --> <interceptor-binding>
<ejb-name>DlAbstractBean</ejb-name><exclude-default-interceptors>true</exclude-default-interceptors> </interceptor-binding></assembly-descriptor></jboss:ejb-jar><!-- ejb名字貌似只支持'*'这种通配符,其他的只能用具体的ejb名 -->