1 43 package net.jforum.util.search.quartz; 44 45 import java.io.FileInputStream ; 46 import java.sql.Connection ; 47 import java.util.Properties ; 48 49 import net.jforum.DBConnection; 50 import net.jforum.cache.CacheEngine; 51 import net.jforum.cache.Cacheable; 52 import net.jforum.dao.DataAccessDriver; 53 import net.jforum.dao.ScheduledSearchIndexerDAO; 54 import net.jforum.util.preferences.ConfigKeys; 55 import net.jforum.util.preferences.SystemGlobals; 56 57 import org.apache.log4j.Logger; 58 import org.quartz.Job; 59 import org.quartz.JobExecutionContext; 60 import org.quartz.JobExecutionException; 61 62 66 public class QuartzSearchIndexerJob implements Job, Cacheable 67 { 68 private static final String FQN = "quartz"; 69 private static final String INDEXING = "indexing"; 70 private static Logger logger = Logger.getLogger(QuartzSearchIndexerJob.class); 71 private static CacheEngine cache; 72 73 76 public void setCacheEngine(CacheEngine engine) 77 { 78 cache = engine; 79 } 80 81 84 public void execute(JobExecutionContext context) throws JobExecutionException 85 { 86 if ("1".equals(cache.get(FQN, INDEXING))) { 87 logger.info("Indexing is already running. Going home..."); 88 return; 89 } 90 91 Properties p = this.loadConfig(); 92 93 if (p == null) { 94 return; 95 } 96 97 int step = Integer.parseInt(p.getProperty(ConfigKeys.QUARTZ_CONTEXT + ConfigKeys.SEARCH_INDEXER_STEP)); 98 99 Connection conn = null; 100 boolean autoCommit = false; 101 102 try { 103 conn = DBConnection.getImplementation().getConnection(); 104 autoCommit = conn.getAutoCommit(); 105 conn.setAutoCommit(true); 106 107 cache.add(FQN, INDEXING, "1"); 108 109 ScheduledSearchIndexerDAO dao = DataAccessDriver.getInstance().newScheduledSearchIndexerDAO(); 110 dao.index(step, conn); 111 } 112 catch (Exception e) { 113 logger.error("Error while trying to index messages. Cannot proceed. " + e); 114 e.printStackTrace(); 115 } 116 finally { 117 cache.remove(FQN, INDEXING); 118 119 if (conn != null) { 120 try { conn.setAutoCommit(autoCommit); } catch (Exception e) {} 121 DBConnection.getImplementation().releaseConnection(conn); 122 } 123 } 124 } 125 126 private Properties loadConfig() 127 { 128 String filename = SystemGlobals.getValue(ConfigKeys.SEARCH_INDEXER_QUARTZ_CONFIG); 129 130 try { 131 Properties p = new Properties (); 132 p.load(new FileInputStream (filename)); 133 134 return p; 135 } 136 catch (Exception e) { 137 logger.warn("Failed to load " + filename + ": " + e, e); 138 return null; 139 } 140 } 141 } 142 | Popular Tags |