How does CDI remove stateful session bean?

五迷三道 提交于 2019-12-07 03:11:57

问题


The spec says that CDI container removes a SFSB when the scope's context is about to be destroyed. How does it exactly remove the EJB? It does not seem to be calling the method annotated with @Remove.

@Stateful
public class CustomerDAOImpl implements CustomerDAO {
    @PreDestroy
    public void onDestroy() {
        //This is getting called as expected
    }
    @Remove
    public void deleteMyBean() {
        //This is not getting called!
    }
}

So, CDI is technically doing what the spec says. The question is how is it managing to ask the EJB container to remove the instance? Thanks.


回答1:


As covener says this is done using an implementation specific EJB API, that isn't part of the EJB standard API.

As covener says, calling @Remove is NOT the right way to proceed. @Remove methods are called by users code, and tell the EJB container to remove the EJB. If you want a callback when the EJB is removed, use @PreDestroy.




回答2:


I think the CDI container needs a hook into the EJB container to ask it to "do what you'd do if an @Remove method had just completed". Looking at EJB spec, EJB 2.1 had a mechanism for this in the interfaces you had to extend.

For obvious reasons, the container calling an arbitrary @Remove annotated method for the side effect is pretty ill advised.




回答3:


The method with @Remove annotation must be called explicitly by the client, then the container will call the method annotated with @PreDestroy implicitly, if it exists. After that, the bean instance will be ready for garbage collection.

This is the only lifecycle method which client can control, all other methods are controlled by the container.



来源:https://stackoverflow.com/questions/10403531/how-does-cdi-remove-stateful-session-bean

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!