-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpJob.java
141 lines (121 loc) · 4.89 KB
/
HttpJob.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.HttpConnectionParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import test.HttpException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
*
* HttpClient的恢复策略可以自定义(通过实现接口HttpMethodRetryHandler来实现)
* Created with IntelliJ IDEA.
* User: WuYifei
* Date: 2017/7/21
* Time: 16:04
*/
public abstract class HttpJob<T> extends CurrentThreadJob<T> {
private static final Logger logger = LoggerFactory.getLogger(HttpJob.class);
protected static HttpClient httpClient;
static {
httpClient = createHttpClient();
}
protected HttpRequestBase httpRequest;
protected HttpResponse httpResponse;
protected long startTime = -1;
protected T result;
private static HttpClient createHttpClient() {
if (httpClient != null) {
return httpClient;
}
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
// 设置http https支持
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", 80, PlainSocketFactory
.getSocketFactory()));
schReg.register(new Scheme("https", 443, ssf));
httpClient = new DefaultHttpClient(new PoolingClientConnectionManager(schReg));
} catch (Exception ex) {
logger.error("create https client support fail:" + ex.getMessage(), ex);
}
return httpClient;
}
public static void setMaxPerRoute(HttpHost httpHost, int count) {
PoolingClientConnectionManager connectionManager = (PoolingClientConnectionManager) httpClient.getConnectionManager();
connectionManager.setMaxPerRoute(new HttpRoute(httpHost), count);
}
public static void setDefaultMaxPerRoute(int count) {
PoolingClientConnectionManager connectionManager = (PoolingClientConnectionManager) httpClient.getConnectionManager();
connectionManager.setDefaultMaxPerRoute(count);
}
public static void setMaxTotal(int count) {
PoolingClientConnectionManager connectionManager = (PoolingClientConnectionManager) httpClient.getConnectionManager();
connectionManager.setMaxTotal(count);
}
public static void setMaxBufferSize(int size) {
int defaultSize = 8192;
if (size < defaultSize) {
HttpConnectionParams.setSocketBufferSize(httpClient.getParams(), defaultSize);
} else {
HttpConnectionParams.setSocketBufferSize(httpClient.getParams(), size);
}
}
@Override
public T getResult() {
return result;
}
protected long getTimeout() {
return 120 * 1000;
}
public long startTime() {
return startTime;
}
public void run() throws JobException {
startTime = System.currentTimeMillis();
try {
setHttpRequest();
httpResponse = httpClient.execute(httpRequest);
handler(httpResponse);
} catch (HttpException e) {
throw new JobException(e.getErrorCode(), e.getMessage(), e);
} catch (Exception e) {
throw new JobException(e.getMessage(), e);
} finally {
if (httpRequest != null) {
httpRequest.releaseConnection();
}
}
}
protected abstract void setHttpRequest() throws Exception;
protected abstract void handler(HttpResponse httpResponse) throws Exception;
public void about(Thread thread) throws JobException {
httpRequest.abort();
}
public boolean checkTimeout() {
return System.currentTimeMillis() > startTime() + getTimeout();
}
}