3

CDI newbie question. Simple test scenario: JSF + CDI SessionScoped beans.

I need an elegant way to instantiate a known set of session scoped CDI beans without mentioning them on a JSF page or calling their methods from other beans. As a test case - a simple logging bean, which simply logs start and end time of an http session.

Sure, I could create an empty JSF component, place it inside of a site-wide template and make it trigger dummy methods of the required session beans, but it's kinda ugly from my pov.

Another option I see, is to choose a single session bean (which gets initialized 100% either by EL in JSF or by references from other beans), and use its @PostConstruct method to trigger other session beans - the solution a little less uglier than the previous one.

Looks like I'm missing something here, I'd appreciate any other ideas.

andbi
  • 4,426
  • 5
  • 45
  • 70
  • For your test case, I would use a [`HttpSessionListener`](http://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpSessionListener.html) instead. Could you provide a real use case where you need to do this? – Luiggi Mendoza Nov 13 '13 at 20:23
  • @LuiggiMendoza, thanks, session listener slipped my mind despite the fact that I used to utilize SessionBindingListener in JSF beans. As for real case, I said it's a newbie question, I tried to decouple a lot of session classes with cross-referencies and found that some of them require some activity to be done on session start and destroy - logging and security stuff mostly. – andbi Nov 14 '13 at 04:29

2 Answers2

3

While accepting the Karl's answer and being thankful to Luiggi for his hint, I also post my solution which is based on HttpSessionListener but does not require messing with BeanProvider or BeanManager whatsoever.

@WebListener
public class SessionListener implements HttpSessionListener {

  @Inject
  Event<SessionStartEvent> startEvent;
  @Inject
  Event<SessionEndEvent> endEvent;

  @Override
  public void sessionCreated(HttpSessionEvent se) {
    SessionStartEvent e = new SessionStartEvent();
    startEvent.fire(e);
  }

  @Override
  public void sessionDestroyed(HttpSessionEvent se) {
    SessionEndEvent e = new SessionEndEvent();
    endEvent.fire(e);
  }
}

To my surprise, the code above instantiates all the beans which methods are observing these events:

@Named
@SessionScoped
public class SessionLogger implements Serializable {

  @PostConstruct
  public void init() {
    // is called first
  }

  public void start(@Observes SessionStartEvent event) {
    // is called second
  }
}
andbi
  • 4,426
  • 5
  • 45
  • 70
1

Yes, HttpSessionListener would do it. Simply inject the beans and invoke them.

If you container does not support injection in a HttpSessionListener you could have a look at deltaspike core and BeanProvider

http://deltaspike.apache.org/core.html

Karl Kildén
  • 2,415
  • 22
  • 34