Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > sleeping in servlet deffers writing to res output stream.

Reply
Thread Tools

sleeping in servlet deffers writing to res output stream.

 
 
null7
Guest
Posts: n/a
 
      01-23-2006

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 ) { }
}
}
 
Reply With Quote
 
 
 
 
Raymond DeCampo
Guest
Posts: n/a
 
      01-23-2006
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.
 
Reply With Quote
 
 
 
 
null7
Guest
Posts: n/a
 
      01-23-2006
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.

Greetings
 
Reply With Quote
 
Raymond DeCampo
Guest
Posts: n/a
 
      01-29-2006
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is hi-res + shrink better than taking lo-res? veg_all@yahoo.com Digital Photography 15 02-02-2007 08:30 AM
servlet: writing to servlet stream that is closed Pawel Java 2 04-12-2006 01:44 PM
RES: RES: RES: Bare-bones Ruby Leiradella, Andre V Matos Da Cunha Ruby 0 12-29-2004 06:27 PM
RES: RES: Bare-bones Ruby Leiradella, Andre V Matos Da Cunha Ruby 1 12-29-2004 01:45 PM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM



Advertisments