• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what is the purpose of HttpSessionActivationListener ?

 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic