Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I eventually found a workaround, which allows me to get a reference of the <code>@Stateful</code> bean:</p> <p>I created a <code>@Named @Singleton @Startup</code> bean SessionController which holds a local <code>HashMap&lt;String, UserSession&gt; sessionMap</code> with the references of my <code>@Stateful</code> beans:</p> <pre><code>@Named @Singleton @Startup @ConcurrencyManagement(ConcurrencyManagementType.CONTAINER) public class SessionController { private HashMap&lt;String, UserSession&gt; sessionMap; @PostConstruct void init() { sessionMap = new HashMap&lt;String, UserSession&gt;(); } @PreDestroy void terminate() { for (UserSession us : sessionMap.values()) { us.logoutCleanUp(); //This is annotated as @Remove } sessionMap.clear(); } public void addSession(String sessionId, UserSession us) { sessionMap.put(sessionId, us); System.out.println("New Session added: " + sessionId); } public UserSession getCurrentUserSession() { FacesContext context = FacesContext.getCurrentInstance(); String sessionId = ((HttpSession) context.getExternalContext().getSession(false)).getId(); return sessionMap.get(sessionId); } </code></pre> <p>}</p> <p>I add the references from within each bean's <code>@PostConstruct</code> method:</p> <pre><code>public class UserSession implements Serializable { @Inject SessionController sc; ... @PostConstruct void init() { FacesContext context = FacesContext.getCurrentInstance(); String sessionId = ((HttpSession) context.getExternalContext().getSession(true)).getId(); sc.addSession(sessionId, this); } </code></pre> <p>Notice the <code>.getSession(true)</code> which is required since the Session might not be created yet. Also notice that <code>this</code> is safely passed since the <code>@PostConstruct</code> is not the constructor...</p> <p>After all these, I can get the reference in my EntityListener (and any other place) like this:</p> <pre><code>SessionController sc = (SessionController) new InitialContext().lookup("java:module/SessionController"); UserSession us = sc.getCurrentUserSession(); </code></pre> <p>or like this in CDI beans</p> <pre><code>@Inject SessionController sc; </code></pre> <p>The only drawback I see is that this approach works well only for web applications (where <code>FacesContext context = FacesContext.getCurrentInstance()</code> is meaningful). Some of my beans (and finally my EntityListeners) are also exposed via <code>@javax.jws.WebService</code> as <code>@Stateless</code> beans. In this context (actually: absence of), my Singleton wouldn't work (haven't tested yet) since there is no <em>sessionId</em> of any kind (no session at all to be exact). I will have to use a workaround for this, possibly using SessionContext of the bean or inventing a usable <em>sessionId</em> of some sort. I will post back if I create something usable...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload