1 41 package com.mvnforum.search.company; 42 43 import java.io.IOException ; 44 import java.util.*; 45 46 import com.mvnforum.db.CompanyBean; 47 import com.mvnforum.db.DAOFactory; 48 import org.apache.commons.logging.Log; 49 import org.apache.commons.logging.LogFactory; 50 import org.apache.lucene.index.IndexWriter; 51 52 55 public class RebuildCompanyIndexTask extends TimerTask 56 { 57 private static Log log = LogFactory.getLog(RebuildCompanyIndexTask.class); 58 59 public static final int MERGE_FACTOR = 20; 60 61 64 RebuildCompanyIndexTask() { 65 } 66 67 70 public void run() { 71 long start = System.currentTimeMillis(); 72 73 Collection companies = null; 74 try { 75 companies = DAOFactory.getCompanyDAO().getCompanies(); 76 } catch (Exception ex) { 77 log.error("RebuildCompanyIndexTask.run : cannot get all companies from database for indexing", ex); 78 } 79 80 if (companies == null) return; 81 82 86 IndexWriter writer = null; 87 try { 88 writer = CompanyIndexer.getIndexWriter(true); 89 writer.mergeFactor = MERGE_FACTOR; 90 91 int i = 0; 92 for (Iterator iter = companies.iterator(); iter.hasNext(); ) { 93 CompanyBean companyBean = (CompanyBean) iter.next(); 94 CompanyIndexer.doIndexCompany(companyBean, writer); 95 i++; 96 } 97 writer.optimize(); 98 log.info("Rebuilding index finished successfully! " + i + " company(s) indexed"); 99 } catch (Exception e) { 100 log.error("Error while rebuilding index", e); 101 } finally { 102 try { 103 if (writer != null) { 104 writer.close(); 105 } 106 } catch (IOException ignore) { 107 } 108 } 109 log.info("RebuildCompanyIndexTask took " + (System.currentTimeMillis() - start) + " ms"); 110 } 111 } 112 | Popular Tags |