Servlet startup completion

R

Rob Whiteside

Hi,

The "init" method for a servlet provides a place for you to do things
during the Web Server's startup. Is there a way to know when the Web
Server has completely finished starting up and is forwarding requests
to its servlets?

My situation is this:
When my servlet starts, it needs to populate the database with some
data that it grabs from another web service. The interaction with
that web service is such that I send it a request for data, and the
web service immediately responds, then later it sends the data to my
servlet via http post/SOAP.

The problem is that when I send that request for data in the servlet's
init method, I have no guarantee that my Web Server is actually
started and receiving requests. So, if the external web service posts
its data to me right away, I never get it.

I wish there was another method in the servlet that got called when
the web server startup was complete.

currently, my solution is to spawn a thread in my init method that
sends requests to myself over and over until I get a valid response
code.

I was hoping that I was missing a simpler solution (and one slightly
less hokey).

Currently I am using Apache Tomcat, but the solution must be generic
to all web servers

Thanks for your help!
 
K

KosciaK

Rob Whiteside pisze:
Hi,

The "init" method for a servlet provides a place for you to do things
during the Web Server's startup. Is there a way to know when the Web
Server has completely finished starting up and is forwarding requests
to its servlets?

Check the ServletContextListener interface
 
L

Lew

That is not when init() runs. The init() method runs when
the servlet is being placed into service. ....
The servlet container calls the init method exactly once after instantiating the servlet.
The init method must complete successfully before the servlet can receive any requests.
<http://java.sun.com/javaee/5/docs/api/javax/servlet/Servlet.html#init(javax.servlet.ServletConfig)>

The web server could have been running for years when init() is called. For
that matter, the init() method for that servlet class might have been called
before any number of times on other instances of the servlet.
 
R

Rob Whiteside

That is not when init() runs. The init() method runs when> the servlet is being placed into service.
...

<http://java.sun.com/javaee/5/docs/api/javax/servlet/Servlet.html#init...)>

The web server could have been running for years when init() is called. For
that matter, the init() method for that servlet class might have been called
before any number of times on other instances of the servlet.

Thanks Lew,
you are quite right, I just tested the servletContextListener, and it
isn't what I was looking for after all.

I suppose I should have read the servlet javadoc more carefully,
because it says pretty plainly, that a servlet will not be put into
service until the init method has completed successfully.

Do you know of a way to verify that init is complete?
 
R

Rob Whiteside

Do you know of a way to verify that init is complete?

Or more specifically, that the servlet has been put into service
 
L

Lew

Rob said:
Or more specifically, that the servlet has been put into service

I don't know what you mean by "verify". There are two ways that I can think
of offhand - program invariant checks and log output.

Presumably you care about init() because you want it to establish some
condition for the rest of the servlet's life. Let's say it's to make sure
that the servlet context-wide variable 'foo' has been set to a sensible value,
say, not null. Then in your service() method or the doPost() / doGet() called
by service(), you start with a check for 'foo != null' before doing any other
work.

Of course, the service() method isn't even called unless init() completed.
This technique simply ensures that it completed correctly.

Another, complementary technique is to issue a log statement at the end of the
init() method. This tells you afterward if init() completed, and usually
when. The invariant-check technique ensures your program will only proceed if
init() had completed correctly in the moment.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top