2
votes

I have some @SessionScoped CDI beans. Previously all of them were JSF managed beans (changed from JSF managed beans to CDI managed beans).

I was doing like the following to remove some session scoped JSF managed after a user successfully places an online order.

Map<String, Object> sessionMap = context.getSessionMap();

sessionMap.remove("cartBean");
sessionMap.remove("orderItems");
sessionMap.remove("reviewOrderBean");
sessionMap.remove("intermediateLocaleBean");
sessionMap.remove("localeBean");
sessionMap.remove("currencyRateBean");
sessionMap.remove("currency");

So that everything is set to default after order processing completes. This is irrelevant to destroying/invalidating the entire HTTP session where the user must not be logged out after order processing terminates successfully.


Unlike JSF managed beans, CDI beans are stored into server's memory by some CDI manager implementation like Weld. Therefore, they are not available in a session map - Map<String, Object>.

How can this be simulated anyway regarding CDI beans?


UPDATE :

The Weld documentation :

Keep in mind that once a bean is bound to a context, it remains in that context until the context is destroyed. There is no way to manually remove a bean from a context. If you don't want the bean to sit in the session indefinitely, consider using another scope with a shorted lifespan, such as the request or conversation scope.

So I assume, it is hard to manually destroy a bean without destroying something else.

1

1 Answers

4
votes

CDI 1.1 introduced an AlterableContext interface with a destroy(Bean<T>) method.

Get the session context via beanManager.getContext(SessionScoped.class), downcast to AlterableContext and then invoke destroy() with the appropriate bean type.