You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by er...@novartis.com on 2007/05/18 15:30:58 UTC
how to execute one-time start-up code?
Hi,
This is a general JSF question; probably not MyFaces-specific. Can anyone
tell me how to get one-time initialization code executed during
application startup? With Spring and Struts, this was really easy to do.
With JSF, it's not obvious to me how to do it.
Thanks for any suggestions,
Eric
_________________________
CONFIDENTIALITY NOTICE
The information contained in this e-mail message is intended only for the
exclusive use of the individual or entity named above and may contain
information that is privileged, confidential or exempt from disclosure
under applicable law. If the reader of this message is not the intended
recipient, or the employee or agent responsible for delivery of the
message to the intended recipient, you are hereby notified that any
dissemination, distribution or copying of this communication is strictly
prohibited. If you have received this communication in error, please
notify the sender immediately by e-mail and delete the material from any
computer. Thank you.
Re: how to execute one-time start-up code?
Posted by er...@novartis.com.
What I'm trying to do is cache database data in a bunch of managed beans
at startup. This data rarely changes, so it doesn't make sense to run the
queries again and again. I've seen other people on the list do it this
way:
<h:commandLink
actionListener="#{myManagedBean.init}"
action="#{foo.bar}">
and although this would work, I have 4 problems with this approach:
1. I have *many* managed beans that need to be init'd, and since I think
you can only have one actionListener per command, I'd need a "container"
or "wrapper" to call each and every init.
2. This init code would have to be sprinkled all over many JSF page (code
bloat; harder to maintain if a change needs to be done)
3. I'd need to write extra logic to prevent these managed beans from being
initialized twice. Although this is easy and small to write, it's
extraneous code. More code = more chance for bugs.
4. Even though I'd have logic in each managed beans to prevent them from
being initialized twice, the init() methods are continually called. This
uses needless CPU cycles.
You might suggest a static block in each and every bean and, while this
would work, it's less than elegant.
Thanks again for any ideas,
Eric
"Cagatay Civici" <ca...@gmail.com>
05/18/2007 10:47 AM
Please respond to
"MyFaces Discussion" <us...@myfaces.apache.org>
To
"MyFaces Discussion" <us...@myfaces.apache.org>
cc
Subject
Re: how to execute one-time start-up code?
Calling FacesContext.getCurrentInstance() returns null from my own
ServletContextListener.contextInitialized(), even though I've written the
web.xml like so
There won't be any facescontext during startup, it's associated with a
particular request so since there's no request during startup it'll be
null.
Eric what are you trying to do in this listener, I'm sure there's another
way to achieve this without touching FacesContext.
Cagatay
_________________________
CONFIDENTIALITY NOTICE
The information contained in this e-mail message is intended only for the
exclusive use of the individual or entity named above and may contain
information that is privileged, confidential or exempt from disclosure
under applicable law. If the reader of this message is not the intended
recipient, or the employee or agent responsible for delivery of the
message to the intended recipient, you are hereby notified that any
dissemination, distribution or copying of this communication is strictly
prohibited. If you have received this communication in error, please
notify the sender immediately by e-mail and delete the material from any
computer. Thank you.
Re: how to execute one-time start-up code?
Posted by Cagatay Civici <ca...@gmail.com>.
>
> Calling FacesContext.getCurrentInstance() returns null from my own
> ServletContextListener.contextInitialized(), even though I've written the
> web.xml like so
There won't be any facescontext during startup, it's associated with a
particular request so since there's no request during startup it'll be null.
Eric what are you trying to do in this listener, I'm sure there's another
way to achieve this without touching FacesContext.
Cagatay
Re: how to execute one-time start-up code?
Posted by er...@novartis.com.
Yes, but how to invoke bean constructor at startup?
"::SammyRulez::" <sa...@gmail.com>
05/18/2007 10:43 AM
Please respond to
"MyFaces Discussion" <us...@myfaces.apache.org>
To
"MyFaces Discussion" <us...@myfaces.apache.org>
cc
Subject
Re: how to execute one-time start-up code?
can't you do it in the bean constructor?
2007/5/18, eric.jung@novartis.com <er...@novartis.com>:
>
> Excellent idea. Thanks for the reply, Cagatay. Since I need to
initialize
> application-scoped managed beans in this code, the listener must execute
> after
> org.apache.myfaces.webapp.StartupServletContextListener. Is
> the best way to do this to subclass
> org.apache.myfaces.webapp.StartupServletContextListener,
> specify my subclass as the sole <listener-class> in web.xml (instead of
> after
> org.apache.myfaces.webapp.StartupServletContextListener),
> call super.contextInitialized() in my contextInitialized(), then execute
my
> code? I'm not aware of a way to specify <listener-class/> order in
web.xml.
>
> Thanks agan,
>
> Eric
> _________________________
>
> CONFIDENTIALITY NOTICE
>
> The information contained in this e-mail message is intended only for
the
> exclusive use of the individual or entity named above and may contain
> information that is privileged, confidential or exempt from disclosure
under
> applicable law. If the reader of this message is not the intended
recipient,
> or the employee or agent responsible for delivery of the message to the
> intended recipient, you are hereby notified that any dissemination,
> distribution or copying of this communication is strictly prohibited. If
you
> have received this communication in error, please notify the sender
> immediately by e-mail and delete the material from any computer. Thank
you.
>
--
::SammyRulez::
http://www.kyub.com/blog/
-----------------------------------------------------------------
La programmazione è per un terzo interpretazione e per due terzi
ispirazione.
E per un terzo mistificazione
_________________________
CONFIDENTIALITY NOTICE
The information contained in this e-mail message is intended only for the
exclusive use of the individual or entity named above and may contain
information that is privileged, confidential or exempt from disclosure
under applicable law. If the reader of this message is not the intended
recipient, or the employee or agent responsible for delivery of the
message to the intended recipient, you are hereby notified that any
dissemination, distribution or copying of this communication is strictly
prohibited. If you have received this communication in error, please
notify the sender immediately by e-mail and delete the material from any
computer. Thank you.
Re: how to execute one-time start-up code?
Posted by "::SammyRulez::" <sa...@gmail.com>.
can't you do it in the bean constructor?
2007/5/18, eric.jung@novartis.com <er...@novartis.com>:
>
> Excellent idea. Thanks for the reply, Cagatay. Since I need to initialize
> application-scoped managed beans in this code, the listener must execute
> after
> org.apache.myfaces.webapp.StartupServletContextListener. Is
> the best way to do this to subclass
> org.apache.myfaces.webapp.StartupServletContextListener,
> specify my subclass as the sole <listener-class> in web.xml (instead of
> after
> org.apache.myfaces.webapp.StartupServletContextListener),
> call super.contextInitialized() in my contextInitialized(), then execute my
> code? I'm not aware of a way to specify <listener-class/> order in web.xml.
>
> Thanks agan,
>
> Eric
> _________________________
>
> CONFIDENTIALITY NOTICE
>
> The information contained in this e-mail message is intended only for the
> exclusive use of the individual or entity named above and may contain
> information that is privileged, confidential or exempt from disclosure under
> applicable law. If the reader of this message is not the intended recipient,
> or the employee or agent responsible for delivery of the message to the
> intended recipient, you are hereby notified that any dissemination,
> distribution or copying of this communication is strictly prohibited. If you
> have received this communication in error, please notify the sender
> immediately by e-mail and delete the material from any computer. Thank you.
>
--
::SammyRulez::
http://www.kyub.com/blog/
-----------------------------------------------------------------
La programmazione è per un terzo interpretazione e per due terzi ispirazione.
E per un terzo mistificazione
Re: how to execute one-time start-up code?
Posted by Cagatay Civici <ca...@gmail.com>.
Hi,
I'm not sure if there's a statement about servlet specification about
listener order but usually containers invoke listeners using the declaration
order during startup and in reverse order when the app shuts down.
I'd go with my own listener rather because the myfaces listener is shipped
in a taglib(although containers like jetty 5.1.2 does not support it) and
subclassing it may cause duplicate invocations.
Regards,
Cagatay
On 5/18/07, eric.jung@novartis.com <er...@novartis.com> wrote:
>
>
> Excellent idea. Thanks for the reply, Cagatay. Since I need to initialize
> application-scoped managed beans in this code, the listener must execute
> after org.apache.myfaces.webapp.StartupServletContextListener. Is the best
> way to do this to subclass
> org.apache.myfaces.webapp.StartupServletContextListener, specify my
> subclass as the sole <listener-class> in web.xml (instead of after
> org.apache.myfaces.webapp.StartupServletContextListener), call
> super.contextInitialized() in my contextInitialized(), then execute my
> code? I'm not aware of a way to specify <listener-class/> order in web.xml
> .
>
> Thanks agan,
> Eric
> _________________________
>
> CONFIDENTIALITY NOTICE
>
> The information contained in this e-mail message is intended only for the
> exclusive use of the individual or entity named above and may contain
> information that is privileged, confidential or exempt from disclosure under
> applicable law. If the reader of this message is not the intended recipient,
> or the employee or agent responsible for delivery of the message to the
> intended recipient, you are hereby notified that any dissemination,
> distribution or copying of this communication is strictly prohibited. If you
> have received this communication in error, please notify the sender
> immediately by e-mail and delete the material from any computer. Thank you.
>
Re: how to execute one-time start-up code?
Posted by er...@novartis.com.
That worked. Thanks, Craig.
"Craig McClanahan" <cr...@apache.org>
Sent by: craigmcc@gmail.com
05/18/2007 12:16 PM
Please respond to
"MyFaces Discussion" <us...@myfaces.apache.org>
To
"MyFaces Discussion" <us...@myfaces.apache.org>
cc
Subject
Re: how to execute one-time start-up code?
On 5/18/07, eric.jung@novartis.com <er...@novartis.com> wrote:
Hi,
Calling FacesContext.getCurrentInstance() returns null from my own
ServletContextListener.contextInitialized(), even though I've written the
web.xml like so:
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
<listener>
<!-- Our context listener must come AFTER MyFaces's
ContextListener -->
<listener-class>com.myco.MyServletContextListener</listener-class>
</listener>
This is using JBoss 4.x. Is there any way to force MyFaces'
ContextListener to execute first?
Ordering will not make any difference, because there never will be a
FacesContext at initialization time ... that only happens when a request
comes in.
However, you can still easilhy initialize application scope attributes,
because your contextInitialized() method gets a n indirect reference to
the ServletContext, so you can just store an attribute there. Once you
start processing requests, these attributes will be pre-existing
application scope beans.
public void contextInitialized(ServletContextEvent event) {
ServletContext context = event.getServletContext();
Foo foo = new Foo(...);
context.setAttribute("foo", foo);
}
Craig
_________________________
CONFIDENTIALITY NOTICE
The information contained in this e-mail message is intended only for the
exclusive use of the individual or entity named above and may contain
information that is privileged, confidential or exempt from disclosure
under applicable law. If the reader of this message is not the intended
recipient, or the employee or agent responsible for delivery of the
message to the intended recipient, you are hereby notified that any
dissemination, distribution or copying of this communication is strictly
prohibited. If you have received this communication in error, please
notify the sender immediately by e-mail and delete the material from any
computer. Thank you.
Re: how to execute one-time start-up code?
Posted by Craig McClanahan <cr...@apache.org>.
On 5/18/07, eric.jung@novartis.com <er...@novartis.com> wrote:
>
>
> Hi,
>
> Calling FacesContext.getCurrentInstance() returns null from my own
> ServletContextListener.contextInitialized(), even though I've written the
> web.xml like so:
>
> <listener>
> <listener-class>
> org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
> </listener>
>
> <listener>
> <!-- Our context listener must come AFTER MyFaces's
> ContextListener -->
> <listener-class>com.myco.MyServletContextListener
> </listener-class>
> </listener>
>
> This is using JBoss 4.x. Is there any way to force MyFaces'
> ContextListener to execute first?
Ordering will not make any difference, because there never will be a
FacesContext at initialization time ... that only happens when a request
comes in.
However, you can still easilhy initialize application scope attributes,
because your contextInitialized() method gets a n indirect reference to the
ServletContext, so you can just store an attribute there. Once you start
processing requests, these attributes will be pre-existing application scope
beans.
public void contextInitialized(ServletContextEvent event) {
ServletContext context = event.getServletContext();
Foo foo = new Foo(...);
context.setAttribute("foo", foo);
}
Craig
Re: how to execute one-time start-up code?
Posted by er...@novartis.com.
Hi,
Calling FacesContext.getCurrentInstance() returns null from my own
ServletContextListener.contextInitialized(), even though I've written the
web.xml like so:
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
<listener>
<!-- Our context listener must come AFTER MyFaces's
ContextListener -->
<listener-class>com.myco.MyServletContextListener</listener-class>
</listener>
This is using JBoss 4.x. Is there any way to force MyFaces'
ContextListener to execute first?
"Cagatay Civici" <ca...@gmail.com>
05/18/2007 09:33 AM
Please respond to
"MyFaces Discussion" <us...@myfaces.apache.org>
To
"MyFaces Discussion" <us...@myfaces.apache.org>
cc
Subject
Re: how to execute one-time start-up code?
How about a servlet context listener?
On 5/18/07, eric.jung@novartis.com < eric.jung@novartis.com> wrote:
Hi,
This is a general JSF question; probably not MyFaces-specific. Can anyone
tell me how to get one-time initialization code executed during
application startup? With Spring and Struts, this was really easy to do.
With JSF, it's not obvious to me how to do it.
Thanks for any suggestions,
Eric
_________________________
CONFIDENTIALITY NOTICE
The information contained in this e-mail message is intended only for the
exclusive use of the individual or entity named above and may contain
information that is privileged, confidential or exempt from disclosure
under applicable law. If the reader of this message is not the intended
recipient, or the employee or agent responsible for delivery of the
message to the intended recipient, you are hereby notified that any
dissemination, distribution or copying of this communication is strictly
prohibited. If you have received this communication in error, please
notify the sender immediately by e-mail and delete the material from any
computer. Thank you.
_________________________
CONFIDENTIALITY NOTICE
The information contained in this e-mail message is intended only for the
exclusive use of the individual or entity named above and may contain
information that is privileged, confidential or exempt from disclosure
under applicable law. If the reader of this message is not the intended
recipient, or the employee or agent responsible for delivery of the
message to the intended recipient, you are hereby notified that any
dissemination, distribution or copying of this communication is strictly
prohibited. If you have received this communication in error, please
notify the sender immediately by e-mail and delete the material from any
computer. Thank you.
Re: how to execute one-time start-up code?
Posted by er...@novartis.com.
Excellent idea. Thanks for the reply, Cagatay. Since I need to initialize
application-scoped managed beans in this code, the listener must execute
after org.apache.myfaces.webapp.StartupServletContextListener. Is the best
way to do this to subclass
org.apache.myfaces.webapp.StartupServletContextListener, specify my
subclass as the sole <listener-class> in web.xml (instead of after
org.apache.myfaces.webapp.StartupServletContextListener), call
super.contextInitialized() in my contextInitialized(), then execute my
code? I'm not aware of a way to specify <listener-class/> order in
web.xml.
Thanks agan,
Eric
_________________________
CONFIDENTIALITY NOTICE
The information contained in this e-mail message is intended only for the
exclusive use of the individual or entity named above and may contain
information that is privileged, confidential or exempt from disclosure
under applicable law. If the reader of this message is not the intended
recipient, or the employee or agent responsible for delivery of the
message to the intended recipient, you are hereby notified that any
dissemination, distribution or copying of this communication is strictly
prohibited. If you have received this communication in error, please
notify the sender immediately by e-mail and delete the material from any
computer. Thank you.
Re: how to execute one-time start-up code?
Posted by Cagatay Civici <ca...@gmail.com>.
How about a servlet context listener?
On 5/18/07, eric.jung@novartis.com <er...@novartis.com> wrote:
>
>
> Hi,
>
> This is a general JSF question; probably not MyFaces-specific. Can anyone
> tell me how to get one-time initialization code executed during application
> startup? With Spring and Struts, this was really easy to do. With JSF, it's
> not obvious to me how to do it.
>
> Thanks for any suggestions,
> Eric
>
> _________________________
>
> CONFIDENTIALITY NOTICE
>
> The information contained in this e-mail message is intended only for the
> exclusive use of the individual or entity named above and may contain
> information that is privileged, confidential or exempt from disclosure under
> applicable law. If the reader of this message is not the intended recipient,
> or the employee or agent responsible for delivery of the message to the
> intended recipient, you are hereby notified that any dissemination,
> distribution or copying of this communication is strictly prohibited. If you
> have received this communication in error, please notify the sender
> immediately by e-mail and delete the material from any computer. Thank you.
>