LockObtainFailedException和索引器的实例化

时间:2014-09-29 17:12:58

标签: java exception lucene ejb

我正在开发一个用于信息检索的Web应用程序(我使用JSF),并且我使用Lucene来读取和索引文件.pdf。我读了一个关于LockObtainFailedException的上一个问题(LockObtainFailedException at new IndexWriter()),但我仍然无法解决我的问题,所以我将简要解释一下。

我有一个使用此方法索引文档的managedBean:

     @ManagedBean
      @RequestScoped
      public class managedBeanIB{


       @EJB
       private Indexer indexer;


       private Part file;;
       private long ISBN;
       private String author;
       private String title;
       private String content; // getter and setter for each field

       ...

        public String add() throws IOException{


                indexer = new Indexer();
                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";

   }
}//class

类索引器如下:

@Stateless
public class Indexer implements Serializable {


private static final String directory = "C:\\Users\\EngineFM\\Documents\\NetBeansProjects\\SIA\\SIA-ejb\\src\\resources\\index\\";

    public static final String ENG = "english";


    private IndexWriter iw;

    public Indexer() {



        try {
            if (iw == null) {


                iw = new IndexWriter(FSDirectory.open(new File(directory)),
                        new IndexWriterConfig(Version.LATEST, new EnglishAnalyzer()));

            }
        } catch (IOException ex) {
            System.out.println("something wrong");
            ex.printStackTrace();

        }

    }



    public void index(Book item) throws IOException{

        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(Libro.CONTENT, item.getContent(),Field.Store.YES));



       iw.addDocument(doc);
       iw.commit();

    }

    public void close() throws IOException {
        iw.close();
    }

当我启动我的应用程序(主页)时,我注意到Indexer与我的托管bean一起被实例化。然后我继续添加页面,我填写所需的所有信息,然后点击"添加文档"。在这种情况下,Indexer再次被实例化,但之前的锁仍然是打开的(我猜),所以doc没有存储,我有以下异常:

  

Grave:org.apache.lucene.store.LockObtainFailedException:Lock   获得超时:   NativeFSLock @ C:\用户\ EngineFM \文档\的NetBeansProjects \ SIA \ SIA-EJB的\ src \资源\首页\ write.lock   在org.apache.lucene.store.Lock.obtain(Lock.java:89)at   org.apache.lucene.index.IndexWriter。(IndexWriter.java:755)at   pack.Indexer。(Indexer.java:62)at   managedBean.managedBeanIB.add(managedBeanIB.java:107)at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)   在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)   在java.lang.reflect.Method.invoke(Method.java:483)at   javax.el.E​​LUtil.invokeMethod(ELUtil.java:332)at   javax.el.BeanELResolver.invoke(BeanELResolver.java:537)at at   javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)at at   com.sun.el.parser.AstValue.invoke(AstValue.java:283)at at   com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)   在   com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)   在   javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)     在   com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)     在javax.faces.component.UICommand.broadcast(UICommand.java:315)at   javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)     在   javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)     在   com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)     在com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)at   com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)     在javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)at   org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)     在   org.glassfish.tyrus.servlet.TyrusServletFilter.doFilter(TyrusServletFilter.java:295)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)     在   org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)     在   org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)     在com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)at   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)     在   org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)     在   com.sun.enterprise.v3.services.impl.ContainerMapper $ HttpHandlerCallable.call(ContainerMapper.java:459)     在   com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)     在   org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)     在   org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)     在   org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)     在   org.glassfish.grizzly.filterchain.ExecutorResolver $ 9.execute(ExecutorResolver.java:119)     在   org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)     在   org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)     在   org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)     在   org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)     在   org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)     在   org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)     在   org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)     在   org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)     在   org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access $ 100(WorkerThreadIOStrategy.java:56)     在   org.glassfish.grizzly.strategies.WorkerThreadIOStrategy $ WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)     在   org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:565)     在   org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:545)     在java.lang.Thread.run(Thread.java:745)

我该如何避免这种情况?我正在考虑使用unlock()方法(如https://lucene.apache.org/core/4_1_0/core/org/apache/lucene/index/IndexWriter.html#close()中所述),但我不知道将它放在何处。任何帮助表示赞赏。

根据femtoRgon的建议,我尝试修改我的代码以避免每次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并且我有两个或更多锁?我应该更改@RequestScoped注释吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。正如我之前所说,它取决于托管bean的范围。在@RequestScoped的情况下,为每个请求创建托管bean:在我的例子中,这意味着还为每个请求生成了Indexer。我在Indexer构造函数中实例化了IndexWriter,因此实例是在开始时生成的(当应用程序启动时),当我尝试计算请求时,它再次生成并且写锁定存在冲突问题。我解决了在方法index()中推迟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();

    }

此外,我删除了指令

  

indexer.close()

来自类managedBeanIB中的add()方法,因为只有在应用程序结束时才需要调用它。