3

java.net.SocketTimeoutException在jira搜索时得到。如何增加超时?

代码:

JiraRestClientFactory restClientFactory = new AsynchronousJiraRestClientFactory();
SearchResult results = null;

try {
    URI uri = new URI(jira_url);
    restClient = restClientFactory.createWithBasicHttpAuthentication(uri, jira_username, jira_password);
    final SearchRestClient searchClient = restClient.getSearchClient();
    String jql = searchClient.getFilter(jira_filterid).get().getJql();
    // setting max result to 1000 and start with 0
    results = searchClient.searchJql(jql, 500, 0).claim();
    System.out.println("Took: " + stopWatch.toString() + " to find " + results.getTotal() + " case in jira filter with id " + jira_filterid);
} catch (Exception e) {
    e.printStackTrace();
    System.exit(1);
}
return results;

搜索不应该花这么长时间,我认为是在进行声明()时。

例外:

java.lang.RuntimeException: java.net.SocketTimeoutException
    at com.google.common.base.Throwables.propagate(Throwables.java:160)
    at com.atlassian.httpclient.apache.httpcomponents.DefaultHttpClient$3.apply(DefaultHttpClient.java:256)
    at com.atlassian.httpclient.apache.httpcomponents.DefaultHttpClient$3.apply(DefaultHttpClient.java:249)
    at com.atlassian.util.concurrent.Promises$Of$2.apply(Promises.java:276)
    at com.atlassian.util.concurrent.Promises$Of$2.apply(Promises.java:272)
    at com.atlassian.util.concurrent.Promises$2.onFailure(Promises.java:167)
    at com.google.common.util.concurrent.Futures$4.run(Futures.java:1172)
    at com.google.common.util.concurrent.MoreExecutors$SameThreadExecutorService.execute(MoreExecutors.java:297)
    at com.google.common.util.concurrent.ExecutionList.executeListener(ExecutionList.java:156)
    at com.google.common.util.concurrent.ExecutionList.execute(ExecutionList.java:145)
    at com.google.common.util.concurrent.AbstractFuture.setException(AbstractFuture.java:202)
    at com.google.common.util.concurrent.SettableFuture.setException(SettableFuture.java:68)
    at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient$1$2.run(SettableFuturePromiseHttpPromiseAsyncClient.java:59)
    at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient$ThreadLocalDelegateRunnable$1.run(SettableFuturePromiseHttpPromiseAsyncClient.java:197)
    at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient.runInContext(SettableFuturePromiseHttpPromiseAsyncClient.java:90)
    at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient$ThreadLocalDelegateRunnable.run(SettableFuturePromiseHttpPromiseAsyncClient.java:192)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:680)
Caused by: java.net.SocketTimeoutException
    at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.timeout(HttpAsyncRequestExecutor.java:279)
    at org.apache.http.impl.nio.client.LoggingAsyncRequestExecutor.timeout(LoggingAsyncRequestExecutor.java:128)
    at org.apache.http.impl.nio.DefaultHttpClientIODispatch.onTimeout(DefaultHttpClientIODispatch.java:136)
    at org.apache.http.impl.nio.DefaultHttpClientIODispatch.onTimeout(DefaultHttpClientIODispatch.java:50)
    at org.apache.http.impl.nio.reactor.AbstractIODispatch.timeout(AbstractIODispatch.java:169)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.sessionTimedOut(BaseIOReactor.java:257)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.timeoutCheck(AbstractIOReactor.java:494)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.validate(BaseIOReactor.java:207)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:284)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:106)
    at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:604)
    ... 1 more
4

3 回答 3

2

