Code Comments
Programming Forum and web based access to our favorite programming groups.Hello,
Java v1.4.2_05.
I am using the URL and HttpsURLConnection classes in a program. As soon
as the openConnection() method is called, the CPU usage goes to 100%
greatly slowing the functioning of other programs. When the connection
closes, the usage drops back to normal.
Is this expected? A possible problem in the JVM?
Is there an alternate, low CPU usage, way of connecting?
Here is the code that pegs the CPU meter (less the exception code):
URL url;
String url_str = protocol + "://" + hostname
+ "/" + prefix + "/" + service;
// Create new URL and connect
//
// Let Sun's JSSE to deal with SSL
java.security.Security.addProvider(
new com.sun.net.ssl.internal.ssl.Provider());
System.getProperties().put("java.protocol.handler.pkgs",
"com.sun.net.ssl.internal.www.protocol");
url = new URL(url_str);
HttpsURLConnection connection =
(HttpsURLConnection) url.openConnection();
// Setup HTTP POST parameters
connection.setDoOutput (true);
connection.setDoInput (true);
connection.setUseCaches(true);
// Get POST data from input StringBuffer containing an XML document
//
String queryString = XmlIn.toString();
// POST data
//
System.out.println("...sending request..."); System.out.flush();
OutputStream out = connection.getOutputStream();
out.write(queryString.getBytes());
out.close();
// get data from URL and return the XML doc as a StringBuffer
//
String data = "";
data = readURLConnection(connection);
// Data is saved and the function exits here closing the connection.
--
jmm dash list (at) sohnen-moe (dot) com
(Remove .AXSPAMGN for email)
Post Follow-up to this messagejmm-list-gn <jmm-list.AXSPAMGN@sohnen-moe.com> wrote in message news:<0qWdnXdwQ6w7JM7cRVn-s Q@giganews.com>... > Hello, > Java v1.4.2_05. > I am using the URL and HttpsURLConnection classes in a program. As soon > as the openConnection() method is called, the CPU usage goes to 100% > greatly slowing the functioning of other programs. When the connection > closes, the usage drops back to normal. > Is this expected? A possible problem in the JVM? Which JVM would that be? ;-) > Is there an alternate, low CPU usage, way of connecting? Almost certainly, the fault is in your code. You don't specify what the readURLConnection method does. It's possible there's some bad logic in there controlling a loop. That's the most likely possibility. SSL is another candidate, although I'd rule out your own code first, before you start to blame Sun's. -FISH- ><>
Post Follow-up to this messageFISH wrote: > > Which JVM would that be? ;-) > java version "1.4.2_05" Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_05-b04) Java HotSpot(TM) Client VM (build 1.4.2_05-b04, mixed mode) > > > Almost certainly, the fault is in your code. You don't specify what > the readURLConnection method does. > Oops. Here it is: int letter; reader = new BufferedReader(new InputStreamReader(uc.getInputStream())); while (-1 != (letter = reader.read())) buffer.append((char) letter); reader.close(); I'd have thought that read() would block until there was actual input. -- jmm dash list (at) sohnen-moe (dot) com (Remove .AXSPAMGN for email)
Post Follow-up to this message"jmm-list-gn" <jmm-list.AXSPAMGN@sohnen-moe.com> schreef in bericht news:0qWdnXdwQ6w7JM7cRVn-sQ@giganews.com... > Hello, > Java v1.4.2_05. > I am using the URL and HttpsURLConnection classes in a program. As soon > as the openConnection() method is called, the CPU usage goes to 100% > greatly slowing the functioning of other programs. When the connection > closes, the usage drops back to normal. > Is this expected? A possible problem in the JVM? Any unbuffered system stream is likely to draw 100% CPU, period.
Post Follow-up to this messageBoudewijn Dijkstra wrote: > > Any unbuffered system stream is likely to draw 100% CPU, period. > Hmm, yes. Most of the time is spent in the HttpsURLConnection methods. readURLConnection() is a buffered stream. So your implication is that unbuffered streams are used in the http and URL classes. Is there a way to use buffered streams? -- jmm dash list (at) sohnen-moe (dot) com (Remove .AXSPAMGN for email)
Post Follow-up to this message"jmm-list-gn" <jmm-list.AXSPAMGN@sohnen-moe.com> schreef in bericht news:IsidnaBhcZDKLcncRVn-oA@giganews.com... > Boudewijn Dijkstra wrote: > Hmm, yes. Most of the time is spent in the HttpsURLConnection methods. > readURLConnection() is a buffered stream. > So your implication is that unbuffered streams are used in the http and > URL classes. Is there a way to use buffered streams? You can buffer an arbitrary stream by wrapping it into a new java.io.BufferedXxxputStream, like so: myBufferedInput = new BufferedInputStream(myInput); or myBufferedOutput = new BufferedOutputStream(myOutput); You can then use the new stream in the same way as the old one, or even use the same reference: myXxxput = new BufferedXxxputStream(myXxxput);
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.