add:http异步请求
This commit is contained in:
@@ -369,6 +369,12 @@
|
||||
<artifactId>jsch</artifactId>
|
||||
<version>0.1.55</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpasyncclient -->
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpasyncclient</artifactId>
|
||||
<version>4.1.4</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 获取系统信息 -->
|
||||
<dependency>
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package org.nl.common.utils;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpRequestBase;
|
||||
import org.apache.http.concurrent.FutureCallback;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
|
||||
import org.apache.http.impl.nio.client.HttpAsyncClients;
|
||||
import org.nl.modules.wql.util.SpringContextHolder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/*
|
||||
* HTtp异步调用
|
||||
*/
|
||||
public class AsyncHttpRequest {
|
||||
|
||||
private StringEntity entity;
|
||||
public HttpRequestBase httpbase;
|
||||
private String url;
|
||||
|
||||
public AsyncHttpRequest(String url) {
|
||||
this.url=url;
|
||||
}
|
||||
|
||||
public static AsyncHttpRequest post(String url,String body){
|
||||
return new AsyncHttpRequest(url).body(body);
|
||||
};
|
||||
|
||||
public static AsyncHttpRequest get(String url){
|
||||
return new AsyncHttpRequest(url).getEntity();
|
||||
};
|
||||
private AsyncHttpRequest getEntity(){
|
||||
HttpGet httpGet = new HttpGet(this.url);
|
||||
this.httpbase = httpGet;
|
||||
return this;
|
||||
};
|
||||
private AsyncHttpRequest body(String body){
|
||||
this.entity = new StringEntity(body, "UTF-8");
|
||||
this.entity.setContentType("application/json");
|
||||
HttpPost httpPost = new HttpPost(this.url);
|
||||
httpPost.setEntity(entity);
|
||||
this.httpbase = httpPost;
|
||||
return this;
|
||||
};
|
||||
public Future<HttpResponse> execute(FutureCallback<HttpResponse> callback){
|
||||
CloseableHttpAsyncClient client = SpringContextHolder.getBean(CloseableHttpAsyncClient.class);
|
||||
Future<HttpResponse> execute = client.execute(httpbase, callback);
|
||||
return execute;
|
||||
};
|
||||
}
|
||||
@Component
|
||||
class HttpAsyncClientInit{
|
||||
|
||||
@Bean
|
||||
public CloseableHttpAsyncClient closeableHttpAsyncClient(){
|
||||
CloseableHttpAsyncClient aDefault = HttpAsyncClients.createDefault();
|
||||
aDefault.start();
|
||||
return aDefault;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user