【问题标题】:Lucene IndexWriters in @Singleton @ApplicationScoped bean closes the IndexWriter@Singleton @ApplicationScoped bean 中的 Lucene IndexWriters 关闭 IndexWriter
【发布时间】:2017-10-11 17:19:54
【问题描述】:

我需要根据 Java Web 应用程序中的内容将文档上传到不同的索引中,其中多个用户可以同时上传多个文档

我正在使用 Lucene 6.2.1 进行索引

为此,我创建了一个无状态 EJB。在上传文档时对文档进行索引,称为 IndexingSessionBean

但由于我不能在一个索引上打开多个 IndexWriters,我创建了一个名为 CatagoryIndexWriters 的 @Singleton 和 @ApplicationScoped bean,它应该为每个文档类别提供一个索引编写器映射并将其传递给 IndexingSessionBean。

我的代码如下

IndexingSessionBean.java

@Stateless
public class IndexingSessionBean {
    @EJB
    CatagoryIndexWriters catagoryIndexWriters;

    public void indexFile(String documentId, String catId, byte[] fileBytes, boolean isUpdate) {

        String content = // get contents of the fileBytes in String

        try {
            IndexWriter writer = catagoryIndexWriters.getTargetIndexWriter(catId)
            Document doc = new Document();
            Field documentIdField = new StringField("documentId", documentId, Field.Store.YES);
            doc.add(documentIdField);
            doc.add(new TextField("contents", content, Field.Store.YES));
            if (!isUpdate) {
                LOG.log(Level.INFO, "Indexing file with documentId {0}", documentId);
                writer.addDocument(doc);
            } else {
                LOG.log(Level.INFO, "Updating Index for file with documentId {0}", documentId);
                writer.updateDocument(new Term("documentId", documentId), doc);
            }
        }
        catch (IOException ex) {
            LOG.log(Level.SEVERE, "Unable to index document!", ex);
        }

    }
}

CatagoryIndexWriters

@Singleton
@ApplicationScoped
@ConcurrencyManagement(BEAN)
public class CatagoryIndexWriters {

    @EJB
    SystemConfigBean systemConfigBean;


    Map<String, IndexWriter> indexWritersMap =new HashMap<String, IndexWriter>();
    private double RAMBufferSize = 256.00;

    public IndexWriter getCatagoryIndexWriter(String catId){
        IndexWriter writer;
        writer = indexWritersMap.get(catId);
        if (writer != null){
            return writer;
        }else{
            addCatagoryIndexWriterToMap(catId);
            return indexWritersMap.get(catId);
        }

    }

    private void createCatagoryIndexPath(String catId){
        String indexPath = systemConfigBean.getSearchindexPath();
        String catIndexPathString = indexPath+systemConfigBean.SEPARATORCHAR+catId;
        Path catIndexPath = new File(catIndexPathString).toPath();

        //Check the Catagory Index Folder if there is no index folder create it.

    }

    private void addCatagoryIndexWriterToMap(String catId){
        createCatagoryIndexPath(catId);
        String indexPath = systemConfigBean.getSearchindexPath();
        String catIndexPathString = indexPath+systemConfigBean.SEPARATORCHAR+catId;
        Path catIndexPath = new File(catIndexPathString).toPath();

        try {
            Directory dir = FSDirectory.open(catIndexPath);
            Analyzer analyzer = new StandardAnalyzer();
            IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
            iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
            iwc.setRAMBufferSizeMB(this.RAMBufferSize);
            try (IndexWriter writer = new IndexWriter(dir, iwc)) {
                indexWritersMap.put(catId, writer);
            }
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

但是在添加文档时我得到以下异常..

Mai 12, 2017 12:54:59 PM org.apache.openejb.core.transaction.EjbTransactionUtil handleSystemException
SCHWERWIEGEND: EjbTransactionUtil.handleSystemException: this IndexWriter is closed
org.apache.lucene.store.AlreadyClosedException: this IndexWriter is closed
        at org.apache.lucene.index.IndexWriter.ensureOpen(IndexWriter.java:740)
        at org.apache.lucene.index.IndexWriter.ensureOpen(IndexWriter.java:754)
        at org.apache.lucene.index.IndexWriter.updateDocument(IndexWriter.java:1558)
        at org.apache.lucene.index.IndexWriter.addDocument(IndexWriter.java:1307)
        at de.zaffar.docloaddoc.beans.IndexingSessionBean.indexFile(IndexingSessionBean.java:257)

我不知道IndexWriter bieng中的close方法是从哪里调用的

【问题讨论】:

    标签: java indexing lucene


    【解决方案1】:

    您的问题似乎出在try (IndexWriter writer = new IndexWriter(dir, iwc)) 这一行,因此该资源将在尝试语句后自动关闭,即一旦您将其放入地图。

    try-with-resource 有一个非常具体的用例,即在 try - 块中使用该资源,否则它将被关闭。

    IndexWriter确实实现了AutoCloseable,所以它被关闭了。

    将其从 try-with-resource 中删除并使其成为正常语句,然后重试。

    【讨论】:

    • 在 SO 上,如果您稍后找到解决方案,您可以回答自己的问题。这样人们就会知道你已经解决了你的问题并且不会试图回答。
    • 来到 SOF 回答它......但发现你的回复很有帮助......无论如何,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多