From 98186e21c88a08526b979676009d46ec996816d9 Mon Sep 17 00:00:00 2001 From: zhangzhiqiang Date: Mon, 6 Feb 2023 16:01:53 +0800 Subject: [PATCH] =?UTF-8?q?add:http=E5=BC=82=E6=AD=A5=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lms/nladmin-system/pom.xml | 6 ++ .../org/nl/common/utils/AsyncHttpRequest.java | 65 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 lms/nladmin-system/src/main/java/org/nl/common/utils/AsyncHttpRequest.java diff --git a/lms/nladmin-system/pom.xml b/lms/nladmin-system/pom.xml index c9f0f2918..3710cca9a 100644 --- a/lms/nladmin-system/pom.xml +++ b/lms/nladmin-system/pom.xml @@ -369,6 +369,12 @@ jsch 0.1.55 + + + org.apache.httpcomponents + httpasyncclient + 4.1.4 + diff --git a/lms/nladmin-system/src/main/java/org/nl/common/utils/AsyncHttpRequest.java b/lms/nladmin-system/src/main/java/org/nl/common/utils/AsyncHttpRequest.java new file mode 100644 index 000000000..7de5debe6 --- /dev/null +++ b/lms/nladmin-system/src/main/java/org/nl/common/utils/AsyncHttpRequest.java @@ -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 execute(FutureCallback callback){ + CloseableHttpAsyncClient client = SpringContextHolder.getBean(CloseableHttpAsyncClient.class); + Future execute = client.execute(httpbase, callback); + return execute; + }; +} +@Component +class HttpAsyncClientInit{ + + @Bean + public CloseableHttpAsyncClient closeableHttpAsyncClient(){ + CloseableHttpAsyncClient aDefault = HttpAsyncClients.createDefault(); + aDefault.start(); + return aDefault; + } +}