I have a CDI / Weld application running with Hibernate and JSF 2 inside Jetty.
I need to do some initialization after Jetty server is started. I do it in a #PostConstruct method on an #ApplicationScoped bean, but it is not called until a request is sent to the application. I cannot wait for that.
I tried to find out if there is any way to hook into application lifecycle through Weld, but I could not find anything.
Can anybody help me with this please?
In Java EE 7 / CDI 1.1 / Weld 2.0 you can observe #Initialized(ApplicationScoped.class) ServletContext event. That will cause an instance of your bean to be created eagerly.
What about a servlet context listener? Injection should work fine in those if you need to get to something. It also depends on what kind of initialization you need to do. There may be things you won't be able to get to such as request or conversation scoped beans.
To note for whoever might stumble upon this question. I couldn't make it work, and ended up writing my own singletons to deal with situation.
Related
I need to call a method annotated with #Asynchronous in EJB from a ConversationScoped bean. Inside this method I create instances of some classes using #Inject to inject ConversationScoped beans.
Is it somehow possible to set the context of the asynchronous method to given Conversation?
I hope you can help me.
No, absolutely not. EJBs do per definition not run in web container, but in EJB container. In essence, having any web-related artifact/dependency (including javax.faces.* classes) inside an EJB class is a red alert. You're not supposed to inject/access any class from the client tier (the WAR) in the business tier (the EJB/EAR). Moreover, conversation scoped beans are tied to a HTTP request parameter and this information is nowhere available in an EJB container.
Whatever problem you're trying to solve and for which you incorrectly thought that this all would be the right solution, it has to be solved differently. As an educated guess, I think you just need to let the EJB fire a CDI event or take a callback argument.
See also:
JSF Service Layer
I'm working with JSF 2.0 and and existing framework. We have a listener class that allows us to see when objects are being added to the request/session by implementing HttpSessionAttributeListener and ServletRequestAttributeListener.
Now that we are dealing with #ViewScoped objects I can't figure out a way to get alerted when a ViewScoped object is added. Is there a new listener for this similar to the 2 mentioned above?
The view scope is represented by UIViewRoot#getViewMap(). This map only fires the creation and destroy events, the PostConstructViewMapEvent and PreDestroyViewMapEvent respectively, which can be listened by a ViewMapListener implementation (which is by the way quite verbose to set up as compared to e.g. HttpSessionBindingListener; the JSF system event listener API is not really well thought out as to configuration). This map does not fire any events for add/remove. To be sure, I even looked in source code of Mojarra if it didn't sneakily do that, but, unfortunately, it doesn't.
Your best bet is to fire those add/remove events manually in #PostConstruct and #PreDestroy of your view scoped beans. Noted should be that in JSF 2.0/2.1 the #PreDestroy of a view scoped isn't invoked on a session expire. This was an oversight in the spec and is fixed for JSF 2.2.
My question is related to this one (and probably others):
#PreDestroy never called on #ViewScoped
As stated there, there's no trivial solution to either have view-scoped beans destroyed upon navigation and the same seems to hold true for when the session expires.
What would a non-trivial approach to release (calling the #PreDestroy method) JSF view-scoped beans look like, or more specifically as soon as the session expires?
I'm using Java EE 6 and Mojarra 2.1.x on GlassFish 3.1.2.
Create a #SessionScoped bean to hold the resources (in some collection/array?) and inject it in the #ViewScoped bean and then rely on the #PreDestroy of the session scoped bean.
True, this way the resources live a little longer than you want, but that's the most easy and reliable solution you can get. If you want to keep the #PreDestroy in the view scoped bean, then you'd need to somehow make sure that the enduser always performs navigation by a HTTP POST request on exactly this view scoped bean. You can't reliably guarantee that (the enduser's PC might crash and so on).
I've noticed that when using Ajax heavy JSF 1.2 implementations like Richfaces you're somehow forced to declare more managed beans than you'll want as Session scoped so that state can be maintained across multiple Ajax requests; there are components that will just stop working.
For instance, I developed this application lately in which I had to declare almost all my JSF Backing Beans as Session Scoped in order to have component "x" working. Is there a way out of this, do you consider it a bad practice, or is just the price to pay for having Ajax enabled component in JSF 1.2.
Thanks in advance.
Session scope beans increase memory usage.
Another available scope is View Scope - This allows to keep a state of a bean between requests, while the user is still on the same view.
If you are using JSF2, please consider using #ViewScope above the bean name:
#ViewScope
public class myBean{
..
}
If you use RichFaces and JSF1.2, consider using <a4j:keepAlive /> under <f:view> in the view. for example:
<a4j:keepAlive beanName = "#{myBean}"/>
Read more info here
Another option is to use Seam conversation. Also, I wouldn't say components stop working, they still work.. it's your logic that needs to maintain some sort of state on the server.
I have an existing application written in SEAM that uses SEAM Security (http://docs.jboss.org/seam/2.1.1.GA/reference/en-US/html/security.html). In a stateless EJB, I might find something like this:
#In
Identity identity;
...
if(identity.hasRole("admin"))
throw new AuthException();
As far as I understand, Seam injects the Identity object from the SessionContext of the servlet that invokes the EJB (this happens "behind the scenes", since Seam doesn't really use servlets) and removes it after the call. Is this correct?
Is it now possible to access this EJB from another servlet (in this case, that servlet is the server side of a GWT application)? Do I have to "inject" the correct Identity instance? If I don't do anything, Seam injects an instance, but doesn't correctly correlate the sessions and instances of Identity (so the instances of Identity are shared between sessions and sometimes calls get new instances etc.).
Any help and pointers are very welcome - thanks!
Technology: EJB3, Seam 2.1.2. The servlets are actually the server-side of a GWT app, although I don't think this matters much. I'm using JBoss 5.
Seam injects the Identity object from the SessionContext of the servlet that invokes the EJB and removes it after the call. Is this correct ?
Yes, but do not forget you must enable EJB Seam interceptor See here how to
...
Is it now possible to access ANY EJB from another servlet
Yes, you can use its Global JNDI (Vendor dependent) to retrieve it. See here how you can set up and retrieve your EJB #State less / ful bean. If you have a fully-supported Java EE app server, You can retrieve it through annotations.
Do I have to "inject" the correct Identity instance ?
You do not have to worry about it. Seam EJB interceptor Takes care of it. Go ahead.
UPDATE
but in the EJB, two different instances of Identity are injected. I'm guessing the Session context that Seam is using is not correctly linked to the Session context of the servlet ? Any ideas ?
Well, Identity component itself does not implement equals method which, by default, uses default equals implementation by using equals comparison (==). I do not know whether, for each EJB call, you have always a fresh Identity component (Maybe it explains why you have "Two different instances")
If your Servlet's share The same context, you can enable IdentityFilter as a way to wrap your Identity assigned role by using isUserInRole method. Here goes its funcionality:
A filter that provides integration between Servlet Security and the Seam identity component. This integration is accomplished by wrapping the HttpServletRequest with an HttpServletRequestWrapper implementation that delegates security-related calls to the Seam identity component.
If use use #Identity component, it is enabled by default
So instead of inject your EJB (And its #In-jected #Identity) and use
identity.hasRole("admin");
You can use
request.hasUserInRole("admin");
And maybe you want to see Setting and reading the Conversation ID And Seam and GWT
More
The ContextFilter (not enabled by default) opens access to the Seam container and its context variables to non-JSF servlets, such as Struts, Spring MVC, and Direct Web Remoting (DWR). I do not know how to use this kind of funcionality.
Your question is incredibly hard to follow and I'm not sure I understood everything. Anyway, I'll assume you are using Stateless Session Beans (since you said I could use stateful beans) which, by definition, are stateless. So how can Mary get authenticated as Joe after a call to a stateless session bean? This can't be, it doesn't make any sense.
PS: You should maybe rephrase your question and try to clearly distinguish concepts such as the HTTP Session, Session Beans (stateless, stateful?), SessionContext.