• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sending a http-request without waiting for response  RSS feed

 
Reza Motevallizadeh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read the thread "Sending a http-request without waiting for response" and I really need to know, how we can send a request, to a server without waiting for a response.

I mean I have server called: http://myserver and I want to call a service (/myService), for example a webservice or even a servlet , without waiting for its response.

Here is my code snipp:

URL targetURL = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) targetURL.openConnection();
conn.setAllowUserInteraction(true);
conn.setConnectTimeout(15000); // 15 sec. connection timeout
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-type", "text/html; charset=" + "UTF-8");

conn.connect(); // just sending the request.

logger.message("The response >>> " + conn.getResponseMessage());

conn.disconnect(); // we don't like to wait for response.


The bottel neck is
HttpURLConnection conn = (HttpURLConnection) targetURL.openConnection();
When the server is too busy, it takes a time for this connection.

IS THERE ANY WAY TO AVOID THIS ? Sending something over TCP/IP with out makeing connection, like sending a packet to a destination, that we are not sure it reach or not ?!

Thank you!
Reza

 
kiran mathew
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest two ways
1) You can use the following classes from the apache library for httpComponents
org.apache.commons.httpclient.HttpClient.HTTPClient
org.apache.commons.httpclient.params.HttpConnectionParams
In HttpConnectionParams, you can specify the timeout using
void setConnectionTimeout(int timeout)

2) Execute your code as a java.util.concurrent.FutureTask<V>. You can specify timeouts for the task.
 
Reza Motevallizadeh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kiran mathew wrote:I would suggest two ways
1) You can use the following classes from the apache library for httpComponents
org.apache.commons.httpclient.HttpClient.HTTPClient
org.apache.commons.httpclient.params.HttpConnectionParams
In HttpConnectionParams, you can specify the timeout using
void setConnectionTimeout(int timeout)

2) Execute your code as a java.util.concurrent.FutureTask&lt;V&gt;. You can specify timeouts for the task.


Hi man,
Thank you for your replay!
I think this doesn't work for me, because I have connection timeout them same as Appache. The problem is the Connection (TCP), the http protocol works on TCP/IP and I think this make much problem for me! My Server is busy all the time, so If I want to make a TCP connection for calling a service, I should spend 8secs or more to get a chance to call my service.

So I think the UDP will work for me !!! I mean a UDP socket to call an HTTP service, for example in my case, but I am not sure??!!!

- Do you have any idea about connection less socket?

- Have you any idea whether I can call a Servlet or webservice on port UDP 80 or not ???!!!

- How can I check whether my web server response me on this port ?

- What about proxy server, If I had a proxy in the middle of my communication path ?

Do have any idea?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Server is busy all the time

That being the case, maybe it's time to upgrade the server or get another one.

UDP works on a lower protocol level than TCP/IP; you can't use it to connect to an HTTP port.

Why can't you run this in a background thread? I don't understand what you mean by "I have connection timeout them same as Apache.", but the timeout for FutureTask has nothing to do with any connection timeouts.
 
Reza Motevallizadeh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
My Server is busy all the time

That being the case, maybe it's time to upgrade the server or get another one.

UDP works on a lower protocol level than TCP/IP; you can't use it to connect to an HTTP port.

Why can't you run this in a background thread? I don't understand what you mean by &quot;I have connection timeout them same as Apache.&quot;, but the timeout for FutureTask has nothing to do with any connection timeouts.


I am going to use background thread, and see what happen!

[Connection Timeout]
I have set the connection for my HttpConnection to 15 secs:
conn.setConnectTimeout(15000); // 15 sec. connection timeout
I tried to explain that I used the connection timeout like what you explained to me by Apache sample code.

Thank you !


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!