searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

alogic-rpc连接池自定义连接超时跟响应超时优化

2024-05-22 05:33:06
2
0

github.com/anylogic/alogic/blob/master/alogic-rpc/src/main/java/com/alogic/remote/xscript/ExecuteRequestDirectly.java

protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
Request req = ctx.getObject(pid);
if (req == null){
throw new BaseException("core.e1001","It must be in a remote-request context,check your script.");
}
 
Response response = null;
try {
String p = ctx.transform(path);
if (StringUtils.isEmpty(p)){
throw new BaseException("core.e1003","The url path to call is null.");
}
response = req.execute(p);
ctx.setObject(cid, response);
 
super.onExecute(root, current, ctx, watcher);
}finally{
ctx.removeObject(cid);
}
}

直接取连接使用

github.com/anylogic/alogic/blob/master/alogic-rpc/src/main/java/com/alogic/remote/httpclient/customizer/Default.java创建连接默认的超时配置

如下代码所示,获取的链接的默认超时时间是

@Override
public HttpClientBuilder customizeHttpClient(HttpClientBuilder builder,Properties p) {
if (builder != null){
final long keepAliveTime = PropertiesConstants.getInt(p,"rpc.http.keepAlive.ttl",60000);
ConnectionKeepAliveStrategy kaStrategy = new DefaultConnectionKeepAliveStrategy() {
    @Override
    public long getKeepAliveDuration(final HttpResponse response, final HttpContext context) {
    long keepAlive = super.getKeepAliveDuration(response, context);
if (keepAlive == -1) {
keepAlive = keepAliveTime;
}
return keepAlive;
    }
};
 
List<Header> headers = new ArrayList<Header>();
boolean keepAliveEnable = PropertiesConstants.getBoolean(p,"rpc.http.keepAlive.enable", true);
if (keepAliveEnable){
headers.add(new BasicHeader("Connection",HTTP.CONN_KEEP_ALIVE));
}else{
headers.add(new BasicHeader("Connection",HTTP.CONN_CLOSE));
}
 
int maxConnPerRoute = PropertiesConstants.getInt(p, "rpc.http.maxConnPerHost", 200);
int maxConn = PropertiesConstants.getInt(p,"rpc.http.maxConn",2000);
int ttlOfConn = PropertiesConstants.getInt(p,"rpc.http.keepalive.ttl",60000);
 
builder.useSystemProperties().
setMaxConnPerRoute(maxConnPerRoute).
setConnectionReuseStrategy(DefaultClientConnectionReuseStrategy.INSTANCE).
setMaxConnTotal(maxConn).
setDefaultHeaders(headers).
setConnectionTimeToLive(ttlOfConn,TimeUnit.MILLISECONDS).
setKeepAliveStrategy(kaStrategy);
}
 
return builder;
}
 
@Override
public RequestConfig.Builder customizeRequestConfig(Builder builder,Properties p) {
if (builder != null){
int timeOut = PropertiesConstants.getInt(p, "rpc.http.timeout", 10000);
builder.setConnectionRequestTimeout(PropertiesConstants.getInt(p, "rpc.http.timeout.request", timeOut))
                .setConnectTimeout(PropertiesConstants.getInt(p, "rpc.http.timeout.conn", timeOut))
                .setSocketTimeout(PropertiesConstants.getInt(p, "rpc.http.timeout.socket", timeOut));
}
 
return builder;
}
整个连接池共用一个配置,连接超时跟等待时间超时都是一个配置项。无法根据不同接口做定制。
 
为了实现每个连接的超时时间自定义,我们做如下改造:
github.com/anylogic/alogic/blob/master/alogic-rpc/src/main/java/com/alogic/remote/httpclient/HttpClientRequest.java增加获取配置及重新设置配置的方法
public RequestConfig getRequestConfig(){
return httpRequest.getConfig();
}
public void setRequestConfig(RequestConfig requestConfig){
httpRequest.setConfig(requestConfig);
}
重写github.com/anylogic/alogic/blob/master/alogic-rpc/src/main/java/com/alogic/remote/xscript/ExecuteRequestDirectly.java的实现

