This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes agents as servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "agents as servlets" Watch "agents as servlets" New topic
Author

agents as servlets

Rafael Prado
Ranch Hand

Joined: May 17, 2003
Posts: 33
Hi,
First post, though I've being visiting for quite some time and it's proved to be great help.
I'm implementing agents using servlets. Here's the situation:
One agent, agComunicator, is called by the login form.
In its doGet() method sets a session value "user" with the value provided in the forum.
Now, we have 3 more agents: agLinksMgr, agCBItemsMgr and agShortcutMng. Each dealing with a component of the page.
When the user clicks on a link, it calls agLinksMgr passing the link url.
In its doGet() method it checks the user variable, as well as the url parameter, in order to update the database (adapt method).
Those 3 last agents need to know if the user logged off. If so, they update the database with other things.
There's 2 cases of logoff:
If the user presses the LEAVE link.
agComunicator destroys the "user" variable and notifies the other agents (getRequestDispatcher('uri-of-servlet-to-be-called').forward ?). They see that there's no "user" and their job is to run the adapt method (uses the db).
If the session timed out
Is there a way to make it looks like the agComunicator that set the "user" to null (since there isn't one) so it behaves like the case above?

I'm thinking of agComunicator notifying the agents both in the doGet() method (case 1) or the destroy() method (case 2). But I guess that only works if agComunicator is the first to be destroyed, doesn't it?
By the way, it's important to say that the adapt() method of the agents is their last job in their lives, so I guess it would go inside the destroy() method.
Thanks for your attention.
Rafael.
[ May 17, 2003: Message edited by: Rafael Prado ]
Paul Lester
Ranch Hand

Joined: Dec 27, 2002
Posts: 40
There are a couple of ways to know if a session has timed out, assuming that you are using a container that is Servlet 2.3 compliant:
1. Add an HttpSessionListener. You will get notified when a session is created or when it is destroyed. This way you take some type of action when the session is destroyed.
2. Add an HttpSessionBindingListener, which was in servlet spec 2.2. This will indicate when an attribute has been bound or unbound. This is not as accurate as choice #1 because a value can be unbound manually or automatically, in the case of a session expiring.
You mention that you can your adapt method inside the destroy() method of a servlet. This won't work as the servlet is not destroyed when the user's session times out. The destroy method of the servlet is only called when the container is about to unload the servlet from memory, as in a shutdown.
Using the methods above, you could make a call to your database or DAO or whatever to do any cleanup that you'll need to do whenever a user's session is expired.
Paul


Where Photography meets vision.<br /><a href="http://www.photogravision.com" target="_blank" rel="nofollow">http://www.photogravision.com</a><br />Please stop by!<br /> <br />SCJP,SCWCD,SCJD,SCEA
Rafael Prado
Ranch Hand

Joined: May 17, 2003
Posts: 33
Originally posted by Paul Lester:
There are a couple of ways to know if a session has timed out, assuming that you are using a container that is Servlet 2.3 compliant:
1. Add an HttpSessionListener. You will get notified when a session is created or when it is destroyed. This way you take some type of action when the session is destroyed.
2. Add an HttpSessionBindingListener, which was in servlet spec 2.2. This will indicate when an attribute has been bound or unbound. This is not as accurate as choice #1 because a value can be unbound manually or automatically, in the case of a session expiring.

EDIT: I'll look deeper into them, like how it's used inside the servlet (I only saw the listener class written separatedly, not with a servlet calling or something).
End of EDIT
So I have this:
If the user logged off successfully, agComunicator puts "action" with "adapt" value and notifies the other agents, who in the doGet/doPost method will call the adapt method().
Now, if session expires, there should be a listener in the agents that will find that the session is over, so it can run the adapt method.
Question: How would they know the "user" value, since it doesn't exist anymore?
I thought about using session values other than global values, because there should be a group of 4 agents per user working. Since global is shared by everyone, any agent of any user would see each other global variables, when I want just the group of 4 per user see each other values, no matter how the other groups are doing.
Is this way better than session tracking? (in the case we lost the "user" variable)
Originally posted by Paul Lester:
You mention that you can your adapt method inside the destroy() method of a servlet. This won't work as the servlet is not destroyed when the user's session times out. The destroy method of the servlet is only called when the container is about to unload the servlet from memory, as in a shutdown.

Oh, I thought that after time out the servlets were destroyed. Thanks for clearing that up.
[ May 18, 2003: Message edited by: Rafael Prado ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: agents as servlets
 
Similar Threads
How integrate the forum
Call destroy() inside init()
Relationship between locking and GUI
Chapter 2 (HFSJ) notes , may be useful for anyone
Grouping servlets