Exception -- org. Apache. Lucene. util. setonce $ alreadysetexception

Source: Internet
Author: User
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.AlreadySetExceptionIs 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.