@Override
protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
Request req = ctx.getObject(pid);
if (req == null) {
throw new BaseException("core.e1001", "It must be in a remote-request context,check your script.");
}
Response response = null;
try {
String p = ctx.transform(path);
if (StringUtils.isEmpty(p)) {
throw new BaseException("core.e1003", "The url path to call is null.");
}
if (req instanceof HttpClientRequest){
String connectionRequestTimeoutstr=10000;
String connectTimeoutstr=20000;
String socketTimeoutstr=30000;
if(!StringUtils.equals(connectionRequestTimeoutstr,"-1") || !StringUtils.equals(connectTimeoutstr,"-1") || !StringUtils.equals(socketTimeoutstr,"-1")){

HttpClientRequest httpClientRequest=(HttpClientRequest)req;
RequestConfig requestConfig = httpClientRequest.getRequestConfig();
int connectionRequestTimeout = !StringUtils.equals(connectionRequestTimeoutstr,"-1")?Integer.parseInt(connectionRequestTimeoutstr):requestConfig.getConnectionRequestTimeout();
int connectionTimeout = !StringUtils.equals(connectTimeoutstr,"-1")?Integer.parseInt(connectTimeoutstr):requestConfig.getConnectTimeout();
int socketTimeout = !StringUtils.equals(socketTimeoutstr,"-1")?Integer.parseInt(socketTimeoutstr):requestConfig.getSocketTimeout();
//重写超时时间
RequestConfig config = RequestConfig.copy(requestConfig)
.setConnectionRequestTimeout(connectionRequestTimeout)
.setConnectTimeout(connectionTimeout)
.setSocketTimeout(socketTimeout).build();
httpClientRequest.setRequestConfig(config);
}
}
response = req.execute(p, id);
ctx.setObject(cid, response);

super.onExecute(root, current, ctx, watcher);
} finally {
ctx.removeObject(cid);

}
}
0条评论
作者已关闭评论
陈青龙
7文章数
0粉丝数
陈青龙
7 文章 | 0 粉丝
原创

alogic-rpc连接池自定义连接超时跟响应超时优化

2024-05-22 05:33:06
2
0

github.com/anylogic/alogic/blob/master/alogic-rpc/src/main/java/com/alogic/remote/xscript/ExecuteRequestDirectly.java

protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
Request req = ctx.getObject(pid);
if (req == null){
throw new BaseException("core.e1001","It must be in a remote-request context,check your script.");
}
 
Response response = null;
try {
String p = ctx.transform(path);
if (StringUtils.isEmpty(p)){
throw new BaseException("core.e1003","The url path to call is null.");
}
response = req.execute(p);
ctx.setObject(cid, response);
 
super.onExecute(root, current, ctx, watcher);
}finally{
ctx.removeObject(cid);
}
}

直接取连接使用

github.com/anylogic/alogic/blob/master/alogic-rpc/src/main/java/com/alogic/remote/httpclient/customizer/Default.java创建连接默认的超时配置

如下代码所示,获取的链接的默认超时时间是

@Override
public HttpClientBuilder customizeHttpClient(HttpClientBuilder builder,Properties p) {
if (builder != null){
final long keepAliveTime = PropertiesConstants.getInt(p,"rpc.http.keepAlive.ttl",60000);
ConnectionKeepAliveStrategy kaStrategy = new DefaultConnectionKeepAliveStrategy() {
    @Override
    public long getKeepAliveDuration(final HttpResponse response, final HttpContext context) {
    long keepAlive = super.getKeepAliveDuration(response, context);
if (keepAlive == -1) {
keepAlive = keepAliveTime;
}
return keepAlive;
    }
};
 
List<Header> headers = new ArrayList<Header>();
boolean keepAliveEnable = PropertiesConstants.getBoolean(p,"rpc.http.keepAlive.enable", true);
if (keepAliveEnable){
headers.add(new BasicHeader("Connection",HTTP.CONN_KEEP_ALIVE));
}else{
headers.add(new BasicHeader("Connection",HTTP.CONN_CLOSE));
}
 
int maxConnPerRoute = PropertiesConstants.getInt(p, "rpc.http.maxConnPerHost", 200);
int maxConn = PropertiesConstants.getInt(p,"rpc.http.maxConn",2000);
int ttlOfConn = PropertiesConstants.getInt(p,"rpc.http.keepalive.ttl",60000);
 
builder.useSystemProperties().
setMaxConnPerRoute(maxConnPerRoute).
setConnectionReuseStrategy(DefaultClientConnectionReuseStrategy.INSTANCE).
setMaxConnTotal(maxConn).
setDefaultHeaders(headers).
setConnectionTimeToLive(ttlOfConn,TimeUnit.MILLISECONDS).
setKeepAliveStrategy(kaStrategy);
}
 
return builder;
}
 
