sleeping in servlet deffers writing to res output stream.

Discussion in 'Java' started by null7, Jan 23, 2006.

  1. null7

    null7 Guest

    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 ) { }
    }
    }
     
    null7, Jan 23, 2006
    #1
    1. Advertising

  2. 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.
     
    Raymond DeCampo, Jan 23, 2006
    #2
    1. Advertising

  3. null7

    null7 Guest

    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
     
    null7, Jan 23, 2006
    #3
  4. 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.
     
    Raymond DeCampo, Jan 29, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. circuit_breaker
    Replies:
    2
    Views:
    2,081
    Jack Jia
    Apr 4, 2004
  2. Pawel
    Replies:
    2
    Views:
    995
    William Brogden
    Apr 12, 2006
  3. =?utf-8?B?6L65IOaxnw==?=

    [Servlet] how to send a stream to servlet doPost

    =?utf-8?B?6L65IOaxnw==?=, Sep 14, 2007, in forum: Java
    Replies:
    3
    Views:
    668
    GArlington
    Sep 14, 2007
  4. Leiradella, Andre V Matos Da Cunha

    RES: RES: Bare-bones Ruby

    Leiradella, Andre V Matos Da Cunha, Dec 29, 2004, in forum: Ruby
    Replies:
    1
    Views:
    322
    Stefan Schmiedl
    Dec 29, 2004
  5. Leiradella, Andre V Matos Da Cunha

    RES: RES: RES: Bare-bones Ruby

    Leiradella, Andre V Matos Da Cunha, Dec 29, 2004, in forum: Ruby
    Replies:
    0
    Views:
    149
    Leiradella, Andre V Matos Da Cunha
    Dec 29, 2004
Loading...

Share This Page