|
经过将近一天的努力,终于搞定了Solr的 Data Import Request Handler Scheduler。
Scheduler主要解决两个问题:
1.定时增量更新索引。
2.定时重做索引。
经过测试,Scheduler已经可以实现完全基于配置,无需开发功能,无需人工干预的情况下实现以上两个功能(结合 Solr 的 Data Import Request Handler前提下)。
为了方便以后使用,我将代码放到http://code.google.com上,地址是:http://code.google.com/p/solr-dataimport-scheduler/
这里贴出一下主要的代码备忘:
SolrDataImportProperties.java 配置文件读取:
View Code BaseTimerTask.java TimerTask基类,封装了一些基本的属性读取、请求发送方法:
View Code
1 package org.apache.solr.handler.dataimport.scheduler;
2
3 import java.io.IOException;
4 import java.net.HttpURLConnection;
5 import java.net.MalformedURLException;
6 import java.net.URL;
7 import java.text.DateFormat;
8 import java.text.ParseException;
9 import java.text.SimpleDateFormat;
10 import java.util.Date;
11 import java.util.Timer;
12 import java.util.TimerTask;
13
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 public abstract class BaseTimerTask extends TimerTask {
18 protected String syncEnabled;
19 protected String[] syncCores;
20 protected String server;
21 protected String port;
22 protected String webapp;
23 protected String params;
24 protected String interval;
25 protected String cores;
26 protected SolrDataImportProperties p;
27 protected boolean singleCore;
28
29 protected String reBuildIndexParams;
30 protected String reBuildIndexBeginTime;
31 protected String reBuildIndexInterval;
32
33 protected static final Logger logger = LoggerFactory
34 .getLogger(BaseTimerTask.class);
35
36 public BaseTimerTask(String webAppName, Timer t) throws Exception {
37 // load properties from global dataimport.properties
38 p = new SolrDataImportProperties();
39 reloadParams();
40 fixParams(webAppName);
41
42 if (!syncEnabled.equals("1"))
43 throw new Exception("Schedule disabled");
44
45 if (syncCores == null
46 || (syncCores.length == 1 && syncCores[0].isEmpty())) {
47 singleCore = true;
48 logger.info(" Single core identified in dataimport.properties");
49 } else {
50 singleCore = false;
51 logger.info(" Multiple cores identified in dataimport.properties. Sync active for: "
52 + cores);
53 }
54 }
55
56 protected void reloadParams() {
57 p.loadProperties(true);
58 syncEnabled = p.getProperty(SolrDataImportProperties.SYNC_ENABLED);
59 cores = p.getProperty(SolrDataImportProperties.SYNC_CORES);
60 server = p.getProperty(SolrDataImportProperties.SERVER);
61 port = p.getProperty(SolrDataImportProperties.PORT);
62 webapp = p.getProperty(SolrDataImportProperties.WEBAPP);
63 params = p.getProperty(SolrDataImportProperties.PARAMS);
64 interval = p.getProperty(SolrDataImportProperties.INTERVAL);
65 syncCores = cores != null ? cores.split(",") : null;
66
67 reBuildIndexParams = p
68 .getProperty(SolrDataImportProperties.REBUILDINDEXPARAMS);
69 reBuildIndexBeginTime = p
70 .getProperty(SolrDataImportProperties.REBUILDINDEXBEGINTIME);
71 reBuildIndexInterval = p
72 .getProperty(SolrDataImportProperties.REBUILDINDEXINTERVAL);
73
74 }
75
76 protected void fixParams(String webAppName) {
77 if (server == null || server.isEmpty())
78 server = "localhost";
79 if (port == null || port.isEmpty())
80 port = "8080";
81 if (webapp == null || webapp.isEmpty())
82 webapp = webAppName;
83 if (interval == null || interval.isEmpty() || getIntervalInt() |
|
|
|
|
|
|