Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SolrJ:我应该为每个请求创建一个新的SolrClient还是重用它?_Solr_Solrj - Fatal编程技术网

SolrJ:我应该为每个请求创建一个新的SolrClient还是重用它?

SolrJ:我应该为每个请求创建一个新的SolrClient还是重用它?,solr,solrj,Solr,Solrj,我正在编写代码,以便使用SolrJ客户端对Solr进行索引和检索 这是我的POC import java.io.IOException; import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.beans.Field; import org.apache.solr.cli

我正在编写代码,以便使用SolrJ客户端对Solr进行索引和检索

这是我的POC

import java.io.IOException;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.beans.Field;
import org.apache.solr.client.solrj.impl.HttpSolrClient;

public class TestSolrService {

    public static void main(String[] args) throws IOException,
            SolrServerException {
        new TestSolrService().test();
    }

    public void test() throws IOException, SolrServerException {

        SolrClient client = new HttpSolrClient("http://localhost:8983/solr/");

        Test test = new Test();
        test.setId("3");
        test.name="TEST";
        Child c = new Child();
        c.flag = 1;
        c.id = "1";
        test.setChild(c);
        client.addBean("test", test, 10);

        client.close();

    }

    public class Child {
        @Field
        public String id;
        @Field("flag_l")
        public int flag;
    }

    public class Test {

        @Field
        private String id;

        @Field("name")
        private String name;

        @Field(child = true)
        private Child child;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public Child getChild() {
            return child;
        }

        public void setChild(Child child) {
            this.child = child;
        }

    }
}
我不确定是应该创建一个新的客户端并执行操作,还是保留一个客户端并在有新请求时使用它。 在编写上述代码时,eclipse警告关闭客户端


请建议使用哪种方法。

这是处理连接的传统用例。您可以使用工厂类,也可以是单例,并在那里维护连接

此外,建议使用负载平衡器solrserver部件