Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interceptor method not called with interceptor binding

I'm using Java EE 6 & Jboss AS7.1 and try to use interceptor binding (Example from jboss site).

I have an InterceptorBinding annotation:

@InterceptorBinding
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface GeoRestrictedEquipment {
}

The interceptor:

@GeoRestrictedEquipment
@Interceptor
public class GeoRestrictedEquipmentInterceptor {

    @EJB EquipmentDao equipmenttDao;    
    @EJB SecurityService securityService;    

    @AroundInvoke
    public Object checker(InvocationContext ctx) throws Exception {
        Integer id = (Integer) ctx.getParameters()[0];
        Equipment equipment = equipmenttDao.findById(id);
        GeoChecker.check(equipment.getSite(), securityService.getUser());

        return ctx.proceed();
    }
}

And a bean:

@Stateless
@LocalBean
@SecurityDomain(Realm.NAME)
@RolesAllowed({ Roles.REGISTERED })
public class PumpService implements PumpServiceLocal {

    @Override
    @GeoRestrictedEquipment
    public PumpInfos getPumpInfos(Integer pumpId) {
        /* ... */
    }
}

But the interceptor is not called... What do I miss from the example ?

The interceptor is called when I write this:

@Override
@Interceptors({GeoRestrictedEquipmentInterceptor.class})
public PumpInfos getPumpInfos(Integer pumpId) {
    /* ... */
}

Thanks for your help.

like image 237
Philippe Gonday Avatar asked Aug 22 '12 15:08

Philippe Gonday


People also ask

What is interceptor method?

Basically, an interceptor is a class whose methods are invoked when business methods on a target class are invoked, life-cycle events such as methods that create/destroy the bean occur, or an EJB timeout method occurs.

How interceptors are declared?

An interceptor is defined using one of the interceptor metadata annotations listed in Table 50-1 within the target class, or in a separate interceptor class. The following code declares an @AroundTimeout interceptor method within a target class. @Stateless public class TimerBean { ...

Which is the preferred way to intercept Java calls?

Defining Interceptors The preferred way to define in Java code is by using meta-data annotations. They can be defined in the application descriptor as well, but, in that case they are not portable across Java EE servers. Some of the meta-data annotations found in the javax.


2 Answers

According to the documentation there is another way rather than using beans.xml:

You do not need to specify the interceptor in the beans.xml file when you use the @Priority annotation.

@Logged
@Interceptor
@Priority(Interceptor.Priority.APPLICATION)
public class LoggedInterceptor implements Serializable { ... }

And it works.

like image 137
andrew.dman Avatar answered Oct 22 '22 08:10

andrew.dman


Did you enable your interceptor as described in the referenced example?

By default, a bean archive has no enabled interceptors bound via interceptor bindings. An interceptor must be explicitly enabled by listing its class under the element of the beans.xml file of the bean archive.

like image 30
zellus Avatar answered Oct 22 '22 08:10

zellus