Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CDI - using interceptor class from library

Can we use annotation-based interceptor in ejb-jar from a different ejb-jar? I tried it with the @Logged example, but stucked with it. Can somebody help me out?

In core.jar:

@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface Logged {}

and

@Logged
@Interceptor
public class LoggedInterceptor implements Serializable {

    private static final long serialVersionUID = 1L;

    public LoggedInterceptor() {
    }

    @AroundInvoke
    public Object logMethodEntry(InvocationContext invocationContext)
            throws Exception {
        System.out.println("Entering method: "
            + invocationContext.getMethod().getName() + " in class "
            + invocationContext.getMethod().getDeclaringClass().getName());

        return invocationContext.proceed();
    }
}

The question is: how to use this interceptor from another ejb-jar (inside an Enterprise Application)? For example: logging business method calls, where methods can be found in different modules:

module1.jar:

public class ModuleClass{
    @Logged public void doSomething(){...}
}

I've tried to put <interceptor><class..... to beans.xml too, but its not working for me.

Thanks for any advice!

like image 851
user1451106 Avatar asked Mar 08 '26 01:03

user1451106


1 Answers

That should definitely work, even though I recall that I fiddled quite a bit on JBoss 6.

You will have to activate the interceptor in the beans.xml of the JAR where it's defined, and I think there was an issue with EAR deployments, but that's quite a while ago and I don't have access to the source code any more.

Should that not work - play with the activation in the beans.xml in both JARs. Try to query the BeanManager if the interceptor is registered.

like image 191
Jan Groth Avatar answered Mar 10 '26 14:03

Jan Groth