sentence
stringlengths 1
1.38k
| label
stringclasses 3
values |
---|---|
(Unless your remote systems are underpowered, but then why are you using Java on them?
|
o
|
) Take a look at URL_http://ws.apache.org/xmlrpc/ [Apache's-XML-RPC] package.
|
o
|
It's easy to use, completely hides the network stuff from you, and works over good ol' HTTP.
|
p
|
No need to worry about protocol issues ... it'll all look like method calls to you, on both ends.
|
n
|
XML-RPC looks interesting, but I'm using SE, and I think adding/switching to EE is much more complicated than even NIO, but correct me if I'm wrong (do they run on the same JVM?
|
o
|
).
|
o
|
It's a plain library and should work in SE and EE.
|
o
|
Like another commentator mentioned, it does have its scalability issues.
|
o
|
but for the 98% case, I think it's pretty solid.
|
o
|
Given the small number of connections involved, java.net sounds like the right solution.
|
o
|
Other posters talked about using XML-RPC.
|
o
|
This is a good choice if the volumes of data being shipped are small, however I have had bad experiences with XML- based protocols when writing inter-process communications that ship large amounts of data (e.g.
|
n
|
large request/responses, or frequent small amounts of data).
|
o
|
The cost of XML parsing is typically orders of magnitude higher than more optimised wire formats (e.g.
|
p
|
ASN.1).
|
o
|
For low volume control applications the simplicity of XML-RPC should outweigh the performance costs.
|
n
|
For high volume data communications it may be better to use a more efficient wire protocol.
|
p
|
There is almost no reason to write this kind of networking code from scratch now.
|
o
|
Packages like URL_http://netty.io/ [netty.io] will almost always get you more reliable and flexible code with fewer lines of code than a hand-crafted solution will.
|
p
|
Also, with Netty, you can get SSL support w/o complicating your implementation at all.
|
o
|
Libraries like netty also obviate the "async vs threads" question almost entirely, gives you good performance, and still lets you tweak the threading model as needed.
|
p
|
Java multithreaded file downloading performance.
|
p
|
Having recently worked on a project which required some more IO interaction than I'm used to, I felt like I wanted to look past the regular libraries (Commons IO, in particular) and tackle some more in depth IO issues.
|
o
|
As an academic test, I decided to implement a basic, multi-threaded HTTP downloader.
|
o
|
The idea is simple: provide a URL to download, and the code will download the file.
|
p
|
To increase download speeds, the file is chunked and each chunk is downloaded concurrently (using the HTTP CODETERM1 header) to use as much bandwidth as possible.
|
o
|
I have a working prototype, but as you may have guessed, it's not exactly ideal.
|
n
|
At the moment I manually start 3 "downloader" threads which each download 1/3 of the file.
|
o
|
These threads use a common, synchronized "file writer" instance to actually write the files to disk.
|
o
|
When all threads are done, the "file writer" is completed and any open streams are closed.
|
o
|
Some snippets of code to give you an idea: The thread start-up: CODESNIPPET_JAVA1 .
|
o
|
Each "downloader" thread downloads a chunk (buffered) and uses the "file writer" to write to disk: CODESNIPPET_JAVA2 .
|
o
|
The "file writer" writes to disk using a CODETERM2 to CODETERM3 and CODETERM4 the chunks to disk: CODESNIPPET_JAVA3 .
|
o
|
All things considered, this approach seems to work.
|
o
|
However, it doesn't work very well.
|
o
|
I'd appreciate some advice/help/opinions on the following points.
|
p
|
Much appreciated.
|
p
|
1.
|
o
|
The
usage** of this code is through the roof.
|
o
|
It's using half my CPU (50% of each of the 2 cores) to do this, which is exponentially more than comparable downloading tools which barely stress the CPU at all.
|
o
|
I'm a bit mystified as to where this CPU usage comes from, as I wasn't expecting this.
|
n
|
2.
|
o
|
Usually, there seems to be 1 of the 3 threads that is
behind** significantly.
|
o
|
The other 2 threads will finish, after which it takes the third thread (which seems to be mostly the first thread with the first chunk) 30 or more seconds to complete.
|
o
|
I can see from the task manager that the javaw process is still doing small IO writes, but I don't really know why this happens (I'm guessing race conditions?).
|
n
|
3.
|
o
|
Despite the fact that I've chosen quite a big buffer (1MB), I get the feeling that the CODETERM5 almost never actually fills the buffer, which causes more IO writes than I would like.
|
o
|
I'm under the impression that in this scenario, it would be best to keep the IO access to a minimum, but I don't know for sure whether this is the best approach.
|
p
|
4.
|
o
|
I realise Java may not be the ideal language to do something like this, but I'm convinced there's much more performance to be had than I get in my current implementation.
|
p
|
Is NIO worth exploring in this case?
|
o
|
_Note:_ I use Apache HTTPClient to do the HTTP interaction, which is where the CODETERM6 comes from (in case anyone is wondering).
|
o
|
Found a good related topic here: URL_http://stackoverflow.com/questions/921262 /how-to-download-and-save-a-file-from-internet-using-javaMight give that a try tonight when I get home :).
|
p
|
Update: the high CPU usage was due to a while() loop on the ExecutorService isTerminated() method call.
|
o
|
Doh!.
|
o
|
To answer my own questions: 1.
|
o
|
The increased CPU usage was due to a CODETERM1 loop that was waiting for the threads to finish.
|
o
|
As it turns out, CODETERM2 is a much better alternative to wait for an CODETERM3 to finish :) 2.
|
o
|
(And 3 and 4) This seems to be the nature of the beast; in the end I achieved what I wanted to do by using careful synchronization of the different threads that each download a chunk of data (well, in particular the writes of these chunks back to disk).
|
n
|
Presumably the Apache HTTP client will be doing some buffering, with a smaller buffer.
|
o
|
It will need a buffer to read the HTTP header reasonably, and probably handling chunked encoding.
|
o
|
My immediate thought for best performance on Windows would be to use URL_http://msdn.microsoft.com/en-us/library/aa365198%28VS.85%29.aspx [IO- completions-ports] .
|
p
|
What I don't know is (a) whether there are similar concepts in other OSes, and (b) whether there are any suitable Java wrappers?
|
o
|
If portability isn't important to you, though, it may be possible to roll your own wrapper with JNI.
|
o
|
Set a very large socket receive buffer.
|
o
|
But really your performance will be limited by the network bandwidth, not CPU bandwidth.
|
o
|
All you're doing really is allocating 1/3 of the network bandwidth to each downloader.
|
o
|
I'd be surprised if you get much benefit.
|
p
|
Three connections can be faster than one, briefly at the start of a transfer.
|
p
|
It takes TCP a little whiel to find the optimal window size, so if you use parallel connections, this process goes 3x faster!.
|
p
|
This is the reason I'm chunking in the first place, splitting the file in 3 chunks allows me to download the same file 3 times faster, presuming a single chunk is too slow to saturate the connection.
|
n
|
The download speed is maxed out though, so this is not an issue.
|
o
|
It's the CPU usage that I'm worried about.
|
n
|
Could it be related to the fact that the code is executing in Eclipse?
|
o
|
Well you're doing N-1 redundant seeks, and last time I looked at it, which is decades ago, seek was a surprisingly expensive operation.
|
p
|
Each writer only needs to seek once; after that it is just sequential I/O.
|
o
|
Read/convert an InputStream to a String.
|
o
|
If you have CODETERM1 object, how should you process that object and produce a CODETERM2 ?
|
o
|
Suppose I have an CODETERM3 that contains text data, and I want to convert this to a CODETERM4 (for example, so I can write the contents of the stream to a log file).
|
o
|
What is the easiest way to take the CODETERM5 and convert it to a CODETERM6 ?
|
o
|
CODESNIPPET_JAVA1 .
|
o
|
Boy, I'm absolutely in love with Java, but this question comes up so often you'd think they'd just figure out that the chaining of streams is somewhat difficult and either make helpers to create various combinations or rethink the whole thing.
|
n
|
You are right.
|
o
|
I tend to use a set of helper classes that do it once for me, so I don't need keep referring to Google or even StackOverflow for the answer.
|
o
|
In this case, I was away from my utility code and couldn't remember exactly how to do it.
|
o
|
What better way to open my account on the site.
|
o
|
Yeah, it really shouldn't require so much boilerplate to do something as simple as read strings from a stream.
|
n
|
It's not that difficult, just annoying.
|
n
|
@Adam: It really depends on what kind of Stream you're working with.
|
p
|
For instance, System.console().readLine() (new in Java 6) is pretty easy.
|
p
|
Same with BufferedReader's readLine().
|
o
|
The only hard part is when you don't know how many characters you need to read.
|
n
|
The answers to this question only work if you want to read the stream's contents _fully_ (until it is closed).
|
o
|
Since that is not always intended (http requests with a keep-alive connection won't be closed), these method calls block (not giving you the contents).
|
o
|
You
to know and specify the character encoding for the stream, or you
have character encoding bugs, since you will be using a randomly chosen encoding depending on which machine/operating system/platform or version thereof your code is run on.
|
n
|
That is, do
use methods that depend on the platform default encoding.
|
o
|
I don't really know Java, but "InputStream
" seems wrong.
|
n
|
@Hello71: It is ok in Java, since the instanceof operator is spelled differently.
|
o
|
@Ronald: Erm... never mind that.
|
o
|
I must have been partially asleep while writing that.
|
o
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.