Solr5.0.0定时更新索引

由于通过配置的方式定时更新不生效,故通过代码执行定时任务更新

package com.thinkgem.jeesite.modules.meeting.task;

import java.io.IOException;

import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;



/** 
* @ClassName: SolrTask 
* @Description: 
* @author wd
* @date 2018年3月23日 上午11:13:22 
*  
*/
@Service
@Lazy(false)
public class SolrTask {

    @Value("#{APP_PROP['solr.Core.full']}")
    private String coreUrlfull;
    
    @Value("#{APP_PROP['solr.Core.delta']}")
    private String coreUrldelta;

    private static final Logger LOGGER = LoggerFactory.getLogger(SolrTask.class);

    /**
     * 
     * 适用于修改 新增 (删除未做)
     * 每日零点
     * @throws IOException 
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void SolrTaskDelta() throws IOException {
        CloseableHttpClient httpCilent = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(coreUrldelta);
        try {
            CloseableHttpResponse response = httpCilent.execute(httpGet);
            if (response != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                LOGGER.info("solr增量索引任务执行成功" );
            }
        } catch (IOException e) {
            LOGGER.info("solr增量索引任务执行失败");
            e.printStackTrace();
        }finally {
            httpCilent.close();
        }
    }
    

    /**
     * 每周一零点
     * 全量索引删除后重建
     * @throws IOException 
     * 
     */
    @Scheduled(cron = "0 0 0 ? * MON ")
    public void SolrTaskFull() throws IOException {
        CloseableHttpClient httpCilent = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(coreUrlfull);
        try {
            CloseableHttpResponse response = httpCilent.execute(httpGet);
            if (response != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                LOGGER.info("solr重建全量索引成功" );
            }
        } catch (IOException e) {
            LOGGER.info("solr重建全量索引失败");
            e.printStackTrace();
        }finally {
            httpCilent.close();
        }
    }
}

 

#期刊索引更新测试服务器
solr.Core.delta=http://10.1.101.134:8983/solr/PaperReview/dataimport?command=delta-import&clean=false&commit=true
solr.Core.full=http://10.1.101.134:8983/solr/PaperReview/dataimport?command=full-import&clean=true&commit=true

 

posted @ 2018-04-02 14:53  wdmiye  阅读(280)  评论(0编辑  收藏  举报