3

I am trying to make a java controller class that accomplishes the following tasks:

  1. when it receives one request , it willprocess on it.

  2. when the process is done and before the return statement , I instanited one Httpclient and try to make one request exactly like current one and post it to another server.

I already done with the tasks. but I wonder what happens here : (below)

HttpResponse response=client.execute(post);// will the program blocks here ? 

As I search the internet , it seems to be true that the program blocks and that's not what I want.

I also read about the asynchronise things , but it seems to process the response in the end which may still takes some time. ( not quite sure )

How could I just send the request and cares nothing about the response. Is it possible ?

Sorry if i get everything messed up . It will be really kind of you to shed some light on my mind or just offer some advice on this topic. Thanks.

user2749336
  • 31
  • 1
  • 1
  • 2
  • What do you mean by "still takes some time"? Are you worried about how much data you'll end up receiving? Do you really not even care if the request fails (e.g. the server is down, or responds with a 500)? – Jon Skeet Sep 05 '13 at 05:54
  • do not care about the request fails. still goes one. – user2749336 Sep 05 '13 at 06:07
  • So what are you concerned about in terms of an asynchronous call, precisely? – Jon Skeet Sep 05 '13 at 06:08
  • I wonder whether the asynchronous call will still takes up resources when the response returned even I choose to ignore it ? – user2749336 Sep 05 '13 at 06:27
  • There may be *some* processing for validation etc, but it sounds like you're optimizing prematurely at this stage. Why don't you just implement it and test? What resources are you particularly concerned about? – Jon Skeet Sep 05 '13 at 06:36
  • In my case, I am just trying to add some code to some functioning project so that I get every related request the web app is receiving . The result would be that :the web app should go on as smoothly as before and should not disturbed by the request-dispatcher thing , and outside of the web app I get the extra request send out by the web app . – user2749336 Sep 05 '13 at 08:09
  • Okay, so normal: implement the simplest possible solution, and *measure* whether there's a performance impact. I doubt that there will be. – Jon Skeet Sep 05 '13 at 08:38

2 Answers2

1

My favorite asynchronous HTTP library for Java is AsyncHttpClient.

If you don't care about processing the response, then you can just do:

AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
Future<Response> f = asyncHttpClient.preparePost("http://www.myurlhere.com/").execute();

and then just forget about it.

William Gaul
  • 3,181
  • 2
  • 15
  • 21
  • I will go and try it. thanks . Personally , I don't understand what happens to asynHttpClient before the response return ? It's waiting somewhere in the memory ? then what happens when the future response returns. what happens if no response ? – user2749336 Sep 05 '13 at 08:17
  • Yes, it's running in a separate thread which is basically waiting. If there is no response I'm assuming it'll continue to wait, but that shouldn't happen. Even if something bad happens like connection issues this will throw an error and the Future will return. – William Gaul Sep 06 '13 at 00:03
0

Even though I find the question a bit confusing I think what you are after is a asynchronous Http call. This way you can choose if you want to read the response or not. There is a good post on this forum on how to write this.

How do you create an asynchronous HTTP request in JAVA?

Also, this might be of interest http://hc.apache.org/httpcomponents-client-ga/ for future references.

Community
  • 1
  • 1
Qben
  • 2,617
  • 2
  • 24
  • 36