null7 wrote:
> Raymond DeCampo <> writes:
>
>
>>null7 wrote:
>>
>>>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
>>
>>--
>>This signature intentionally left blank.
>
>
> 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
--
This signature intentionally left blank.
|