sleeping in servlet deffers writing to res output stream.

N

null7

Hallo

Below is my service method. Sleep causes that data are not written to `res'
stream immediatelly, but after some time, usually as the `while' loop
is finished and hence service method finishes.
My question is, how to make it be written immediatelly ?

Greetings

my service method:

public void service( HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException {

res.setStatus( 200 ) ;
res.setContentType( "text/plain" ) ;
PrintStream out = new PrintStream( res.getOutputStream() );

for ( int i = 1; i < 20; i++ ) {
out.println( "my text" );
try {
int timeMillis = 5000;
System.out.println( "Sleeping for a " + timeMillis/1000 + " secs..." );
out.flush(); // this and the line below does not help.
res.flushBuffer();
Thread.sleep( timeMillis ); // wait 5 seconds
} catch ( InterruptedException ie ) { }
}
}
 
R

Raymond DeCampo

null7 said:
Hallo

Below is my service method. Sleep causes that data are not written to `res'
stream immediatelly, but after some time, usually as the `while' loop
is finished and hence service method finishes.
My question is, how to make it be written immediatelly ?

Greetings

my service method:

public void service( HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException {

res.setStatus( 200 ) ;
res.setContentType( "text/plain" ) ;
PrintStream out = new PrintStream( res.getOutputStream() );

for ( int i = 1; i < 20; i++ ) {
out.println( "my text" );
try {
int timeMillis = 5000;
System.out.println( "Sleeping for a " + timeMillis/1000 + " secs..." );
out.flush(); // this and the line below does not help.
res.flushBuffer();
Thread.sleep( timeMillis ); // wait 5 seconds
} catch ( InterruptedException ie ) { }
}
}

I do not know the answer to your direct question, but I suspect we can
help you if you tell us what you are *really* trying to accomplish. Why
do you want to sleep during a servlet call?

HTH,
Ray
 
N

null7

Raymond DeCampo said:
I do not know the answer to your direct question, but I suspect we can
help you if you tell us what you are *really* trying to accomplish.
Why do you want to sleep during a servlet call?

HTH,
Ray

The servlet is going to have an open connection to its caller, for a very long time (a few days).
Its task will be to read some files on local filesystem every 15 secs and in case there
is anything to be passed to the caller, it will do that. Otherwise it will sleep for another 15 secs.
That is why I need servlet to sleep.

Greetings
 
R

Raymond DeCampo

null7 said:
The servlet is going to have an open connection to its caller, for a very long time (a few days).
Its task will be to read some files on local filesystem every 15 secs and in case there
is anything to be passed to the caller, it will do that. Otherwise it will sleep for another 15 secs.
That is why I need servlet to sleep.

This doesn't sound like something that fits the servlet architecture
very well. You might want to go with a traditional client/server
application. Or, if you want J2EE, use JMS (i.e. have your clients
subscribe to a JMS topic and have a timed app post a message to the
topic whenever there are updates to the files).

HTH,
Ray
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top