KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > lucene > index > LuceneOptimizeIndexTimerTask


1 /*
2  * Copyright 28/03/2005 - Vicinity - www.vicinity.com.br All rights reserveds
3  */

4 package org.javabb.lucene.index;
5
6
7 import java.io.IOException JavaDoc;
8 import java.util.Date JavaDoc;
9 import java.util.TimerTask JavaDoc;
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 /**
24  * {@link java.util.Timer Timer task} to optimize lucene index in a defined time
25  * period. It can be configured with a schedule which will invoke this task in
26  * defined period. Therefore, indexer classes avoid optimize index each time that
27  * them put a new document in index. If you plan use it with <a
28  * HREF="http://www.springframework.org">SpringFramework</a>, create a schedule
29  * using Timer support like the following:
30  *
31  * <pre>
32  * &lt;bean id=&quot;optimizeLuceneIndex&quot; class=&quot;org.javabb.lucene.index.LuceneOptimizeIndexTimerTask&quot;&gt;
33  * &lt;constructor-arg index=&quot;0&quot;&gt;&lt;ref local=&quot;lucenePath&quot; /&gt;&lt;/constructor-arg&gt;
34  * &lt;constructor-arg index=&quot;1&quot;&gt;&lt;ref local=&quot;analyzer&quot; /&gt;&lt;/constructor-arg&gt;
35  * &lt;/bean&gt;
36  *
37  * &lt;bean id=&quot;scheduledTask&quot; class=&quot;org.springframework.scheduling.timer.ScheduledTimerTask&quot;&gt;
38  * &lt;property name=&quot;delay&quot;&gt;&lt;value&gt;delayTime&lt;/value&gt;&lt;/property&gt;
39  * &lt;property name=&quot;period&quot;&gt;&lt;value&gt;periodTime&lt;/value&gt;&lt;/property&gt;
40  * &lt;property name=&quot;timerTask&quot;&gt;&lt;ref local=&quot;optimizeLuceneIndex&quot; /&gt;&lt;/property&gt;
41  * &lt;/bean&gt;
42  * </pre>
43  *
44  * See Spring documentation for more details about task schedule.
45  *
46  * @author Marcos Silva Pereira - marcos.pereira@vicinity.com.br
47  * @since 13/03/2005
48  *
49  * @version $Id$
50  *
51  * @see java.util.Timer
52  * @see java.util.TimerTask
53  */

54 public class LuceneOptimizeIndexTimerTask extends TimerTask JavaDoc {
55
56     private static final Log logger;
57
58     static {
59
60         // create the logger.
61
logger = LogFactory.getLog(LuceneOptimizeIndexTimerTask.class);
62
63     }
64
65     private static final Object JavaDoc monitor = Monitor.MONITOR;
66
67     private Directory lucenePath;
68
69     private Analyzer analyzer;
70
71     /**
72      * Create a LuceneOptimizeIndexTimerTask object. It is the preferencial
73      * constructor because {@link Directory} class provides a high abstraction
74      * over where lucene index will be created.
75      *
76      * @param lucenePath
77      * @param analyzer
78      */

79     public LuceneOptimizeIndexTimerTask ( Directory lucenePath,
80             Analyzer analyzer ) {
81
82         this.lucenePath = lucenePath;
83         this.analyzer = analyzer;
84
85     }
86
87     /**
88      * @param lucenePath
89      * @param analyzer
90      * @throws IOException
91      */

92     public LuceneOptimizeIndexTimerTask ( Resource lucenePath, Analyzer analyzer )
93             throws IOException JavaDoc {
94
95         this.lucenePath = FSDirectory.getDirectory(lucenePath.getFile(), false);
96         this.analyzer = analyzer;
97
98     }
99
100     /**
101      * Optimize lucene index at a period time.
102      *
103      * @see java.util.TimerTask#run()
104      */

105     public void run() {
106
107         try {
108
109             Date JavaDoc start = new Date JavaDoc();
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 JavaDoc finish = new Date JavaDoc();
123             logger.info("Index optimized - Time: [" + finish + "].");
124
125         } catch (Exception JavaDoc ex) {
126
127             logger.info("Could not optimize lucene index at [" + lucenePath
128                     + "]", ex);
129
130         }
131
132     }
133
134     /**
135      * Returns the value of {@link analyzer} attribute
136      *
137      * @return Returns the analyzer.
138      */

139     public Analyzer getAnalyzer() {
140
141         return analyzer;
142
143     }
144
145     /**
146      * Returns the value of {@link lucenePath} attribute
147      *
148      * @return Returns the lucenePath.
149      */

150     public Directory getLucenePath() {
151
152         return lucenePath;
153
154     }
155
156 }
157
Popular Tags