【问题标题】:Can we make Lucene IndexWriter serializable for ExecutionContext of Spring Batch?我们可以为 Spring Batch 的 ExecutionContext 使 Lucene IndexWriter 可序列化吗?
【发布时间】:2016-09-27 06:33:46
【问题描述】:

此问题与my another SO question 有关。

为了让IndexWriter 在分区步骤期间保持打开状态,我想在分区器的ExecutionContext 中添加IndexWriter,然后在StepExecutionListenerSupportafterStep(StepExecution stepExecution) 方法中关闭。

我在这种方法中面临的挑战是ExecutionContext 需要对象可序列化。

鉴于这两个问题,Q1Q2 - 这似乎不可行,因为我无法在我的自定义编写器中添加 no - arg 构造函数,因为 IndexWriter 没有任何 no - arg 构造函数。

    public class CustomIndexWriter extends IndexWriter implements Serializable {
    /*
private Directory d;
    private IndexWriterConfig conf;


        public CustomIndexWriter(){
            super();
            super(this.d, this.conf);
        }
        */
        public CustomIndexWriter(Directory d, IndexWriterConfig conf) throws IOException {
            super(d, conf);
        }

        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException{
            input.defaultReadObject();
        }

        private void writeObject(ObjectOutputStream output) throws IOException, ClassNotFoundException {
            output.defaultWriteObject();
        }

    }

在上面的代码中,我无法添加显示为注释的构造函数,因为在 Super 类中不存在 no - arg 构造函数并且无法访问 this 之前的字段 super

有没有办法做到这一点?

【问题讨论】:

    标签: java serialization lucene spring-batch


    【解决方案1】:

    你总是可以添加一个无参数的构造函数。

    例如:

    public class CustomWriter extends IndexWriter implements Serializable {
        private Directory lDirectory;
        private IndexWriterConfig iwConfig;
    
        public CustomWriter() {
            super();
            // Assign default values
            this(new Directory("." + System.getProperty("path.separator")), new IndexWriterConfig());
        }
    
        public CustomWriter(Directory dir, IndexWriterConfig iwConf) {
            lDirectory = dir;
            iwConfig = iwConf;
        }
    
        public Directory getDirectory() { return lDirectory; }
    
        public IndexWriterConfig getConfig() { return iwConfig; }
    
        public void setDirectory(Directory dir) { lDirectory = dir; }
    
        public void setConfig(IndexWriterConfig conf) { iwConfig = conf; }
    
        // ...
    }
    

    编辑:

    查看我自己的代码(使用 Lucene.Net)后,IndexWriter 需要一个分析器和一个 MaxFieldLength。

    所以超级调用看起来像这样:

    super(new Directory("." + System.getProperty("path.separator")), new StandardAnalyzer(), MaxFieldLength.UNLIMITED);
    

    因此,将这些值添加为默认值应该可以解决问题。然后可能会为分析器和 MaxFieldLength 添加 getter 和 setter 方法,这样您就可以在稍后阶段控制它。

    【讨论】:

    • IndexWriter 类中没有 super();
    • 我的意思是IndexWriter 没有无参数构造函数,因此super() 的代码将无法编译。这就是我评论它的原因。
    • 再次查看我自己的代码后,您需要一个分析器。所以它将是超级(lDirectory,分析器)。让我编辑我的帖子。
    • 根据更大的程序逻辑,没有默认值。我正在使用partitioner of Spring Batch 为每个线程计算这些值,并且一个线程必须处理这些值 - 没有默认值。
    【解决方案2】:

    我不确定这种语法在 Spring Batch 中是如何工作的,ExecutionContextStepExecutionListenerSupport 中返回一个非空对象。

    public class CustomIndexWriter implements Serializable {
    
    
        private static final long serialVersionUID = 1L;
    
        private transient IndexWriter luceneIndexWriter;
    
        public CustomIndexWriter(IndexWriter luceneIndexWriter) {
             this.luceneIndexWriter=luceneIndexWriter;
        }
    
        public IndexWriter getLuceneIndexWriter() {
            return luceneIndexWriter;
        }
    
        public void setLuceneIndexWriter(IndexWriter luceneIndexWriter) {
            this.luceneIndexWriter = luceneIndexWriter;
        }
    
    
    }
    

    我将CustomIndexWriter 的实例放在步骤分区器中,分区的步骤块与写入器一起工作,getLuceneIndexWriter() 然后在StepExecutionListenerSupport 中,我关闭了这个写入器。

    这样,我的 spring 批处理分区步骤与 Lucene Index Writer Object 的单个实例一起工作。

    我希望如果尝试对getLuceneIndexWriter() 获得的写入器执行操作,我会得到一个 NullPointer,但这不会发生(尽管它是 transient)。我不确定为什么会这样,但确实如此。

    对于 Spring Batch 作业元数据,我使用的是内存存储库,而不是基于数据库的存储库。不确定一旦我开始使用 db 作为元数据,这是否会继续工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-30
      • 2023-01-22
      • 1970-01-01
      • 2017-06-07
      • 2012-07-31
      • 2011-01-28
      相关资源
      最近更新 更多