I developed an Client-Server application on the web, with a search module using Lucene. In the same application, the users can index new text.
So, multiple sessions can acces to the Index and concurrences problems can be possible.
I used Threads in Java. Is it the best solutions?
I call :
IndexFiles indexFiles = new IndexFiles(); indexFiles.run();
Here you are an extract of my code.
Thanks.
public class IndexFiles extends Thread{
public IndexFiles(){
}public void run(){
SynchronizedIndexWriter.insertDocument(currentIndexDocument(),"tmp/ IndexPath",new MainAnalyser());
}
}
public class SynchronizedIndexWriter {
static synchronized void insertDocument(IndexDocument document,String indexLocValue,Analyzer analyzerValue){
File f=new File(indexLocValue);
if (f.exists()) addDocumentToIndex(document,indexLocValue,analyzerValue,false);
else addDocumentToIndex(document,indexLocValue,analyzerValue,true);
}
static synchronized void addDocumentToIndex(IndexDocument document,String indexLocValue,Analyzer analyzerValue,boolean createNewIndex){
try{
IndexWriter indexWriter = new IndexWriter(indexLocValue,analyzerValue,createNewIndex);
indexWriter.addDocument(document.getDocument());
indexWriter.optimize();
indexWriter.close();
}
catch(IOException io){
// If IndexWrite don't know write on index because it's locked, recall of the function
=> It's not very safe
addDocumentToIndex(document,indexLocValue,analyzerValue,createNewIndex);
}
catch(Exception e){
}
} }
