详细例外是:
java.net.SocketTimeoutException:超时时间 okio.Okio $ 4.newTimeoutException(Okio.java:230)at okio.AsyncTimeout.exit(AsyncTimeout.java:285)at okio.AsyncTimeout $ 2.read(AsyncTimeout.java:241)at okio.RealBufferedSource.indexOf(RealBufferedSource.java:345)at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:217) 在 okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:212) 在 okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189) 在 okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) 在 okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) 在 okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) 在 okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) 在 okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) 在 okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200) 在okhttp3.RealCall.execute(RealCall.java:77)at com.a.service.settlement.conllection.synchronizer.DockerCSynchronizer.queryAndSaveSourceByURL(DockerCSynchronizer.java:127) 在 com.a.service.settlement.conllection.synchronizer.DockerCSynchronizer.access $ 000(DockerCSynchronizer.java:30) 在 com.a.service.settlement.conllection.synchronizer.DockerCSynchronizer $ DockerSyncTask.run(DockerCSynchronizer.java:222) 在 java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511) 在java.util.concurrent.FutureTask.run(FutureTask.java:266)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617) 在java.lang.Thread.run(Thread.java:745)引起: java.net.SocketException:Socket关闭了 java.net.SocketInputStream.read(SocketInputStream.java:203)at java.net.SocketInputStream.read(SocketInputStream.java:141)at okio.Okio $ 2.read(Okio.java:139)at okio.AsyncTimeout $ 2.read(AsyncTimeout.java:237)... 27更多
这是我的代码:
public class CollectionSource{
private OkHttpClient httpClient = new OkHttpClient();
public boolean collectSourceData() {
ExecutorService ex = Executors.newFixedThreadPool(10);
logger.info("docker source is starting!");
for (int i =1; i<=10; i++) {
String url = dockerSourceUrl + "2018-01-01"+ "?pageNum=" + i
+"&pageSize=100";
DockerSyncTask dst = new DockerSyncTask(url);
ex.submit(dst);
}
ex.shutdown();
try {
ex.awaitTermination(1L, TimeUnit.DAYS);
} catch (InterruptedException e) {
logger.error("awaitTermination error", e);
return false;
}
logger.info("docker data source synchronized over!");
return true;
}
}
这是任务,内部类:
class DockerSyncTask implements Runnable {
private String url;
DockerSyncTask(String url) {
this.url = url;
}
public void run() {
Request request = new
Request.Builder().url(url).get().build();
try {
Response resp = httpClient.newCall(request).execute();
if (!resp.isSuccessful()) {
logger.warn("QuerySourceDataTask error with message:
{}", resp.message());
}
String response = resp.body().string();
if (response != null && !response.equalsIgnoreCase("")) {
logger.debug("{} return:{}", url, response);
//保存到DB
savePageData(response);
} else {
logger.warn("QuerySourceDataTask return null with url:
{}!", url);
}
} catch (IOException e) {
logger.error("QuerySourceDataTask error, url is [{}]",
url, e);
}
}
}
当没有使用ExecutorService时(只有一个主线程),没有异常,有什么建议吗?
使用JDK1.8,okhttp3