Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-01-2008, 07:37 AM
Member
 
Join Date: Oct 2008
Posts: 4
Rep Power: 0
zynasis is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-01-2008, 08:35 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 571
Rep Power: 2
fishtoprecords is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-01-2008, 08:55 AM
Member
 
Join Date: Oct 2008
Posts: 4
Rep Power: 0
zynasis is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-01-2008, 08:59 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 571
Rep Power: 2
fishtoprecords is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-01-2008, 09:04 AM
Member
 
Join Date: Oct 2008
Posts: 4
Rep Power: 0
zynasis is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-02-2008, 01:49 AM
Member
 
Join Date: Oct 2008
Posts: 4
Rep Power: 0
zynasis is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] simple server chat program, using InputStream and OutputStream. NullPointerE jim478 New To Java 2 07-28-2008 08:32 PM
Converting InputStream to OutputStream Java Tip Java Tips 1 01-11-2008 11:13 PM
HashMap to share OutputStream gabriel Advanced Java 1 08-06-2007 06:47 PM


All times are GMT +2. The time now is 02:50 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org