wood burning stoves*
The moose likes Servlets and the fly likes Grouping servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Grouping servlets" Watch "Grouping servlets" New topic
Author

Grouping servlets

Rafael Prado
Ranch Hand

Joined: May 17, 2003
Posts: 33
As I said in my previous topic, I am implementing a group of 4 agents(servlets) for each user logged in the site.
I don't want to make the agents being called in every click anymore. Instead, they are created as soon as the user logs in, then stay in a while (true) loop checking messages until one of this conditions is met:
1. Message is equal "logout"
2. Session expires
As mentioned earlier, both ways should let the agents do a final job in the database.
Questions:
1. how can I make the message variable specific to each group of 4 agents?
2. is it possible to be in a while(true) loop and get out when the session listener alerts the session is over?
3. the loop can be inside the doPost() method?
Thanks in advance.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

HTTP is request/response based, and this is not what you are doing.
I think you're stepping too far outside the responsibility of the server and all bets are off. The only way you are going to find out if it will work on your particular server is to try it, and it is almost guaranteed not to work on the next server or even the next version.
Dave
Rafael Prado
Ranch Hand

Joined: May 17, 2003
Posts: 33
I undestand what you're saying, but isn't the way agents are supposed to work?
I've read that servlets are used to build a community of agents, but I can't find anything further.
Do you have any tips on what should I do to get my agents working? Doesn't need to be servlets, then.
EDIT: I guess I'll try using normal beans, putting each group in the session scope, hence separating each user.
[ May 29, 2003: Message edited by: Rafael Prado ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
I think your problem is that you are trying to combine the functions of the agents and the servlets. The servlet should just be interface components and the agent functions should be contained in separate objects.
Remember that servlets live in a request/response world and can only store user specific data via sessions. In order to make the agents specific to a user, you should create your agent objects and attach them to a session for the user.
Bill
Rafael Prado
Ranch Hand

Joined: May 17, 2003
Posts: 33
Originally posted by William Brogden:
I think your problem is that you are trying to combine the functions of the agents and the servlets. The servlet should just be interface components and the agent functions should be contained in separate objects.
Remember that servlets live in a request/response world and can only store user specific data via sessions. In order to make the agents specific to a user, you should create your agent objects and attach them to a session for the user.
Bill

Let me get this straight. Should I have a servlet for checking the request and response, and a session agents running in the background (the while (true) loop)?
The Servlet1 is responsible of noticing that a link is clicked. It stores a session variable called 'link' to the url the user clicked.
Now, since Agent1 is always checking for that session value, it'll notice the variable 'link' changed and it'll do its job.
So then I have
AgComunicatorServlet (servlet is the agent)
This will be called in login and logoff actions, setting 'user' session variable (needed for the other agents) as well as forwarding to the main page.
AgLinkMgrServlet, AgLinkMgr and AgShortcutMgr
The servlet will be called every time a link is chosen, setting the session variable 'link'.
Both the agents will notice this change.
AgComboboxMgrServlet and AgComboboxMgr
Similar of the above, but a little more simple since only one agent is required.
Maybe more session variables are needed, such as 'action' that will tell the agents if they need to observe user activity or just commence adaptation.
What do you think?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16019
    
  20

You can't code a processing loop in a servlet. The servlet HAS to be coded as a "get in, do something, return something" one-shot and not some kind of continuously-running engine. If the servlet doesn't return in time, the web client will time out. Plus you risk hanging the entire server system.
If this were not so, you could code real "Model/View/Controllet" apps (well, unsolicted HTTP messages would be needed, too). As it is, we had to evolve Model 2, which is an "MVC" style approach that can live within the HTTP strictures of request/quick-action/response.
About the closest approach I could recommend would be to filter incoming HTTP requests so that the common code gets hit before the indivdual servlets
just as the security subsystem vets requests to see if the user is authenticated for that request.


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

Joined: May 17, 2003
Posts: 33
Originally posted by Tim Holloway:
You can't code a processing loop in a servlet. The servlet HAS to be coded as a "get in, do something, return something" one-shot and not some kind of continuously-running engine. If the servlet doesn't return in time, the web client will time out. Plus you risk hanging the entire server system.

I know that the servlet mustn't have the loop, that's why I broke it in 2 things. I'm having the servlet just adjust the session variables and redirect to the desired page; and the agent being a bean (inside the session) that reads session variables.
Originally posted by Tim Holloway:
If this were not so, you could code real "Model/View/Controllet" apps (well, unsolicted HTTP messages would be needed, too). As it is, we had to evolve Model 2, which is an "MVC" style approach that can live within the HTTP strictures of request/quick-action/response.

I'll look into MVC.
Originally posted by Tim Holloway:
About the closest approach I could recommend would be to filter incoming HTTP requests so that the common code gets hit before the indivdual servlets
just as the security subsystem vets requests to see if the user is authenticated for that request.

Didn't understant this. What could be the "common code"?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Let me get this straight. Should I have a servlet for checking the request and response, and a session agents running in the background (the while (true) loop)?

Exactly what is an agent supposed to do besides track the activities of the user? Does an agent have a Thread of its own that goes out and does stuff?
Servlet applications typically have variables held in a session that track the activity of a particular user. These might be simple variables or special classes - for which JavaBeans conventions are typically used.
Bill
Rafael Prado
Ranch Hand

Joined: May 17, 2003
Posts: 33
Originally posted by William Brogden:

Exactly what is an agent supposed to do besides track the activities of the user? Does an agent have a Thread of its own that goes out and does stuff?

An agent will track the activities while user is logged on the website. If the user logged off they'll stop tracking and will commence an adaptation method, altering records in the database.
The AgComunicator servlet would set the 'user' and 'action' variables.
The other 2 servlets would set the 'link' (or 'destination') variables.
The agents will read those values. Depending on the action, track or adapt, they'll read the other variables.
What am I missing?
Originally posted by William Brogden:
Servlet applications typically have variables held in a session that track the activity of a particular user.

Wait, isn't the servlet global variables shared with EVERY servlet? This would make different user servlets see each other's messages, wouldn't it?
[ May 30, 2003: Message edited by: Rafael Prado ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Thats exactly why you never use "global" variables to hold user specific data. I see no reason why you could not implement your "agents" as instances held in the user's session. If all servlets are in the same "web application" they will share the same user's session.
Perhaps you should download the servlet API from java.sun.com and study the discussion of sessions.
Bill
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
they are created as soon as the user logs in, then stay in a while (true) loop checking messages until one of this conditions is met:
1. Message is equal "logout"
2. Session expires

I'm still puzzled as to why you feel the need to have a loop here. Why can't you just place an object in a session and have your servlet simply call a method on it when a "logout" request is received or a session expires? Neither of these need a loop, as far as I can tell, but both allow the object to clean up the database etc.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Rafael Prado
Ranch Hand

Joined: May 17, 2003
Posts: 33
Well, I guess the simple request-respond feature of servlets, as Frank asid, would be enough for the servlets to be seen as agents, since they will be communicating with each other through session variables.
If that's not hurting the agent philosophy, then that's the way I'm going.
Case closed? :-)
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Thats good for a start but you can still expand within the same framework. An agent could have a Thread of its own that goes out and does something time consuming, processing the data and having it ready for the next request from the user. There are plenty of fun Java technologies that could be used - Java Message Service, JavaSpaces, SOAP, etc. etc.
Bill
Rafael Prado
Ranch Hand

Joined: May 17, 2003
Posts: 33
Thanks a lot for the help!
I can go on from here.
 
wood burning stoves
 
subject: Grouping servlets