27. CloseableHttpAsyncClient의 사용과 최적화
3152 단어 기타
동기와 비동기의 소개, 사용 장면
이 문서는 비동기 코드에 대한 소개입니다.
//Basic
private static final CredentialsProvider credsProvider = new BasicCredentialsProvider();
// HTTP
private static final RequestConfig reqestConfig;
// http
private static final CloseableHttpAsyncClient httpClient;
// get
private static final HttpGet httpGet;
//jackson
private static final ObjectMapper mapper = new ObjectMapper();
static {
System.setProperty("http.maxConnections","50");
System.setProperty("http.keepAlive", "true");
// BASE
credsProvider.setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
new UsernamePasswordCredentials(“”,“”));
// HTTP
reqestConfig = RequestConfig.custom()
.setContentCompressionEnabled(true)
.setSocketTimeout(100)
.setAuthenticationEnabled(true)
.setConnectionRequestTimeout(100)
.setConnectTimeout(100).build();
// httpClient
httpClient = HttpAsyncClients.custom()
.useSystemProperties()
.setMaxConnTotal(50)
.setDefaultCredentialsProvider(credsProvider).build();
httpClient.start();
httpGet = new HttpGet();
httpGet.setConfig(reqestConfig);
}
/*
* : HTTP GET
* @author zhangdaquan
* @date 2019/1/2 8:31
* @param [url]
* @return org.apache.http.client.methods.HttpGet
* @exception
*/
public static HttpGet get(String url) throws URISyntaxException {
httpGet.setURI(new URI(url));
return httpGet;
}
/*
* :
* @author zhangdaquan
* @date 2019/1/2 8:32
* @param [url]
* @return void
* @exception
*/
public static Future getUrl(String url) throws IOException, HttpException, URISyntaxException {
HttpGet get = get(url);
Future httpResponseFuture = httpClient.execute(get, new FutureCallback() {
@Override
public void completed(HttpResponse httpResponse) {
}
@Override
public void failed(Exception e) {
}
@Override
public void cancelled() {
}
});
return httpResponseFuture;
}
public static void main(String[] args) throws IOException, HttpException, ExecutionException, InterruptedException, URISyntaxException {
Long tm = System.currentTimeMillis();
for (int i = 0; i < 50 ; i++){
new Thread(()->{
try{
HttpResponse httpResponse = getUrl(getUrl).get();
HttpEntity entity = httpResponse.getEntity();
System.out.println(entity);
String s = EntityUtils.toString(entity);
// response httpResponse
HttpClientUtils.closeQuietly(httpResponse);
System.out.println(s);
}catch (Exception e){
e.printStackTrace();
}
}).start();
}
System.out.println(System.currentTimeMillis()-tm);*/
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Github 프로필 Badge로 꾸미기앞으로 열심히 할 예정이니 많은 관심 부탁드립니다 😍😍 나의 머리로는 이해할 수 없는 코드들을 보며 머리가 꺠지려 할 때에, 학교 친구들의 github가 눈에 들어왔습니다. 인터넷에 나온 거로 많이 해 봤고 탬플릿도...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.