1 4 package org.javabb.lucene.index; 5 6 7 import java.io.IOException ; 8 import java.util.Date ; 9 import java.util.TimerTask ; 10 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 import org.apache.lucene.analysis.Analyzer; 14 import org.apache.lucene.index.IndexWriter; 15 import org.apache.lucene.store.Directory; 16 import org.apache.lucene.store.FSDirectory; 17 18 import org.javabb.infra.Monitor; 19 20 import org.springframework.core.io.Resource; 21 22 23 54 public class LuceneOptimizeIndexTimerTask extends TimerTask { 55 56 private static final Log logger; 57 58 static { 59 60 logger = LogFactory.getLog(LuceneOptimizeIndexTimerTask.class); 62 63 } 64 65 private static final Object monitor = Monitor.MONITOR; 66 67 private Directory lucenePath; 68 69 private Analyzer analyzer; 70 71 79 public LuceneOptimizeIndexTimerTask ( Directory lucenePath, 80 Analyzer analyzer ) { 81 82 this.lucenePath = lucenePath; 83 this.analyzer = analyzer; 84 85 } 86 87 92 public LuceneOptimizeIndexTimerTask ( Resource lucenePath, Analyzer analyzer ) 93 throws IOException { 94 95 this.lucenePath = FSDirectory.getDirectory(lucenePath.getFile(), false); 96 this.analyzer = analyzer; 97 98 } 99 100 105 public void run() { 106 107 try { 108 109 Date start = new Date (); 110 logger.info("Optimizing index - Time: [" + start + "]."); 111 112 synchronized (monitor) { 113 114 IndexWriter writer; 115 writer = new IndexWriter(lucenePath, analyzer, false); 116 117 writer.optimize(); 118 writer.close(); 119 120 } 121 122 Date finish = new Date (); 123 logger.info("Index optimized - Time: [" + finish + "]."); 124 125 } catch (Exception ex) { 126 127 logger.info("Could not optimize lucene index at [" + lucenePath 128 + "]", ex); 129 130 } 131 132 } 133 134 139 public Analyzer getAnalyzer() { 140 141 return analyzer; 142 143 } 144 145 150 public Directory getLucenePath() { 151 152 return lucenePath; 153 154 } 155 156 } 157 | Popular Tags |