File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Where i can put a System.out.println( Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » JSF
Reply Bookmark "Where i can put a System.out.println("alert") to know when Bean in destroyed?" Watch "Where i can put a System.out.println("alert") to know when Bean in destroyed?" New topic
Author

Where i can put a System.out.println("alert") to know when Bean in destroyed?

massimo tarantelli
Ranch Hand

Joined: Jun 19, 2012
Posts: 35

Hello,
Since I have still a lot to learn, i always use System.out.println("something") in the code to understand its flow...for example i put System.out.println("Bean created") to the bean constructor to better follow the bean lifecycle.
Where i can write System.out.println("Bean created") to know where it's destroyed?
thanks


IDE: NetBeans 7.1 - Java:1.7.0_2 - JSF 2.0/2.1 - Mojarra 2.1.3 - Primefaces 3.2 - GlassFish 3.1.2
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 14460
    
    7

For web applications, we recommend using loggers instead of System.out. For one thing, you can never tell where a webapp server will send the output, including il inferno.

However, there are 2 distinct events that could be considered as "destruction" here.

1. When the bean goes out of scope. Once that happens, the bean still occupies resources, but it can no longer be located, so it effectively no longer exists.

2. When the JVM Garbage Collector scavenges the bean, breaking it down into its low-level components and releasing them.

In practical terms, the one you normally worry about is going out of scope, but the rules are quite clear there, since that's the very definition of the scope.

A) Request-scope beans go out of scope at the instant that the response output stream is posted back to the client.

B) Session-scope beans go out of scope when the session is invalidated - you can create a session listener, but the usual cause of invalidation is logging out

C) View-scope beans are effectively session-scope beans, but they go out of scope when you transfer to a different View.

D) Application-scope beans go out of scope when the application terminates.

The JSP page scope does not apply to JSF.


Customer surveys are for companies who didn't pay proper attention to begin with.
massimo tarantelli
Ranch Hand

Joined: Jun 19, 2012
Posts: 35

Thanks for the answer, I still have a doubt....
A very simple scenario could be this one:

index.xhtml



page2.xhtml



model.java



Well, I Have noted that the bean is first created when I access to the page (during the rendering, ok) but than it is acreated again again after i click on the commundButton, even though in the page02 there isn't any bean reference, and so it should not be a request...why?
Brendan Healey
Ranch Hand

Joined: May 12, 2009
Posts: 218

I can't remember if this works with JSF managed beans (I use CDI beans and it does) but try this:



Edit: just checked and it looks like there may be some issues with view scoped beans but as you are
using request scoped it might work: http://stackoverflow.com/questions/6368840/predestroy-never-called-on-viewscoped
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 14460
    
    7

The request scope bean is created, referenced, and destroyed when the page is initially displayed

When you click the commandButton, a new request is submitted, so a new request scope bean is created and posted with valued from the form (if there are any). Since you're navigating to a different View, these values will be destroyed unread along with the request scope bean.
massimo tarantelli
Ranch Hand

Joined: Jun 19, 2012
Posts: 35

The request scope bean is created, referenced, and destroyed when the page is initially displayed

Does It mean that the bean is destroyed after the page rendering? (always speaking about request beans)
Or it'll be destroyed when a new request occurs?
thanks
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 14460
    
    7

Technically, the bean isn't "destroyed". It just goes out of scope. The garbage collector will handle the actual destruction. But "destroyed" is a close enough approximation for what happens.

The Request-scope bean is created on-demand when a request comes in. It is used to process the request and as the reference model for rendering the response. Once the response has been rendered - which is to say that the outgoing HTML has been created and passed on to the netword, the Request-scope bean is immediately discarded. It does not endure waiting for a new request. And, in fact, in HTTP, there's no guarantee another request would ever come in anyway, so there would be no point in doing so. To get a bean to stick around for another request, you need Session or View scope.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Where i can put a System.out.println("alert") to know when Bean in destroyed?
 
Similar Threads
HttpSessionListener in JSF App
Error in spec?
kill all user sessions
for each loop and final
Spring + JPA + Hibernate and multiple Database connection