Elasticsearch Rest Client抛出java.lang.ArrayStoreException

时间:2018-04-18 08:33:39

标签: java elasticsearch rest-client

我正在使用Elasticsearch Rest Client v5.5.3在Java应用程序中执行Elasticsearch查询。它总是抛出java.lang.ArrayStoreException。首先,我怀疑查询执行的频率,因为应用程序密集地执行查询,但它首先抛出异常查询。其次,我更新了一些依赖项,例如Elasticsearch其他客户端使用的Apache HttpCore.jar。但无论哪种方式,我无法弄清楚如何解决它仍然抛出异常。堆栈跟踪如下;

java.lang.ArrayStoreException: org.apache.http.impl.cookie.RFC2965VersionAttributeHandler
    at org.apache.http.impl.cookie.DefaultCookieSpecProvider.create(DefaultCookieSpecProvider.java:92) ~[httpclient-4.5.2.jar:4.5.2]
    at org.apache.http.client.protocol.RequestAddCookies.process(RequestAddCookies.java:152) ~[flux-core-1.1.0.jar:1.1.0]
    at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:132) ~[flux-core-1.1.0.jar:1.1.0]
    at org.apache.http.impl.nio.client.MainClientExec.prepareRequest(MainClientExec.java:520) ~[httpasyncclient-4.1.2.jar:4.1.2]
    at org.apache.http.impl.nio.client.MainClientExec.prepare(MainClientExec.java:146) ~[httpasyncclient-4.1.2.jar:4.1.2]
    at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.start(DefaultClientExchangeHandlerImpl.java:124) ~[httpasyncclient-4.1.2.jar:4.1.2]
    at org.apache.http.impl.nio.client.InternalHttpAsyncClient.execute(InternalHttpAsyncClient.java:141) ~[httpasyncclient-4.1.2.jar:4.1.2]
    at org.elasticsearch.client.RestClient.performRequestAsync(RestClient.java:343) ~[rest-5.5.3.jar:5.5.3]
    at org.elasticsearch.client.RestClient.performRequestAsync(RestClient.java:325) ~[rest-5.5.3.jar:5.5.3]
    at org.elasticsearch.client.RestClient.performRequest(RestClient.java:218) ~[rest-5.5.3.jar:5.5.3]
    at org.elasticsearch.client.RestClient.performRequest(RestClient.java:191) ~[rest-5.5.3.jar:5.5.3]
    at com.nova.stats.platform.are.batch.rule.ElasticsearchQueryExecutor.execute(ElasticsearchQueryExecutor.java:36) [classes/:?]

该方法执行查询;

public String execute(String query, QueryParameters parameters) throws IOException {
        String index = (String) parameters.getParameter(GlobalConfigurations.ElasticsearchConfigurations.ConfigurationFields.INDEX);
        String type = (String) parameters.getParameter(GlobalConfigurations.ElasticsearchConfigurations.ConfigurationFields.TYPE);

        RestClient restClient = ElasticsearchRestClient.getClient();
        HttpEntity entity;
        Response response = null;
        try {
            entity = new NStringEntity(query);
            response = restClient.performRequest("GET", "/" + index + "/" + type + "/_search",
                    Collections.singletonMap("pretty", "true"), entity);
        } catch (Exception e) {
            logger.error("Could not perform request, query: " + query, e);
        }

        String responseStr = responseStr = EntityUtils.toString(response.getEntity());


        return responseStr;
    }

1 个答案:

答案 0 :(得分:3)

问题描述

由于冲突httpclient-4.5.2flux-core-1.1.0,问题的根本原因是“JAR Hell”。我不知道为什么但是flux-core-1.1.0 jar包含完整的httpclient代码库版本4.3.3,它与4.5.2不兼容。

什么是ArrayStoreException,Javadoc引用:

  

public class ArrayStoreException extends RuntimeException Thrown to   表示已尝试存储错误类型的   将对象转换为对象数组。例如,以下代码   生成ArrayStoreException:          Object x [] = new String [3];          x [0] = new Integer(0);

在版本httpclient-4.3.3 RFC2965VersionAttributeHandler看起来像

@Immutable
public class RFC2965VersionAttributeHandler implements CookieAttributeHandler {

在版本中,httpclient-4.5.2 RFC2965VersionAttributeHandler看起来像

@Immutable
public class RFC2965VersionAttributeHandler implements CommonCookieAttributeHandler {

DefaultCookieSpec中的问题试图从4.3.3中调用带有RFC2965Spec的版本4.5.2的RFC2965VersionAttributeHandler的构造函数,该函数未实现CommonCookieAttributeHandler:< / p>

RFC2965Spec(final boolean oneHeader,
            final CommonCookieAttributeHandler... handlers) {
    super(oneHeader, handlers);
}

<强>解决方案

“JAR Hell”最可能的原因 - 你的pom.xml(或gradle)对httpclient-4.5.2具有依赖性。您应该将其从依赖项中删除或降级到4.3.3版本。此外,您的pom.xml可能依赖于httpasyncclient-4.1.2 - 在这种情况下,您还可以将其从依赖项中删除或降级为4.0.x版本。值得一提的是,您的项目可能具有httpclienthttpasyncclient的传递依赖性 - 在这种情况下,您需要找到并修复它。