Skip to content

Instantly share code, notes, and snippets.

@mocobeta
Created December 28, 2012 13:22
Show Gist options
  • Save mocobeta/4397800 to your computer and use it in GitHub Desktop.
Save mocobeta/4397800 to your computer and use it in GitHub Desktop.
Lucene ディレクトリロックサンプル
public class LockTest {
public static void main(String[] args) {
try {
MyIndexer i1 = new MyIndexer("data");
i1.start();
MyIndexer i2 = new MyIndexer("data");
i2.start();
Thread.sleep(60000);
i1.kill();
i2.kill();
} catch (Exception e) {
e.printStackTrace();
}
}
static class MyIndexer implements Runnable {
private Directory dir;
private boolean live;
MyIndexer(String dirPath) throws IOException {
this.dir = new MMapDirectory(new File(dirPath));
}
void start() {
live = true;
Thread th = new Thread(this);
th.start();
}
public void kill() {
live = false;
}
@Override
public void run() {
String name = Thread.currentThread().getName();
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40,
new WhitespaceAnalyzer(Version.LUCENE_40));
while (live) {
try {
IndexWriter writer = new IndexWriter(dir, config);
System.out.println(name + ": Obtain Lock.");
Thread.sleep(10000);
System.out.println(name + ": Release Lock.");
writer.close();
} catch (LockObtainFailedException e) {
System.out.println(name + ": Failed Obtain Lock.");
} catch (Exception e) {
} finally {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment