public void run()

in lucene/src/main/java/org/apache/ofbiz/content/search/DocumentIndexer.java [77:158]


    public void run() {
        IndexWriter indexWriter = null;
        int uncommittedDocs = 0;
        while (true) {
            LuceneDocument ofbizDocument;
            try {
                // Execution will pause here until the queue receives a LuceneDocument for indexing
                ofbizDocument = documentIndexQueue.take();
            } catch (InterruptedException e) {
                Debug.logError(e, MODULE);
                if (indexWriter != null) {
                    try {
                        indexWriter.close();
                        indexWriter = null;
                    } catch (IOException ioe) {
                        Debug.logError(ioe, MODULE);
                    }
                }
                break;
            }
            Term documentIdentifier = ofbizDocument.getDocumentIdentifier();
            Document document = ofbizDocument.prepareDocument(this.delegator);
            if (indexWriter == null) {
                try {
                    StandardAnalyzer analyzer = new StandardAnalyzer();
                    indexWriter = new IndexWriter(this.indexDirectory, new IndexWriterConfig(analyzer));
                } catch (CorruptIndexException e) {
                    Debug.logError("Corrupted lucene index: " + e.getMessage(), MODULE);
                    break;
                } catch (LockObtainFailedException e) {
                    Debug.logError("Could not obtain Lock on lucene index " + e.getMessage(), MODULE);
                    // TODO: put the thread to sleep waiting for the locked to be released
                    break;
                } catch (IOException e) {
                    Debug.logError(e.getMessage(), MODULE);
                    break;
                }
            }
            try {
                if (document == null) {
                    indexWriter.deleteDocuments(documentIdentifier);
                    if (Debug.infoOn()) {
                        Debug.logInfo(getName() + ": deleted Lucene document: " + ofbizDocument, MODULE);
                    }
                } else {
                    indexWriter.updateDocument(documentIdentifier, document);
                    if (Debug.infoOn()) {
                        Debug.logInfo(getName() + ": indexed Lucene document: " + ofbizDocument, MODULE);
                    }
                }
            } catch (Exception e) {
                Debug.logError(e, getName() + ": error processing Lucene document: " + ofbizDocument, MODULE);
                if (documentIndexQueue.peek() == null) {
                    try {
                        indexWriter.close();
                        indexWriter = null;
                    } catch (IOException ioe) {
                        Debug.logError(ioe, MODULE);
                    }
                }
                continue;
            }
            uncommittedDocs++;
            if (uncommittedDocs == UNCOMMITTED_DOC_LIMIT || documentIndexQueue.peek() == null) {
                // limit reached or queue empty, time to commit
                try {
                    indexWriter.commit();
                } catch (IOException e) {
                    Debug.logError(e, MODULE);
                }
                uncommittedDocs = 0;
            }
            if (documentIndexQueue.peek() == null) {
                try {
                    indexWriter.close();
                    indexWriter = null;
                } catch (IOException e) {
                    Debug.logError(e, MODULE);
                }
            }
        }
    }