我如何知道org.apache.http.client是否使用JavaNIO,以及它是否';换成NIO对我有好处吗?
我有以下代码:我如何知道org.apache.http.client是否使用JavaNIO,以及它是否';换成NIO对我有好处吗?,java,apache,nio,Java,Apache,Nio,我有以下代码: import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.imp
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import com.google.inject.Inject;
public class GenericHttpClient implements IGenericHttpClient {
private CloseableHttpClient client;
@Inject
@Singleton
public GenericHttpClient() {
// client = HttpClientBuilder.createForBl()
// .setConnectionTimeToLive(1, TimeUnit.MINUTES)
// .build();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(20 * 1000)
.build();
client = HttpClientBuilder
.create()
.setDefaultRequestConfig(requestConfig)
.build();
}
@Override
public String sendHttpGetRequest(String url) {
// Constants.CONFIG_QUERY_URL
String answer = null;
try {
CloseableHttpResponse response = client.execute(new HttpGet(url));
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
answer = EntityUtils.toString(response.getEntity());
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
return answer;
}
}
我如何知道org.apache.http.client
在其API下面是否使用JavaNIO或NIO.2?是否有另一个我可以使用的库在下面实现NIO
此外,我对网络所做的一切就是GET
并返回一个对象,该对象的响应是对象成员之一
因此,我的线程将在NIO之后被阻塞Future.get()
。我不确定我是否节省了任何资源
//Send the message
Future<Integer> successfullyWritten= asynchronousSocketChannel.write(helloBuffer);
//Do some stuff here. The point here is that asynchronousSocketChannel.write()
//returns almost immediately, not waiting to actually finish writing
//the hello to the channel before returning control to the currently executing thread
doSomethingElse();
//now you can come back and check if it was all written (or not)
System.out.println("Bytes written "+successfullyWritten.get());
//发送消息
Future successfullywrited=asynchronousSocketChannel.write(helloBuffer);
//在这里做些事情。这里的要点是异步socketchannel.write()
//几乎立即返回,而不是等待实际完成编写
//在将控制返回到当前执行的线程之前,向通道发送hello
doSomethingElse();
//现在您可以回来检查它是否都写了(或没有写)
System.out.println(“Bytes writed”+successfullywrited.get());
如果您关心它下面的用途,您也许应该自己编写代码?使用库的目的是抽象它下面的功能。旁注:from:HttpCore支持两种I/O模型:基于经典Java I/O的阻塞I/O模型和基于Java NIO的非阻塞、事件驱动I/O模型。我正在考虑性能问题。我是否可以使用另一个库在下面实现NIO?HTTP客户端的性能由网络而不是i/O API决定。这个问题毫无意义。但为什么,非阻塞线程不会比阻塞线程更快呢?还是说我可以在apache HTTP客户端上控制它?