Not restarting a servlet each time a page is requested

  • Thread starter Jonck van der Kogel
  • Start date
J

Jonck van der Kogel

Hi,
I am having a problem with a slow servlet and I'm hoping some of you
will give me some advice as to how I can improve performance.

The situation is as follows:
Users can contact my servlet upon which an object A (I'll just call it
A for easy reference) needs to be created. The creation of this object
takes a long time (a few seconds) but once created the object can be
interacted with very quickly.
What's happening now is that each time a user contacts my servlet a
response lag of a few seconds occurrs before the response is returned
due to the fact that a new A is created for each user. It is however
not at all necessary for a new A to be created for each user, A can be
reused by different users.

What I would like to do, is keep A in memory and reuse it every time a
new user contacts my servlet. But as far as I know this is not
possible with servlets, the page just gets run each and every time the
page is called.
If I run a seperate application on the server that keeps object A in
memory I suppose my problems are solved, but then how do I contact
this application from my servlet?
I was thinking of using RMI (running both the client and the server on
the same machine), but I had understood that RMI is rather slow and
speed is of the essence.

My question therefore is, do any of you know of a speedy solution with
which I can keep a Java object in memory and call this from a servlet
each time a user contacts me?

Thanks in advance for any help, Jonck
 
P

Peter Kirk

"Jonck van der Kogel" <[email protected]> skrev i en meddelelse
What I would like to do, is keep A in memory and reuse it every time a
new user contacts my servlet. But as far as I know this is not
possible with servlets, the page just gets run each and every time the
page is called.
<snip>

Can't you just create a single A as an instance variable of your servlet?
For example, create it when the servlet initialises?
 
C

Christophe Vanfleteren

Peter Kirk said:
"Jonck van der Kogel" <[email protected]> skrev i en meddelelse

<snip>

Can't you just create a single A as an instance variable of your servlet?
For example, create it when the servlet initialises?

Or put it in the application context, so it is also available in other
servlets.
You have to be sure that your A is threadsafe though, if you're allowing
concurrent acces to it.
 
A

Andy Fish

Christophe Vanfleteren said:
Or put it in the application context, so it is also available in other
servlets.
You have to be sure that your A is threadsafe though, if you're allowing
concurrent acces to it.

of course if it is not threadsafe, you will need to either synchronize on it
(so only one person can use it at a time) or create a pool of "A"s, so that
each one can handle a single request concurrently
 
J

Jonck van der Kogel

Hi everyone,
Thanks for all your quick replies!

