aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes what is the purpose of HttpSessionActivationListener ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "what is the purpose of HttpSessionActivationListener ?" Watch "what is the purpose of HttpSessionActivationListener ?" New topic
Author

what is the purpose of HttpSessionActivationListener ?

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

please refer hfsj page no . 260. the book says

A Container is required to migrate Serializable attributes (which
assumes that all instance variables within the attribute are either
Serializable or null).
But a Container is not required to use Serialization as the means
for migrating the HttpSession object!
What does this mean to you? Simple: make sure your attribute
class types are Serializable and you never have to worry about it.
But if they’re not Serializable (which could be because one of the
attribute object’s instance variables is not Serializable), have your
attribute object class implement HttpSessionActivationListener
and use the activation/passivation callbacks to work around it.


so what i understand is that container has to migrate all the serializable attributes, but it may or may not use serialization. that is it can use its own mechanism for migrating serializable attributes. is that right ? if yes then suppose that i have an attribute Dog in my session which is NOT serializable. the book says " have your attribute object class implement HttpSessionActivationListener and use the activation/passivation callbacks to work around it".
what i didn't understood is what workaround ? if Dog is not serializable what work around can be done by implementing HttpSessionActivationListener. ? we cant make Dog serializable ? can we ? for what purpose is HttpSessionActivationListener there ? the book says to prepare attrbutes for migration. what preparation ? what other things can i do in callback methods of the said listener ?


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1529
    
  22

Hi Gurpeet Singh,

Check this thread for some discussion about Session migration.

what i didn't understood is what workaround ? if Dog is not serializable what work around can be done by implementing HttpSessionActivationListener. ? we cant make Dog serializable ? can we ? for what purpose is HttpSessionActivationListener there ? the book says to prepare attrbutes for migration. what preparation ? what other things can i do in callback methods of the said listener ?

If you can't serialize some of your objects, because for instance, they represent a connection to a database-resource you can close the connection to the database in the sessionWillPassivate() method and re-create the connection in the sessionDidActivate() method of the HttpSessionActivationListener.

Regards,
Frits
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

suppose i have a attribute class lets say Dog as defined below


now lets create a dog object Dog d = new Dog(c, "tommy'); assume that c is a reference to valid coller object already created. we add this dog object to session as session.setAttribute("dog",d)

now since Dog implements Serializable , the means that container is required to migrate Dog to other JVM. also container may or may not use serialization. my first question is that WHETHER DOG CLASS IS SERIALIZABLE ? i say NO. though dog implements serialziable but one of its instance varible Collar does not. so if we try to serialize dog it will give error. am i correct ?

now since Dog implements Serializable , will container be able to migrate the collar instance variable of Dog object or not ? if not then what can be written in the callback methods so that i am able to migrate Collar instance variable as well ?
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1529
    
  22

my first question is that WHETHER DOG CLASS IS SERIALIZABLE ? i say NO.

Correct your Dog is not serializable, so you can't let it implement the Serializable interface. You, as a developer, have made a mistake.

Of course there are ways to solve this, the obvious solution is to let the Collar implement the Serializable interface. However if that Collar object isn't serializable then there is the option to make the instance variable transient. This way the Dog can be serialized and the Collar object is skipped.

The thing to understand here is that if a class is not Serializable you can't migrate it. ( so you should try to avoid having non-Serializable objects in your Session )
examples of non-Serializable classes are database-connections, streams, threads, runtime...

So what is the purpose of the HttpSessionActivationListener:
- cleaning up (closing) resources that can't be Serialized in the sessionWillPassivate() method, and creating them in the sessionDidActivate() method
- when you have a serializable class but the superclass isn't, then any instance variables you inherit from that superclass will have to be reset (in the sessionDidActivate() method) to the values that were given during the original construction (remember: this is automatically done if java serialization is used for Session migration, but if the container uses its own mechanism, you will have to do that yourself)


Regards,
Frits
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the purpose of HttpSessionActivationListener ?
 
Similar Threads
Doubts on Http session migration & Serialization
A couple questions:
HFJS Chapter-6 Page 260 - Lost in Session Migration
Session Migration
HttpSessionActivationListener example