2016-09-27 4 views
0

この質問はmy another SO questionに関連しています。分割ステップの期間オープンIndexWriterを保つためにSpring BatchのExecutionContextに対してLucene IndexWriterをシリアライズできますか?

は、私がStepExecutionListenerSupportafterStep(StepExecution stepExecution)方法で近いその後、パーティショナのExecutionContextIndexWriterを追加すると考えられます。

私がこのアプローチで直面している挑戦は、ExecutionContextがオブジェクトをシリアライズ可能にする必要があるということです。これら二つの質問の光、Q1

Q2からIndexWriterが持っていないので、私のカスタム・ライターでコンストラクタARG無 - - コンストラクタ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(); 
     } 

    } 

ないので、私はコメントとして示さコンストラクタを追加することはできません - 引数コンストラクタは、スーパークラス内に存在しないとsuperthis前のフィールドにアクセスすることはできません。

これを達成する方法はありますか?

答えて

0

Spring Batchではこの構文がどのように機能するのかよくわかりませんが、ExecutionContextStepExecutionListenerSupportにnull以外のオブジェクトを返します。

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に、私はこのライターを閉じます。

私の春のバッチパーティションステップは、Lucene Index Writer Objectの単一インスタンスで動作します。

getLuceneIndexWriter()によって取得された作者で操作を実行しようとするとNullPointerを取得することを期待していましたが、それは発生しません(transientであるにもかかわらず)。私はなぜこれが機能するのか分かりませんが、それはありません。

Springバッチジョブのメタデータの場合、私はメモリベースのリポジトリを使用していますが、データベースベースのリポジトリは使用していません。私がメタデータのためにdbを使い始めると、これがうまくいくかどうかは分かりません。

0

いつでもパラメータなしコンストラクタを追加できます。

例えば:

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; } 

    // ... 
} 

EDIT:

(Lucene.Netを使用して)自分自身のコードを見てとった、IndexWriterはアナライザを必要とし、MaxFieldLength。デフォルトは問題を修正する必要があるよう

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

だから、これらの値を追加:

だから、スーパーの呼び出しは次のようになります。次に、アナライザのゲッターとセッターのメソッドとMaxFieldLengthを追加すると、後でそれを制御できます。

+0

'indexWriter'クラスに' super(); 'はありません。 –

+0

私が言っているのは、 'IndexWriter'は引数なしのコンストラクタを持たないため、' super() 'のコードはコンパイルされないということです。それが私がコメントした理由です。 –

+0

自分のコードをもう一度見て、アナライザーが必要です。 それはsuper(lDirectory、analyzer)になります。 私の投稿を編集させてください。 – SimonC

関連する問題