Question

Can a CDI Decorator be a stateless ejb?

I tried:

@Decorator
@Stateless
public class WarehouseHandlingDecorator implements SerialKitServiceWarehouseHandling {


@Inject
@Any
@Delegate
protected SerialKitServiceWarehouseHandling serialKitServiceWarehouseHandling;

...

}

I deploy on JBoss 6.1 and i get the following message:

WELD-000038 Cannot place @Delegate at an injection point which is not on a Decorator: @New Session bean [class com.eggsweb.production.services.WarehouseHandlingDecorator with qualifiers [@New]; local interfaces are [SerialKitServiceWarehouseHandling]

Was it helpful?

Solution

Decorators and Interceptors cannot be EJBs. You can put Decorators and Interceptors on an EJB, but an EJB cannot be a Decorator or Interceptor.

You can have EJBs injected into a Decorator or Interceptor, so that could open up some options. Maybe inject a @Stateless bean into the @Decorator and have it delegate the work you were imagining for the EJB.

In fact, you could pass the EJB a reference to the @Delegate in the Decorator's @PostConstruct and then delegate all the calls to the EJB rather than the original delegate.

OTHER TIPS

my problem is to wrap in a single transaction the call to the delegate ejb and the call to another ejb, suppose that the above method is the decorator method:

protected void method(Object param1, Object param2){
 //decorated method
 delegate.method(param1,param2);
 //another ejb call
 anotherEJB.doSomething(param1);    
}

if I inject UserTransaction, assuming to be in Java EE container, is the above snippet correct?

protected void method(Object param1, Object param2){

try{
 userTransaction.begin();

 delegate.method(param1,param2);

 anotherEJB.doSomething(param1);

 userTransaction.commit();

 }catch(){
   try{
    userTransaction.rollback();
   }catch(Exception e){}
 }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top