提交 7d9b07f6 authored 作者: renjiancai's avatar renjiancai

--no commit message

上级 f66a5333
package com.zrqx.resource.commons.solr;
import java.io.IOException;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.springframework.beans.factory.annotation.Value;
public abstract class SolrAdapter implements SolrInterface{
@Value("${spring.data.solr.host}")
private String BASE_URL ;
/**
* 创建SolrServer对象
* @return
* @author ycw
* @date: 2019年3月1日 下午4:38:00
*/
/*private static HttpSolrClient solrServer = new HttpSolrClient.Builder(BASE_URL)
.withConnectionTimeout(10000)
.withSocketTimeout(60000)
.build();*/
/**
* 创建SolrServer对象
* @return
* @author ycw
* @date: 2019年3月1日 下午4:38:00
*/
private HttpSolrClient getSolrClient(){
/*
* 设置超时时间
* .withConnectionTimeout(10000)
* .withSocketTimeout(60000)
*/
return new HttpSolrClient.Builder(BASE_URL)
.withConnectionTimeout(10000)
.withSocketTimeout(60000)
.build();
}
/**
* 更新索引
* @see com.zrqx.resource.commons.solr.SolrInterface#createIndex()
* @return
* @author ycw
* @date: 2019年4月12日 下午3:16:02
*/
@Override
public String createIndex() {
String status = "0";
HttpSolrClient solrServer = getSolrClient();
SolrQuery sq=new SolrQuery();
sq.set("qt","/dataimport");
sq.set("command","full-import");
try {
solrServer.query(sq);
//sq.clear();
solrServer.commit();
status = "1";
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return status;
}
/**
* 增量更新
* @see com.zrqx.resource.commons.solr.SolrInterface#deltaImportIndex()
* @return
* @author ycw
* @date: 2019年4月12日 上午11:59:35
*/
@Override
public String deltaImportIndex() {
String status = "0";
HttpSolrClient solrServer = getSolrClient();
SolrQuery sq=new SolrQuery();
sq.set("qt","/dataimport");
sq.set("command","delta-import");
try {
solrServer.query(sq);
solrServer.commit();
status = "1";
} catch (Exception e) {
e.printStackTrace();
}
return status;
}
}
package com.zrqx.resource.commons.solr;
public interface SolrInterface {
/**
* 更新索引
* @return
* @author ycw
* @date: 2019年4月12日 下午3:08:54
*/
public String createIndex();
/**
* 增量索引
* @return
* @author ycw
* @date: 2019年4月12日 下午3:09:00
*/
public String deltaImportIndex();
}
package com.zrqx.resource.commons.solr;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* 全文检索更新索引
* @author ycw
* @date 2019年4月12日上午11:59:56
*/
@Component
public class SolrManage extends SolrAdapter {
private static final Logger log = Logger.getLogger(SolrManage.class);
@Value("${solr-quartz}")
private Boolean offon;
/**
* 生成索引(全部)
*
* @author ycw
* @date: 2019年4月12日 上午11:57:20
*/
@Scheduled(cron="0 0 0/1 * * ? ")
public void commandIndex(){
if(offon){
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
log.info("定时任务1:全文检索全部更新资源库索引开始...时间:"+f.format(new Date()));
String result = this.createIndex();
if(result.equals("1")){
log.info("定时任务1:全文检索更新成功...时间:"+f.format(new Date()));
}else{
log.info("定时任务1:全文检索更新失败...时间:"+f.format(new Date()));
}
}
}
/**
* 增量更新
*
* @author ycw
* @date: 2019年4月12日 下午4:57:04
*/
//@Scheduled(cron="0 0/10 * * * ? ")
public void updateDeltaIndex(){
if(offon){
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
log.info("定时任务2:全文检索全部更新资源库索引开始...时间:"+f.format(new Date()));
String result = this.deltaImportIndex();
if(result.equals("1")){
log.info("定时任务2:全文检索更新成功...时间:"+f.format(new Date()));
}else{
log.info("定时任务2:全文检索更新失败...时间:"+f.format(new Date()));
}
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论