Commit 1cec9af7 authored by Quxl's avatar Quxl

x

parent 7017c98f
package com.egolm.shop.schedules;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.common.SolrInputDocument;
import com.egolm.common.StringUtil;
import com.egolm.shop.config.XException;
import com.google.common.collect.Lists;
public abstract class AbstractSolrApi implements SolrApi {
public abstract HttpSolrClient getSolrClient();
public void update(List<Map<String, Object>> list) {
HttpSolrClient solrClient = getSolrClient();
try {
List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
for (Map<String, Object> map : list) {
SolrInputDocument solrInputDocument = new SolrInputDocument();
for (String key : map.keySet()) {
if (key != null && key.trim().length() > 0) {
solrInputDocument.addField(key, StringUtil.toString(map.get(key)));
}
}
docs.add(solrInputDocument);
}
List<List<SolrInputDocument>> doclist = Lists.partition(docs, 1000);
for(List<SolrInputDocument> tmp : doclist) {
solrClient.add(tmp);
}
solrClient.commit();
} catch (Exception e) {
try {
solrClient.rollback();
} catch (Exception ex) {
logger.error("重建索引异常回滚失败", ex);
}
throw new XException("重建索引失败", e);
}
}
@Override
public void deleteById(List<String> documentIds) {
HttpSolrClient solrClient = getSolrClient();
try {
if (documentIds != null && documentIds.size() > 0) {
solrClient.deleteById(documentIds);
solrClient.commit();
}
} catch (Exception e) {
try {
solrClient.rollback();
} catch (Exception ex) {
logger.error("删除索引异常回滚失败", ex);
}
throw new XException("删除索引失败", e);
}
}
@Override
public void clear() {
HttpSolrClient solrClient = getSolrClient();
try {
solrClient.deleteByQuery("*");
solrClient.commit();
} catch (Exception e) {
try {
solrClient.rollback();
} catch (Exception ex) {
logger.error("删除全部索引异常回滚失败", ex);
}
throw new XException("删除全部索引失败", e);
}
}
}
package com.egolm.shop.schedules;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
public interface SolrApi {
Logger logger = Logger.getLogger(SolrApi.class);
public void update(List<Map<String, Object>> datas);
public void deleteById(List<String> idList);
public void clear();
}
package com.egolm.shop.schedules.goods;
import java.util.Date;
import java.util.stream.Collectors;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.egolm.common.jdbc.JdbcTemplate;
import com.egolm.shop.schedules.AbstractSolrApi;
@Component
public class GoodsCollectTask extends AbstractSolrApi{
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
@Qualifier("solrClient")
HttpSolrClient solrClient;
@Value("${solr.goods.offset-minute-for-day}")
private Long offsetMinuteForDay;
@Value("${solr.goods.offset-minute-for-minute}")
private Long offsetMinuteForMinute;
@Value("${sql.goods.create-index-query}")
private String createIndexQueryFilePath;
@Value("${sql.goods.delete-index-query}")
private String deleteIndexQueryFilePath;
@Value("${sql.goods.update-index-query}")
private String updateIndexQueryFilePath;
private String createIndexQuerySql;
private String updateIndexQuerySql;
private String deleteIndexQuerySql;
@Scheduled(cron="${solr.goods.cron-for-day}")
public void dayExecute() {
Date date = new Date(System.currentTimeMillis() - (offsetMinuteForDay*60L*1000L));
this.updateIndex(date);
}
@Scheduled(cron="${solr.goods.cron-for-minute}")
public void minuteExecute() {
Date date = new Date(System.currentTimeMillis() - (offsetMinuteForMinute*60L*1000L));
this.updateIndex(date);
}
public void createIndex() {
this.clear();
this.update(jdbcTemplate.queryForList(createIndexQuerySql));
}
public void updateIndex(Date date) {
this.deleteById(jdbcTemplate.queryForList(deleteIndexQuerySql, date).stream().map(o -> String.valueOf(o.get("id"))).collect(Collectors.toList()));
this.update(jdbcTemplate.queryForList(updateIndexQuerySql, date));
}
@Override
public HttpSolrClient getSolrClient() {
return solrClient;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment