File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpSessionActivationListener example

 
Veena Pointi
Ranch Hand
Posts: 442
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

On page 260 of HFSJ it is given that

"A container is required to migrate Serializable attributes,but a container is not required to use serialization as the means for migrating the HttpSession object"

That I understood ,but further it says "make sure your attribute class types are serializable .But if they are not serializable ,have your attribute object class implement HttpSessionActivationListener and use the activation/passivation callbacks to work around it"

My question is how can we do the above? Any example? Do they mean if attribute is not serializable,but if it implements HttpSessionActivationListener it automatically becomes seriallized object? Is my understanding correct?I understood that purpose of having HttpSessionActivation listener is to notify attributes about the migration of HttpSession to different VM.But I didn't understand about seriallizing attribute instance variables part.Can anybody explain with example if possible?
 
Patil Niteen
Ranch Hand
Posts: 48
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Veena Pointi wrote:
but further it says "make sure your attribute class types are serializable .But if they are not serializable ,have your attribute object class implement HttpSessionActivationListener and use the activation/passivation callbacks to work around it"


 
Patil Niteen
Ranch Hand
Posts: 48
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patil Niteen wrote:
Veena Pointi wrote:
but further it says "make sure your attribute class types are serializable .But if they are not serializable ,have your attribute object class implement HttpSessionActivationListener and use the activation/passivation callbacks to work around it"




The above statement in the quotes says that:
If the attributes are serializable , the container takes care of the migration of the attributes. If the attributes are not serializable , its upto the user to implement the serialization functionality in the activation/passivation callbacks methods after implementing HttpSessionActivationListener.

Correct me if I am wrong!
 
Patil Niteen
Ranch Hand
Posts: 48
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do they mean if attribute is not serializable,but if it implements HttpSessionActivationListener it automatically becomes seriallized object? Is my understanding correct?

No, the attribute object doesn't become a seriallized object automatically after implementing HttpSessionActivationListener . HttpSessionActivationListener provides two methods void sessionDidActivate(HttpSessionEvent se) and void sessionWillPassivate(HttpSessionEvent se) for the purpose of notification of activation and passivation of the session object during migration.

 
Veena Pointi
Ranch Hand
Posts: 442
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for replieng.I could understand your explanation.

If the attributes are not serializable , its upto the user to implement the serialization functionality in the activation/passivation callbacks methods after implementing HttpSessionActivationListener.


My question is how do we implement serialization functionality in the activation/passivation callbacks ?Let us take following Dog object with name "abc"

Dog dg = new Dog("abc");
session.setAttribute("dog"dg);

Assuming that dg is not seriallized ,how do we make use of activation/passivation callbacks to seriallize it ?
 
Patil Niteen
Ranch Hand
Posts: 48
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

My question is how do we implement serialization functionality in the activation/passivation callbacks ?Let us take following Dog object with name "abc"

Dog dg = new Dog("abc");
session.setAttribute("dog"dg);

Assuming that dg is not seriallized ,how do we make use of activation/passivation callbacks to seriallize it ?



The Dog class can be written as:

public class Dog implements HttpSessionActivationListener {

private String name;

public void sessionWillPassivate(HttpSessionEvent se) {

//write code to get the non-serializable field in a state that can survive a move to a new VM(searilize)
}

public void sessionDidActivate(HttpSessionEvent se) {

//code to restore the state of the field.(deserialize)

}

}
In this way we can monitor the session states using the HttpSessionActivationListener.
Thanks,
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look on here
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic