solr(2)solrj integration with Spring

solr(2)solrj integration with Spring

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>


package com.sillycat.easyhunter.plugin.solr;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.core.CoreContainer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.SAXException;

public class EmbeddedSolrServerTest {

EmbeddedSolrServer server = null;

@Before
public void init() {
System.setProperty("solr.solr.home",
"D:\\book\\solr\\apache-solr-3.6.0\\example\\solr");
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();
}
server = new EmbeddedSolrServer(coreContainer, "");
}

@After
public void destory() {
server = null;
System.runFinalization();
System.gc();
}

public final void fail(Object o) {
System.out.println(o);
}

@Test
public void server() {
fail(server);
}

@Test
public void query() {
String query = "name:DDR";
SolrParams params = new SolrQuery(query);

try {
QueryResponse response = server.query(params);

SolrDocumentList list = response.getResults();
for (int i = 0; i < list.size(); i++) {
fail(list.get(i));
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}
}

CommonsHttpSolrServer is under deprecation. So in 3.6.0, I change to use HttpSolrServer.

package com.sillycat.easyhunter.plugin.solr;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.SolrParams;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class HttpSolrServerTest {

private HttpSolrServer server;

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.

//sorlr J 目前使用二进制的格式作为默认的格式。对于solr1.2的用户通过显示的设置才能使用XML格式。
server.setParser(new XMLResponseParser());
}

@After
public void destory() {
server = null;
System.runFinalization();
System.gc();
}

public final void fail(Object o) {
System.out.println(o);
}

@Test
public void server() {
fail(server);
}

@Test
public void query() {
String query = "name:DDR";
SolrParams params = new SolrQuery(query);
try {
QueryResponse response = server.query(params);

SolrDocumentList list = response.getResults();
for (int i = 0; i < list.size(); i++) {
fail(list.get(i));
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}

}

It is not very simple to use these things like this, so I try to write a factory class for solrserver.
package com.sillycat.easyhunter.plugin.solr;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.core.CoreContainer;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.xml.sax.SAXException;

import com.sillycat.easyhunter.common.StringUtil;

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.

// sorlr J 目前使用二进制的格式作为默认的格式。对于solr1.2的用户通过显示的设置才能使用XML格式。
server.setParser(new XMLResponseParser());
solrServer =  server;
}
}

public String getServerURL() {
if (StringUtil.isBlank(serverURL)) {
serverURL = DEFAULT_SERVER_URL;
}
return serverURL;
}

public void setServerURL(String serverURL) {
this.serverURL = serverURL;
}

public String getSolrHome() {
if (StringUtil.isBlank(solrHome)) {
solrHome = DEFAULT_SOLR_HOME;
}
return solrHome;
}

public void setSolrHome(String solrHome) {
this.solrHome = solrHome;
}

public String getSolrServerClassName() {
if (StringUtil.isBlank(solrServerClassName)) {
solrServerClassName = DEFAULT_SOLR_SERVER_CLASS_NAME;
}
return solrServerClassName;
}

public void setSolrServerClassName(String solrServerClassName) {
this.solrServerClassName = solrServerClassName;
}
}

The spring configuration will be

<bean id="httpSolrServer" class="com.sillycat.easyhunter.plugin.solr.SolrServerFactory">
<property name="solrServerClassName" value="org.apache.solr.client.solrj.impl.HttpSolrServer"/>
<property name="serverURL" value="http://localhost:8983/solr/" />
</bean>

<bean id="embeddedSolrServer" class="com.sillycat.easyhunter.plugin.solr.SolrServerFactory">
<property name="solrServerClassName" value="org.apache.solr.client.solrj.embedded.EmbeddedSolrServer"/>
<property name="solrHome" value="D:\\book\\solr\\apache-solr-3.6.0\\example\\solr" />
</bean>

The test case can be as follow:
package com.sillycat.easyhunter.plugin.solr;

import junit.framework.Assert;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.SolrParams;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/test/resources/test-context.xml" })
public class SolrServerFactoryTest {

@Autowired
@Qualifier("httpSolrServer")
private SolrServer httpSolrServer;

@Autowired
@Qualifier("embeddedSolrServer")
private SolrServer embeddedSolrServer;

@Test
public void dumy() {
Assert.assertTrue(true);
}

@Test
public void embeddedSolrServerQuery() {
String query = "name:DDR";
SolrParams params = new SolrQuery(query);

try {
QueryResponse response = embeddedSolrServer.query(params);

SolrDocumentList list = response.getResults();
for (int i = 0; i < list.size(); i++) {
Assert.assertNotNull(list.get(i));
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}

//@Test
public void httpSolrServerQuery() {
String query = "name:DDR";
SolrParams params = new SolrQuery(query);

try {
QueryResponse response = httpSolrServer.query(params);

SolrDocumentList list = response.getResults();
for (int i = 0; i < list.size(); i++) {
Assert.assertNotNull(list.get(i));
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}
}

references:
http://www.blogjava.net/hoojo/archive/2011/10/21/361747.html
http://wiki.apache.org/solr/Solrj
http://groovy.codehaus.org/Grape
http://www.java2s.com/Code/Java/Spring/MessageDigestExample.htm
http://stackoverflow.com/questions/234600/can-i-use-class-newinstance-with-constructor-arguments

猜你喜欢

转载自sillycat.iteye.com/blog/1530915