As to creating A as an instance variable of my servlet, doesn't that
mean that a new instance of A needs to be created each time a new user
accesses the servlet? I'm not too sure on this, since I'm very new to
servlets (usually use PHP for my webpages, but doesn't the Tomcat
server load a new instance of your servlet class each time a call is
made to the URL associated with it?

For example, lets say I have the following servlet class:
public class TestServlet extends HttpServlet {
  public void doGet (HttpServletRequest request, HttpServletResponse
response) {
//do something
}
}

I could of course place an instance variable of A at the beginning of
TestServlet. But then if a user calls TestServlet, the server needs to
create a new TestServlet (and therefore a new A) right?

Or is this where I am mistaking? Do servlets remain in memory and can
therefore have persistent instance variables?

Thanks again, Jonck


Andy Fish said:
Christophe Vanfleteren said:
Or put it in the application context, so it is also available in other
servlets.
You have to be sure that your A is threadsafe though, if you're allowing
concurrent acces to it.

of course if it is not threadsafe, you will need to either synchronize on it
(so only one person can use it at a time) or create a pool of "A"s, so that
each one can handle a single request concurrently
 
C

Christophe Vanfleteren

Jonck van der Kogel wrote:

Please don't top-post.
Hi everyone,
Thanks for all your quick replies!

As to creating A as an instance variable of my servlet, doesn't that
mean that a new instance of A needs to be created each time a new user
accesses the servlet? I'm not too sure on this, since I'm very new to
servlets (usually use PHP for my webpages, but doesn't the Tomcat
server load a new instance of your servlet class each time a call is
made to the URL associated with it?

For example, lets say I have the following servlet class:
public class TestServlet extends HttpServlet {
public void doGet (HttpServletRequest request, HttpServletResponse
response) {
//do something
}
}

I could of course place an instance variable of A at the beginning of
TestServlet. But then if a user calls TestServlet, the server needs to
create a new TestServlet (and therefore a new A) right?

Or is this where I am mistaking? Do servlets remain in memory and can
therefore have persistent instance variables?

Yes, you are wrong, Servlets are not like PHP scripts that get loaded on
each request. A servlet is a class like any other, and gets loaded once.
Then one instance of that class is created and is used to serve all the
clients (multiple threads acces the same instance at the same time).

So if you put some value in an instance variable, it will be the same for
all clients.
 
L

Liz

Christophe Vanfleteren said:
Jonck van der Kogel wrote:

Please don't top-post.


Yes, you are wrong, Servlets are not like PHP scripts that get loaded on
each request. A servlet is a class like any other, and gets loaded once.
Then one instance of that class is created and is used to serve all the
clients (multiple threads acces the same instance at the same time).

So if you put some value in an instance variable, it will be the same for
all clients.

I just started reading a book on Java Servlets and it says that there is a
xml file used for deployment and that a single servlet can have different
names
and that there will be an instance for each name. They discuss this idea
of caching information by using a field that contains the last time/date
of update.
 
A

Arvind

Instance of Servlets get created ONLY the first time they get loaded -
after that doget,dopost or other methods get called repeatedly on a
per request basis.

The first thing servlet developers are warned is about the same.

Any instance variable in a servlet - is subject to thread contention -
multiple requests *WILL* share the same instance variable and hence
inherently thread unsafe.

In your case since object A need not be recreated every time and also
looks like requests do NOT alter the state of the obj A - you could
safely make that as an instance variable and if need be load it up
during the init method - in which case even the first user does not
have to WAIT !!

Arvind
 
N

Nigel Wade

Liz said:
I just started reading a book on Java Servlets and it says that there is a
xml file used for deployment and that a single servlet can have different
names
and that there will be an instance for each name. They discuss this idea
of caching information by using a field that contains the last time/date
of update.

Yes, that is indeed true. Each servlet is an instance of a class which
implements the Servlet interface, usually by extending either GenericServlet
or HttpServlet. There can be multiple servlets instantiated from the same
class; each of those servlets will share class variables.

Instance variables of the class will only be available to the servlet, but
will be persistent across calls of the doGet()/doPost()/service() methods.

Access to class and instance variables *must* be synchronized.
 
A

Andy Fish

<snip everything>

it's worth pointing out that it is possible to mark your servlet as single
threaded in which case the servlet container might (a) create multiple
instances of your servlet; or (b) only use one instance and synchronize all
access to it.

As a matter of good practice, I think you should ensure your servlet is
thread safe but also do not assume it is a singleton. This should not be
difficult and then you have all the bases covered.

Andy
 
J

Jonck van der Kogel

Sorry, didn't know this was a convention. (See below for rest of post
:) )

Wow, this is really cool. And this will not cause the "dirty write"
problem? By this I mean:
-if user 1 calls the setSomething method of A to enter some info in a
property of A
-right before user 1 calls the doSomething method, user 2 accesses the
setSomething method and so overwrites the info entered by user 1
-if user 1 then goes on to call the doSomething method it will use the
info of user 2

This will thus not happen with servlets? I guess the Tomcat server
then somehow maintains a pool of the servlet class and directs the
different calls to different instances of the class as they are freed
up?

Sorry if I'm asking really obvious questions here, and thanks very
much for your answers!

Regards, Jonck
 
J

Jonck van der Kogel

Hi folks,
Sorry, I use google to read newsgroups and its apparently a bit slow
in posting. Before my last question was posted I see my questions have
already been answered :)

I want to thank all of you for answering my question, you're very
kind.

All the best, Jonck
 
N

Nigel Wade

Christophe said:
Nigel Wade wrote:




SingleThreadModel is also deprecated as of Servlet 2.4.

Really? I'd better start reading the specs.

Good job I made my servlets thread safe ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top