@Override
public RequestConfig.Builder customizeRequestConfig(Builder builder,Properties p) {
if (builder != null){
int timeOut = PropertiesConstants.getInt(p, "rpc.http.timeout", 10000);
builder.setConnectionRequestTimeout(PropertiesConstants.getInt(p, "rpc.http.timeout.request", timeOut))
                .setConnectTimeout(PropertiesConstants.getInt(p, "rpc.http.timeout.conn", timeOut))
                .setSocketTimeout(PropertiesConstants.getInt(p, "rpc.http.timeout.socket", timeOut));
}
 
return builder;
}
整个连接池共用一个配置,连接超时跟等待时间超时都是一个配置项。无法根据不同接口做定制。
 
为了实现每个连接的超时时间自定义,我们做如下改造:
github.com/anylogic/alogic/blob/master/alogic-rpc/src/main/java/com/alogic/remote/httpclient/HttpClientRequest.java增加获取配置及重新设置配置的方法
public RequestConfig getRequestConfig(){
return httpRequest.getConfig();
}
public void setRequestConfig(RequestConfig requestConfig){
httpRequest.setConfig(requestConfig);
}
重写github.com/anylogic/alogic/blob/master/alogic-rpc/src/main/java/com/alogic/remote/xscript/ExecuteRequestDirectly.java的实现

@Override
protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
Request req = ctx.getObject(pid);
if (req == null) {
throw new BaseException("core.e1001", "It must be in a remote-request context,check your script.");
}
Response response = null;
try {
String p = ctx.transform(path);
if (StringUtils.isEmpty(p)) {
throw new BaseException("core.e1003", "The url path to call is null.");
}
if (req instanceof HttpClientRequest){
String connectionRequestTimeoutstr=10000;
String connectTimeoutstr=20000;
String socketTimeoutstr=30000;
if(!StringUtils.equals(connectionRequestTimeoutstr,"-1") || !StringUtils.equals(connectTimeoutstr,"-1") || !StringUtils.equals(socketTimeoutstr,"-1")){

HttpClientRequest httpClientRequest=(HttpClientRequest)req;
RequestConfig requestConfig = httpClientRequest.getRequestConfig();
int connectionRequestTimeout = !StringUtils.equals(connectionRequestTimeoutstr,"-1")?Integer.parseInt(connectionRequestTimeoutstr):requestConfig.getConnectionRequestTimeout();
int connectionTimeout = !StringUtils.equals(connectTimeoutstr,"-1")?Integer.parseInt(connectTimeoutstr):requestConfig.getConnectTimeout();
int socketTimeout = !StringUtils.equals(socketTimeoutstr,"-1")?Integer.parseInt(socketTimeoutstr):requestConfig.getSocketTimeout();
//重写超时时间
RequestConfig config = RequestConfig.copy(requestConfig)
.setConnectionRequestTimeout(connectionRequestTimeout)
.setConnectTimeout(connectionTimeout)
.setSocketTimeout(socketTimeout).build();
httpClientRequest.setRequestConfig(config);
}
}
response = req.execute(p, id);
ctx.setObject(cid, response);

super.onExecute(root, current, ctx, watcher);
} finally {
ctx.removeObject(cid);

}
}
文章来自个人专栏
陈青龙个人
7 文章 | 1 订阅
0条评论
作者已关闭评论
作者已关闭评论
0
0