Nutch 更新索引 不重启tomcat的方法(原创)

好久没做nutch的项目了,前两天需求说,不希望每次更新索引都那么麻烦,于是我就又看了看
首先,需要重启tomcat索引才能生效,那么也就是说 tomcat把nutchBean加载到ServletContext里面了,而启动tomcat的时候,肯定先从监听器开始。所以,第一步,看一眼nutch应用的web.xml,果然第一行就写着呢
<listener>
<listener-class>org.apache.nutch.searcher.NutchBean$NutchBeanConstructor</listener-class>
</listener>
那么打开这个类,发现有个静态类,里面的方法就是要更新ServletContext的
public void contextInitialized(ServletContextEvent sce) {
final ServletContext app = sce.getServletContext();
final Configuration conf = NutchConfiguration.get(app);
LOG.info("creating new bean");
NutchBean bean = null;
try {
bean = new NutchBean(conf);
app.setAttribute(KEY, bean);//这个KEY 就是上面定义的final变量: public static final String KEY = "nutchBean";
}
catch (final IOException ex) {
LOG.error(StringUtils.stringifyException(ex));
}
}
分析这段代码,发现 conf是NutchConfiguration类的一个静态方法,那么进去看,里面还有个create()方法。
public static Configuration create() {
Configuration conf = new Configuration();
addNutchResources(conf);
return conf;
}
//addNutchResources方法
public static Configuration addNutchResources(Configuration conf) {
conf.addResource("nutch-default.xml");
conf.addResource("nutch-site.xml");
return conf;
}
重构的位置就在这了。
看了上面,那么我们要做的就非常非常简单了。一个非常简单的方法,就可以重新加载nutchBean了
===========================================
新建一个JSP,写个方法
<%!
public void rebootNutchBean(){
//首先获取servletContext
ServletContext app=this.getServletConfig().getServletContext();
//然后加载conf
Configuration conf = NutchConfiguration.create();
NutchBean bean = null;
try {
//创建新的nutchBean
bean = new NutchBean(conf);
//添加新的nutchBean到servletContext
app.setAttribute("nutchBean", bean);
}
catch (final IOException ex) {
ex.printStackTrace();
}
}
%>
==================
至此,就解决了每次更新索引后都要重启tomcat.希望对大家有所帮助。
转载请注明来自李川的qzone--http://user.qzone.qq.com/645328554/blog/1324290643
Tags: 

延伸阅读

最新评论

发表评论