I want to use solrj to work with my standalone solr server. According to the latest document, I write 2 test case:
The first one is EmbeddedSolrServer, so I do not need to start my standalone solr server, I can run the junit tests.
The pom.xml are as follow:
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-core</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.3</version>
</dependency>
private static final String DEFAULT_URL = "http://localhost:8983/solr/";
@Before
public void init() {
server = new HttpSolrServer(DEFAULT_URL);
server.setSoTimeout(1000); // socket read timeout
server.setConnectionTimeout(100);
server.setDefaultMaxConnectionsPerHost(100);
server.setMaxTotalConnections(100);
server.setFollowRedirects(false); // defaults to false
// allowCompression defaults to false.
// Server side must support gzip or deflate for this to have any effect.
server.setAllowCompression(true);
server.setMaxRetries(1); // defaults to 0. > 1 not recommended.
public class SolrServerFactory implements FactoryBean<SolrServer>,
InitializingBean {
protected final Log log = LogFactory.getLog(getClass());
private final String DEFAULT_SERVER_URL = "http://localhost:8983/solr/";
private final String DEFAULT_SOLR_HOME = "D:\\book\\solr\\apache-solr-3.6.0\\example\\solr";
private final String DEFAULT_SOLR_SERVER_CLASS_NAME = "org.apache.solr.client.solrj.embedded.EmbeddedSolrServer";
private String serverURL;
private String solrHome;
private String solrServerClassName;
private SolrServer solrServer = null;
public SolrServer getObject() throws Exception {
return solrServer;
}
public Class<SolrServer> getObjectType() {
return SolrServer.class;
}
public boolean isSingleton() {
return true;
}
public void afterPropertiesSet() throws Exception {
if ("org.apache.solr.client.solrj.embedded.EmbeddedSolrServer"
.equalsIgnoreCase(this.getSolrServerClassName())) {
System.setProperty("solr.solr.home", this.getSolrHome());
CoreContainer.Initializer initializer = new CoreContainer.Initializer();
CoreContainer coreContainer = null;
try {
coreContainer = initializer.initialize();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
EmbeddedSolrServer server = new EmbeddedSolrServer(coreContainer, "");
solrServer = server;
} else if ("org.apache.solr.client.solrj.impl.HttpSolrServer"
.equalsIgnoreCase(this.getSolrServerClassName())) {
HttpSolrServer server = new HttpSolrServer(this.getServerURL());
server.setSoTimeout(1000); // socket read timeout
server.setConnectionTimeout(100);
server.setDefaultMaxConnectionsPerHost(100);
server.setMaxTotalConnections(100);
server.setFollowRedirects(false); // defaults to false
// allowCompression defaults to false.
// Server side must support gzip or deflate for this to have any
// effect.
server.setAllowCompression(true);
server.setMaxRetries(1); // defaults to 0. > 1 not recommended.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/test/resources/test-context.xml" })
public class SolrServerFactoryTest {