Java r每个add()操作: public String add() throws IOException{ InputStream is = file.getInputStream(); PDDocument doc = PDDocument.load(is); contenuto = new PDFTextStripper().getText(doc); doc.close(); Book book = new Libro(ISBN,author,title,content); indexer.index(book); indexer.close(); return "home"; }
不幸的是,结果是相同的,似乎在write.lock上存在一些冲突。我不知道怎么可能。可能每个请求都会重新生成bean,我有两个或更多的锁?我应该更改@RequestScope注释吗 我找到了解决问题的办法。正如我前面所说的,这取决于托管bean的范围。在@RequestScoped的情况下,为每个请求创建托管bean:在我的情况下,这意味着也为每个请求生成索引器。我在索引器构造函数中实例化了IndexWriter,因此,由于实例是在开始时(应用程序启动时)生成的,因此当我尝试计算请求时,它再次生成,并且在写锁上存在冲突问题。我解决了在方法index()中延迟IndexWriter实例化的问题。这是我的代码:Java r每个add()操作: public String add() throws IOException{ InputStream is = file.getInputStream(); PDDocument doc = PDDocument.load(is); contenuto = new PDFTextStripper().getText(doc); doc.close(); Book book = new Libro(ISBN,author,title,content); indexer.index(book); indexer.close(); return "home"; },java,exception,lucene,ejb,Java,Exception,Lucene,Ejb,不幸的是,结果是相同的,似乎在write.lock上存在一些冲突。我不知道怎么可能。可能每个请求都会重新生成bean,我有两个或更多的锁?我应该更改@RequestScope注释吗 我找到了解决问题的办法。正如我前面所说的,这取决于托管bean的范围。在@RequestScoped的情况下,为每个请求创建托管bean:在我的情况下,这意味着也为每个请求生成索引器。我在索引器构造函数中实例化了IndexWriter,因此,由于实例是在开始时(应用程序启动时)生成的,因此当我尝试计算请求时,它再次生
public void index(Book item) throws IOException{
if (iw == null) {
iw = new IndexWriter(FSDirectory.open(new File(directory)),
new IndexWriterConfig(Version.LATEST, new EnglishAnalyzer()));
}
iw.deleteDocuments(new Term(Book.ID, String.valueOf(item.getISBN())));
Document doc = new Document();
doc.add(new LongField(Book.ID, item.getISBN(),Field.Store.YES));
doc.add(new StringField(Book.AUTHOR, item.getAuthor(),Field.Store.YES));
doc.add(new StringField(Book.TITLE, item.getTitle(),Field.Store.YES));
doc.add(new TextField(Book.CONTENT, item.getContent(),Field.Store.YES));
iw.addDocument(doc);
iw.commit();
}
此外,我删除了指令
索引器关闭()
从managedBeanIB类中的add()方法,因为只有在应用程序结束时才必须调用它。不要使用
unlock()
。一次只能在索引上打开一个IndexWriter
实例。这就是锁存在的原因unlock()
只应在应用程序无法正常退出并使索引保持锁定的情况下使用,即使您确定当前没有进程访问它。@femtoRgon是的,但我不控制web应用程序启动时的实例化。我该怎么做?好的,您已经用@EJB
对它进行了注释,如果我没有弄错的话,它会这样做的。那么,为什么要构造并关闭索引器作为add()
方法的一部分呢?@femtoRgon我认为这是个好主意。每次手术后(我想)都需要闭合。我应该怎么做?您应该重用相同的IndexWriter
,而不是在每次操作后关闭。打开新的编写器是昂贵的,因此无论如何,重用它们是一个好主意。
public String add() throws IOException{
InputStream is = file.getInputStream();
PDDocument doc = PDDocument.load(is);
contenuto = new PDFTextStripper().getText(doc);
doc.close();
Book book = new Libro(ISBN,author,title,content);
indexer.index(book);
indexer.close();
return "home";
}
public void index(Book item) throws IOException{
if (iw == null) {
iw = new IndexWriter(FSDirectory.open(new File(directory)),
new IndexWriterConfig(Version.LATEST, new EnglishAnalyzer()));
}
iw.deleteDocuments(new Term(Book.ID, String.valueOf(item.getISBN())));
Document doc = new Document();
doc.add(new LongField(Book.ID, item.getISBN(),Field.Store.YES));
doc.add(new StringField(Book.AUTHOR, item.getAuthor(),Field.Store.YES));
doc.add(new StringField(Book.TITLE, item.getTitle(),Field.Store.YES));
doc.add(new TextField(Book.CONTENT, item.getContent(),Field.Store.YES));
iw.addDocument(doc);
iw.commit();
}