At first I thought this is a programming issue as I am creating connections every 10 seconds.Which might get into creating connections and not properly closing it.But as suggested its not programming but an implementation/design issue.
I need to check for any new resource available every 5 or 10 seconds ex: testign.com/list_items. The webservices are developed using cakephp.
If any resource available I will be processing it. Can anyone help/guide me to connect to url and poll the new resource every 10 secs.
Every 10 secs doing the below code gives a connection refused or connection timed out exception after 20 loops and also after the exception starts hitting in, the url cannot be loaded on the browser.
oracleIncoming = new URL("http://calvins.restasy.com/bills/list_current/Kitchen");
inputStreamReaderList = new InputStreamReader(oracleIncoming.openStream());
//= new InputStreamReader(oracle.openStream());
for (int j = 0; j < 10;) {
URLReader.inputStreamReaderList.close();
inputStreamReaderList = new InputStreamReader(oracleIncoming.openStream());
BufferedReader in = new BufferedReader(URLReader.inputStreamReaderList);
String inputLine;
while ((inputLine = in.readLine()) != null) {
URLReader.storeInput = URLReader.storeInput + inputLine;
}
in.close();
URLReader.storeInput = "";
//send response with bill no
if (URLReader.sendBillNo == true) {
URLReader.sendBillNo = false;
oracle = new URL("http://calvins.restasy.com/bills/successfully_generated/" +billNo+"/"+"Kitchen");
inputStreamReader = new InputStreamReader(oracle.openStream());
try {
BufferedReader in2 = new BufferedReader(inputStreamReader);
String inputLine2;
while ((inputLine2 = in2.readLine()) != null) {
urlBillData2 = urlBillData2 + inputLine2;
}
in.close();
inputStreamReader.close();
} catch (Exception e) {
System.out.println(e);
}
System.out.println("Successfully Printed Order " + billNo);
//System.out.println("http://calvins.restasy.com/bills/successfully_generated/" +billNo);
}
Thread.sleep(10000);
}
} catch (Exception e) {
System.out.println(e);
callWeb();
}
Here is the stack trace:
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
at sun.net.www.http.HttpClient.New(HttpClient.java:306)
at sun.net.www.http.HttpClient.New(HttpClient.java:323)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
at java.net.URL.openStream(URL.java:1010)
at printerapp.URLReader.callWeb(URLReader.java:122)
at printerapp.URLReader.main(URLReader.java:71)
As HotLicks suggested to respond from web-service only after any resource is available, the php timeout for an execution is 30 seconds. How to keep a request alive for more than this time.Increasing PHP execution time limit would be a security issue.
Thanks in advance.