Results 1 to 6 of 6
  1. #1
    zynasis is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Red face interrupting urlconnection's outputstream

    hi all,

    I would like to add timeout functionality to a urlconnection's outputstream write.
    I believe the write's flush is blocking and causing long delays on some transmissions. i am not willing to use apache http library, or any other 3rd party library for this.

    currently i am wrapping the outputstream in a seperate thread that write's, flushes and closes the stream. timeouts are implemented with a join on the thread with limit on wait of the timeout amount.

    after the join returns, i then check the status of the thread's write and if it is not complete, i close the outputstream and send an interrupt to the thread.

    firstly, this seems messy, but i do not know of a better way (please explain if there is).
    is there a better way of interrupting the outputstream? or a better way of doing this at all?

  2. #2
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Is it your code that is writing to the output stream? If so, why do you want to time it out?

    I can see setting a timeout on reading from some other site.

    But if you are creating the output, its your job to do it quickly and efficiently.

  3. #3
    zynasis is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    yes, my code is doing the write.
    however, there are network issues outside my control which re-connections and retries fix.

    the writes can take up to 15 minutes at times and i would rather have the thing bail out and stop and retry before waiting this long

  4. #4
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    You need to buffer the output so its all ready at once. You should not be generating more than say ten thousand bytes, so put them in a ByteArrayOutputStream and then send it all at once.

    You can, of course, just setup a thread with a timer and use it to try to interupt the output.

    But you really don't want to do that. Just put the output in a buffer and send it all at once.

    Your code should *not* be sending megabytes. If you need to, say send a video file, put it in an accessible place and send a URL. Let apache deliver the big stuff.

  5. #5
    zynasis is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    it already is buffering and sending in one transmission.
    the amount of data is approx 400 bytes so it isnt size problems.

    i think we hav a dicky router somewhere

  6. #6
    zynasis is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    a bit more info on the problem causing me to write this:
    - data is always the same size at approx 400 bytes
    - data writes & flushes are usually quick at less than a second
    - sometimes a single write lags up and takes over 15 minutes, meanwhile other writes are still going quick
    - i believe if we timeout the write and reconnect and send, the next attempt may be fast
    - there is no synchronization or locking, all sends are sent in new threads on new instances which are independent from each other

Similar Threads

  1. Replies: 2
    Last Post: 07-28-2008, 07:32 PM
  2. Converting InputStream to OutputStream
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 01-11-2008, 10:13 PM
  3. HashMap to share OutputStream
    By gabriel in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 05:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •