lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jm <jmugur...@gmail.com>
Subject using lucene Lock inter-jvm
Date Tue, 20 Jun 2006 17:19:55 GMT
Hi,

I am trying to peruse lucene's Lock for my own purposes, I need to
lock several java processes and I thought I could reuse the Lock
stuff. I understand lucene locks work across jvm.

But I cannot make it work. I tried to reproduce my problem in a small class:

public class SysLock {
    private static final Logger logger = Logger.getLogger(SysLock.class);

    private int id;

    public SysLock(int i) {
        id = i;
    }

//    public static void main(String[] args) throws Exception {
//        System.setProperty("org.apache.lucene.lockDir", "C:\\temp\\todel");
//        SysLock l1 = new SysLock(1);
//        SysLock l2 = new SysLock(2);
//
//        TransferThread t = l1.new TransferThread(l1);
//        t.start();
//        TransferThread t2 = l2.new TransferThread(l2);
//        t2.start();
//
//        logger.info("Finished.");
//    }

    public static void main(String[] args) throws Exception {
        System.setProperty("org.apache.lucene.lockDir", "C:\\temp\\todel");
        SysLock l1 = new SysLock(new Date().getSeconds());

        TransferThread t = l1.new TransferThread(l1);
        t.start();

        logger.info("Finished.");
    }

    private void forever() throws IOException {
        FSDirectory directory = FSDirectory.getDirectory("C:\\temp\\a", true);
        try {
            new Lock.With(directory.makeLock("COMMIT_LOCK_NAME"),
COMMIT_LOCK_TIMEOUT) {
                public Object doBody() throws IOException {
                    while (true) {
                        System.out.println("i'm " + id);
                        try {
                            Thread.sleep(2000);
                        }
                        catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.run();
        }
        catch (Exception e) {
            System.out.println(id + " could not get lock");
        }
    }

    class TransferThread extends Thread {
        public TransferThread(SysLock sl) {
            this.sl = sl;
        }

        public void run() {
            try {
                sl.forever();
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        private SysLock sl;
    }
}

When I run the main() that is commented (that is, the lock works with
two threads in the same jvm) it works ok, the second TransferThread
cannot get the lock.

But when I run the uncommented main() twice, both processes adquire a
lock, even if only one lock file exists in the lockdir. Something I am
missing probably....

Many thanks
javi

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message