Exception description
Setonce
A convenient class which offers a semi-immutable object wrapper implementation which allows one to set the value of an object exactly once, and retrieve it usually times.
A class used to encapsulate objects. values can be assigned only once and can be read multiple times. For example, private setonce <indexwriter> writer = new setonce <> ();
Writer. Set (indexwriter); can only be executed on one side, even if it is the same indexwriter;
Alreadysetexception
Ifset(Object)
Is called more than once,SetOnce.AlreadySetException
Is thrown and the operation will fail.
Writer. Set (indexwriter); this exception is thrown if it is called more than once.
Exception Source
I wrote a scheduled Index Update task.
Timertask task = new task (New runnable () {public void run () {index (); // create index indexwriter = new indexwriter (directory, indexwriterconfig ); // initialize closed indexwriter }});
Exception description
Exception in thread "main" org.apache.lucene.util.SetOnce$AlreadySetException: The object cannot be set twice! at org.apache.lucene.util.SetOnce.set(SetOnce.java:69) at org.apache.lucene.index.IndexWriterConfig.setIndexWriter(IndexWriterConfig.java:148) at org.apache.lucene.index.IndexWriter.<init>(IndexWriter.java:687)
Principle Analysis
Analysis constructor: New indexwriter (directory, indexwriterconfig );
1 public IndexWriter(Directory d, IndexWriterConfig conf) throws IOException {2 conf.setIndexWriter(this); // prevent reuse by other instances3 ……4 ……5 ……6 }
Focus on the second line Conf. setindexwriter (this );
1 private SetOnce<IndexWriter> writer = new SetOnce<>(); 2 3 /** 4 * Sets the {@link IndexWriter} this config is attached to. 5 * 6 * @throws AlreadySetException 7 * if this config is already attached to a writer. 8 */ 9 IndexWriterConfig setIndexWriter(IndexWriter writer) {10 this.writer.set(writer);11 return this;12 }
In this case, the wirter variable in indexwriterconfig is encapsulated using setonce. When indexwriter is constructed using indexwriterconfig, the writer is assigned twice, so an error is returned.
Solution
Reconstructs an indexwriterconfig
Timertask task = new task (New runnable () {public void run () {index (); // index indexwriterconfig Config = new indexwriterconfig (version. paie_36, analyzer); indexwriter = new indexwriter (directory, indexwriterconfig); // initialize closed indexwriter }});
To be continued ......
Exception -- org. Apache. Lucene. util. setonce $ alreadysetexception