lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jm <jmugur...@gmail.com>
Subject Re: using lucene Lock inter-jvm
Date Wed, 21 Jun 2006 14:47:29 GMT
ok, in case somebody has the same problem:
The problem is the true value in
FSDirectory directory = FSDirectory.getDirectory("C:\\temp\\a", true);
it deletes the previous lock file, that belongs to the lock adquired
by the first process. Changing it to false prevents the lock being
deleted and locking works ok

On 6/20/06, jm <jmuguruza@gmail.com> wrote:
> 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