我不敢相信我必须如此深入才能改变它。你可以使用反射来实现它

    try (JiraRestClient client = clientFactory.createWithBasicHttpAuthentication(new URI(jira.getUrl()), jira.getUsername(), jira.getPassword())) {
        try {
            Field f1 = Class.forName("com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClient").getDeclaredField("httpClient");
            Field f2 = Class.forName("com.atlassian.jira.rest.client.internal.async.AtlassianHttpClientDecorator").getDeclaredField("httpClient");
            Field f3 = Class.forName("com.atlassian.httpclient.apache.httpcomponents.ApacheAsyncHttpClient").getDeclaredField("httpClient");
            Field f4 = Class.forName("org.apache.http.impl.client.cache.CachingHttpAsyncClient").getDeclaredField("backend");
            Field f5 = Class.forName("org.apache.http.impl.nio.client.InternalHttpAsyncClient").getDeclaredField("defaultConfig");
            Field f6 = Class.forName("org.apache.http.client.config.RequestConfig").getDeclaredField("socketTimeout");
            f1.setAccessible(true);
            f2.setAccessible(true);
            f3.setAccessible(true);
            f4.setAccessible(true);
            f5.setAccessible(true);
            f6.setAccessible(true);
            Object requestConfig = f5.get(f4.get(f3.get(f2.get(f1.get(client)))));
            f6.setInt(requestConfig, 120 * 1000);
            f1.setAccessible(false);
            f2.setAccessible(false);
            f3.setAccessible(false);
            f4.setAccessible(false);
            f5.setAccessible(false);
            f6.setAccessible(false);
        } catch (Exception ignore) {
        }
        // now you can start using it :)
    } catch (URISyntaxException | IOException e) {
        logger.error("invalid jira server address: " + jira.getUrl(), e);
        throw new RuntimeException("can not access jira server");
    }

它将为您购买 120 秒的套接字时间。

于 2017-05-15T05:26:18.333 回答
1

一种可行的解决方法是为每次迭代获取 100 个结果并设置 startAt

results = searchClient.searchJql(jql, 100, 0).claim();
results1 = searchClient.searchJql(jql, 100, 100).claim();
results2 = searchClient.searchJql(jql, 100, 200).claim();

等等。

于 2014-03-05T09:56:22.730 回答
1

免责声明:我使用的是Groovy 编程语言,但语法与 Java 非常相似,因此您应该能够重用代码(提示:在 Groovy 中不需要分号,return 语句是可选的,而不是变量声明我正在使用deffinal仅使用)。

我正在使用以下库版本(gradle 风格):

compile "com.atlassian.jira:jira-rest-java-client-core:4.0.0"
compile "com.atlassian.fugue:fugue:2.2.1"

这里我们有标准的休息客户端定义:

JiraRestClient getJiraRestClient()
{
    // read user specific Jira password settings and build authentification
    final inputFile = new File("${System.getProperty('user.home')}/jiraSettings.json")
    final authInfo = new JsonSlurper().parseText(inputFile.text)

    // setting up the jira client
    def restClient = new AsynchronousJiraRestClientFactory()
        .createWithBasicHttpAuthentication(
            jiraServerUri,
            authInfo.jiraUser.toString(),
            authInfo.jiraPassword.toString())

    restClient
}

我深入研究了createWithBasicHttpAuthentication函数并提取并调整了代码(仅getClientOptions- 我将套接字超时设置为 45 秒,查看HttpClientOptions默认设置):

JiraRestClient getJiraRestClient()
{
    return new AsynchronousJiraRestClient(jiraServerUri, getHttpClient());
}

HttpClientOptions getClientOptions()
{
    def options = new HttpClientOptions();
    options.socketTimeout = 45000L;
    options
}

DisposableHttpClient getHttpClient()
{
    final DefaultHttpClientFactory defaultHttpClientFactory =
        new DefaultHttpClientFactory(new AsynchronousHttpClientFactory.NoOpEventPublisher(),
        new AsynchronousHttpClientFactory.RestClientApplicationProperties(jiraServerUri),
        new ThreadLocalContextManager() {
            @Override
            public Object getThreadLocalContext() {
                return null;
            }

            @Override
            public void setThreadLocalContext(Object context) {}

            @Override
            public void clearThreadLocalContext() {}
        });

    final HttpClient httpClient = defaultHttpClientFactory.create(getClientOptions())

    return new AtlassianHttpClientDecorator(httpClient, getAuthenticationHandler()) {
        @Override
        public void destroy() throws Exception {
            defaultHttpClientFactory.dispose(httpClient);
        }
    }
}

BasicHttpAuthenticationHandler getAuthenticationHandler()
{
    // read user specific Jira password settings and build authentification
    final inputFile = new File("${System.getProperty('user.home')}/jiraSettings.json")
    final authInfo = new JsonSlurper().parseText(inputFile.text)

    return new BasicHttpAuthenticationHandler(
        authInfo.jiraUser.toString(),
        authInfo.jiraPassword.toString())
}

不利的一面是,当我切换到新版本的 jira-rest-java-client 时,我可能会被迫调整此代码,但我确实需要这个,因为即使大量使用分页,超时也很短。

于 2016-07-06T06:07:45.097 回答