HttpSimpleClient优化
This commit is contained in:
@@ -27,7 +27,7 @@ import org.redkale.util.*;
|
|||||||
@ResourceType(AsyncGroup.class)
|
@ResourceType(AsyncGroup.class)
|
||||||
public class AsyncIOGroup extends AsyncGroup {
|
public class AsyncIOGroup extends AsyncGroup {
|
||||||
|
|
||||||
private boolean started;
|
private final AtomicBoolean started = new AtomicBoolean();
|
||||||
|
|
||||||
private boolean skipClose;
|
private boolean skipClose;
|
||||||
|
|
||||||
@@ -108,20 +108,18 @@ public class AsyncIOGroup extends AsyncGroup {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AsyncGroup start() {
|
public AsyncGroup start() {
|
||||||
if (started) {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
if (closed.get()) {
|
if (closed.get()) {
|
||||||
throw new RedkaleException("group is closed");
|
throw new RedkaleException("group is closed");
|
||||||
}
|
}
|
||||||
for (int i = 0; i < this.ioReadThreads.length; i++) {
|
if (started.compareAndSet(false, true)) {
|
||||||
this.ioReadThreads[i].start();
|
for (int i = 0; i < this.ioReadThreads.length; i++) {
|
||||||
if (this.ioWriteThreads[i] != this.ioReadThreads[i]) {
|
this.ioReadThreads[i].start();
|
||||||
this.ioWriteThreads[i].start();
|
if (this.ioWriteThreads[i] != this.ioReadThreads[i]) {
|
||||||
|
this.ioWriteThreads[i].start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
//connectThread用时才初始化
|
||||||
}
|
}
|
||||||
//connectThread用时才初始化
|
|
||||||
started = true;
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.Predicate;
|
||||||
import org.redkale.convert.TextConvert;
|
import org.redkale.convert.TextConvert;
|
||||||
import org.redkale.convert.json.JsonConvert;
|
import org.redkale.convert.json.JsonConvert;
|
||||||
import org.redkale.util.RedkaleException;
|
import org.redkale.util.RedkaleException;
|
||||||
@@ -103,14 +104,21 @@ public class HttpHeaders implements RestHeaders, Serializable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void forEach(BiConsumer<String, String> consumer) {
|
public void forEach(BiConsumer<String, String> consumer) {
|
||||||
|
forEach((Predicate) null, consumer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEach(Predicate<String> filter, BiConsumer<String, String> consumer) {
|
||||||
if (map != null) {
|
if (map != null) {
|
||||||
map.forEach((k, v) -> {
|
map.forEach((k, v) -> {
|
||||||
if (v instanceof Collection) {
|
if (filter == null || filter.test(k)) {
|
||||||
for (Object item : (Collection) v) {
|
if (v instanceof Collection) {
|
||||||
consumer.accept(k, item == null ? null : item.toString());
|
for (Object item : (Collection) v) {
|
||||||
|
consumer.accept(k, item == null ? null : item.toString());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
consumer.accept(k, v == null ? null : v.toString());
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
consumer.accept(k, v == null ? null : v.toString());
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -246,18 +246,17 @@ public class HttpSimpleClient extends Client<HttpSimpleConnection, HttpSimpleReq
|
|||||||
array.put((method.toUpperCase() + " " + (urlpos > 0 ? url.substring(urlpos) : "/") + " HTTP/1.1\r\n").getBytes(StandardCharsets.UTF_8));
|
array.put((method.toUpperCase() + " " + (urlpos > 0 ? url.substring(urlpos) : "/") + " HTTP/1.1\r\n").getBytes(StandardCharsets.UTF_8));
|
||||||
array.put(("Host: " + uri.getHost() + "\r\n").getBytes(StandardCharsets.UTF_8));
|
array.put(("Host: " + uri.getHost() + "\r\n").getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
||||||
array.put(("Content-Length: " + (body == null ? 0 : body.length) + "\r\n").getBytes(StandardCharsets.UTF_8));
|
array.put(HttpSimpleRequest.contentLengthBytes(body));
|
||||||
if (headers == null || !headers.containsIgnoreCase("User-Agent")) {
|
if (headers == null || !headers.containsIgnoreCase("User-Agent")) {
|
||||||
array.put(header_bytes_useragent);
|
array.put(header_bytes_useragent);
|
||||||
}
|
}
|
||||||
if (headers == null || !headers.containsIgnoreCase("Connection")) {
|
array.put(header_bytes_connclose);
|
||||||
array.put(header_bytes_connclose);
|
|
||||||
}
|
|
||||||
if (headers == null || !headers.containsIgnoreCase(Rest.REST_HEADER_TRACEID)) {
|
if (headers == null || !headers.containsIgnoreCase(Rest.REST_HEADER_TRACEID)) {
|
||||||
array.put((Rest.REST_HEADER_TRACEID + ": " + traceid + "\r\n").getBytes(StandardCharsets.UTF_8));
|
array.put((Rest.REST_HEADER_TRACEID + ": " + traceid + "\r\n").getBytes(StandardCharsets.UTF_8));
|
||||||
}
|
}
|
||||||
if (headers != null) {
|
if (headers != null) {
|
||||||
headers.forEach((k, v) -> array.put((k + ": " + v + "\r\n").getBytes(StandardCharsets.UTF_8)));
|
headers.forEach(k -> !k.equalsIgnoreCase("Connection") && !k.equalsIgnoreCase("Content-Length"),
|
||||||
|
(k, v) -> array.put((k + ": " + v + "\r\n").getBytes(StandardCharsets.UTF_8)));
|
||||||
}
|
}
|
||||||
array.put((byte) '\r', (byte) '\n');
|
array.put((byte) '\r', (byte) '\n');
|
||||||
if (body != null) {
|
if (body != null) {
|
||||||
|
|||||||
@@ -147,7 +147,8 @@ public class HttpSimpleRequest extends ClientRequest implements java.io.Serializ
|
|||||||
array.put(("Content-Type: " + contentType0 + "\r\n").getBytes(StandardCharsets.UTF_8));
|
array.put(("Content-Type: " + contentType0 + "\r\n").getBytes(StandardCharsets.UTF_8));
|
||||||
array.put(contentLengthBytes(clientBody));
|
array.put(contentLengthBytes(clientBody));
|
||||||
if (headers != null) {
|
if (headers != null) {
|
||||||
headers.forEach((k, v) -> array.put((k + ": " + v + "\r\n").getBytes(StandardCharsets.UTF_8)));
|
headers.forEach(k -> !k.equalsIgnoreCase("Content-Type") && !k.equalsIgnoreCase("Content-Length"),
|
||||||
|
(k, v) -> array.put((k + ": " + v + "\r\n").getBytes(StandardCharsets.UTF_8)));
|
||||||
}
|
}
|
||||||
array.put((byte) '\r', (byte) '\n');
|
array.put((byte) '\r', (byte) '\n');
|
||||||
//写body
|
//写body
|
||||||
@@ -160,7 +161,7 @@ public class HttpSimpleRequest extends ClientRequest implements java.io.Serializ
|
|||||||
return headers != null && headers.containsIgnoreCase(name);
|
return headers != null && headers.containsIgnoreCase(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected byte[] contentLengthBytes(byte[] clientBody) {
|
protected static byte[] contentLengthBytes(byte[] clientBody) {
|
||||||
int len = clientBody == null ? 0 : clientBody.length;
|
int len = clientBody == null ? 0 : clientBody.length;
|
||||||
if (len < contentLengthArray.length) {
|
if (len < contentLengthArray.length) {
|
||||||
return contentLengthArray[len];
|
return contentLengthArray[len];
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import java.io.Serializable;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用于RestService类的方法的参数获取HttpHeaders
|
* 用于RestService类的方法的参数获取HttpHeaders
|
||||||
@@ -30,6 +31,8 @@ public interface RestHeaders {
|
|||||||
|
|
||||||
public void forEach(BiConsumer<String, String> consumer);
|
public void forEach(BiConsumer<String, String> consumer);
|
||||||
|
|
||||||
|
public void forEach(Predicate<String> filter, BiConsumer<String, String> consumer);
|
||||||
|
|
||||||
public String[] names();
|
public String[] names();
|
||||||
|
|
||||||
public boolean contains(String name);
|
public boolean contains(String name);
|
||||||
|
|||||||
Reference in New Issue
Block a user