SolrJ:我应该为每个请求创建一个新的 SolrClient 还是重用它?
SolrJ: Should I create a new SolrClient for each request or reuse it?
我正在编写用于使用 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 警告关闭客户端。
请建议采用哪种方法。
这是处理连接的传统用例。您可以使用工厂 class,也可以是单例,并保持那里的连接。
另外推荐使用负载均衡器solrserver片
我正在编写用于使用 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 警告关闭客户端。
请建议采用哪种方法。
这是处理连接的传统用例。您可以使用工厂 class,也可以是单例,并保持那里的连接。
另外推荐使用负载均衡器solrserver片