我正在考虑添加JMX bean以获取lucene索引的热备份。
LuceneMBean mbean = new LuceneMBeanImpl(); ObjectName name = new ObjectName(“indexing.index:type = lucene”); MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); mbs.registerMBean(mbean,name);
LuceneMBean将有一个名为backupIndex(String directory)的方法。
我已经浏览了lucene docs并找到了Directory的copy()方法。如果我在目录上有Writer Open,这个方法会起作用吗?基本上我的代码片段如下:
public class LuceneMBeanImpl implements LuceneMBean{
public void backupIndex(String directory){
Directory fileDirectory = FSDirectory.getDirectory(directory);
Directory.copy(masterDirectory, fileDirectory,false);
}
}
答案 0 :(得分:0)
我认为一个开放的Writer应该没问题,但是当另一个线程正在修改索引时你肯定无法复制,或者你可能会得到一个FileNotFoundException。来自消息来源:
/**
* Copy contents of a directory src to a directory dest.
* If a file in src already exists in dest then the
* one in dest will be blindly overwritten.
*
* <p><b>NOTE:</b> the source directory cannot change
* while this method is running. Otherwise the results
* are undefined and you could easily hit a
* FileNotFoundException.
*
* @param src source directory
* @param dest destination directory
* @param closeDirSrc if <code>true</code>, call {@link #close()} method on source directory
* @throws IOException
*/
public static void copy(Directory src, Directory dest, boolean closeDirSrc) throws IOException {