1 package org.jahia.services.search; 2 3 import java.util.LinkedList ; 4 import java.util.List ; 5 import java.util.Locale ; 6 import java.util.Vector ; 7 8 import org.apache.log4j.Logger; 9 import org.jahia.exceptions.JahiaException; 10 import org.jahia.params.ParamBean; 11 import org.jahia.registries.ServicesRegistry; 12 import org.jahia.services.database.ConnectionDispenser; 13 import org.jahia.services.sites.JahiaSite; 14 import org.jahia.services.sites.SiteLanguageSettings; 15 import org.jahia.utils.LanguageCodeConverters; 16 import org.quartz.JobDataMap; 17 import org.quartz.JobExecutionContext; 18 import org.quartz.JobExecutionException; 19 import org.quartz.StatefulJob; 20 21 28 public class ScheduledSiteIndexationJob implements StatefulJob { 29 30 private static Logger logger = Logger.getLogger(ScheduledSiteIndexationJob.class); 31 32 public ScheduledSiteIndexationJob () { 33 } 34 35 public void execute (JobExecutionContext context) 36 throws JobExecutionException { 37 38 logger.debug("Site Indexation Job executed in Thread name=" + Thread.currentThread().getName()); 39 40 SiteIndexationStatus status = null; 41 ServicesRegistry sReg = ServicesRegistry.getInstance(); 42 JahiaSearchService searchService = sReg.getJahiaSearchService(); 43 boolean success; 44 try { 45 SiteIndexationJobDetail jobDetail = 46 (SiteIndexationJobDetail)context.getJobDetail(); 47 JobDataMap jobDataMap = jobDetail.getJobDataMap(); 48 49 ParamBean jParams = (ParamBean)jobDataMap.get("jParams"); 50 int siteId = jobDetail.getSiteId(); 51 52 status = searchService.getSiteIndexationStatus(siteId); 53 if ( status == null ){ 54 logger.error(" Site Indexation status not found for siteId=" + siteId 55 + " , indexation not started"); 56 } 57 status.setStartingTime(System.currentTimeMillis()); 58 JahiaSite site = null; 59 List localeList = new LinkedList (); 60 List listFieldID = new LinkedList (); 61 62 try { 63 site = sReg.getJahiaSitesService().getSite(siteId); 64 65 Vector siteLanguageSettings = site.getLanguageSettings (); 66 if (siteLanguageSettings != null) { 67 for (int i = 0; i < siteLanguageSettings.size (); i++) { 68 SiteLanguageSettings curSetting = (SiteLanguageSettings) 69 siteLanguageSettings.elementAt (i); 70 if (curSetting.isActivated ()) { 71 Locale tempLocale = LanguageCodeConverters. 72 languageCodeToLocale (curSetting. 73 getCode ()); 74 localeList.add (tempLocale); 75 } 76 } 77 } 78 listFieldID = sReg.getJahiaFieldService().getAllFieldIDs(site.getID()); 79 } catch (JahiaException je ){ 80 status.setStatus(SiteIndexationStatus.ERROR_STATUS); 81 throw new JobExecutionException("Error sarting site re-indexation" + siteId ,je , false); 82 } 83 84 int size = listFieldID.size(); 86 status.setNbDocsToIndex(size); 87 status.setNbIndexedDocs(0); 88 success = true; 89 for (int i = 0; i < size; i++) { 90 if ( status.isShutdown() ){ 91 success = false; 92 return; 93 } 94 int fieldID = ( (Integer ) listFieldID.get(i)).intValue(); 95 while ( searchService.getNbDocumentsInQueue()>500 ) { 96 status = searchService.getSiteIndexationStatus(siteId); 97 if ( status.isShutdown() ){ 98 success = false; 99 return; 100 } 101 try { 102 Thread.sleep(1000); 103 } catch ( java.lang.InterruptedException ie ){ 104 } 105 searchService.notifyMainIndexThread(); 106 } 107 status = searchService.getSiteIndexationStatus(siteId); 108 if ( !status.isShutdown() ){ 109 110 try { 111 status.setNbIndexedDocs(status.getNbIndexedDocs()+1); 112 searchService.indexField(fieldID, false, jParams, false); 113 ConnectionDispenser.terminateConnection(); 114 115 } 116 catch (Throwable t) { 117 logger.debug("Error re-indexing field id=" + fieldID); 119 } 120 } else { 121 success = false; 122 return; 123 } 124 } 125 ConnectionDispenser.terminateConnection(); 126 127 status.setDone(success); 128 status.setEndTime(System.currentTimeMillis()); 129 status.setStatus(success?SiteIndexationStatus.SUCCESS_STATUS:SiteIndexationStatus.ERROR_STATUS); 130 } catch (Throwable t) { 131 ConnectionDispenser.abortConnection(); 132 } 133 } 134 } 135 | Popular Tags |