Multithreaded file locking solution?

J

Joseph

Hello All,

I have a class which establishes a JMS network connection, and then
sniffs a directory for new files. every time it sniffs a new file, it
trylocks it (to make sure the Python program that created the file is
finsished with it) then starts an instance of a runnable class which
parses the passed file and sends some stuff out the passed JMS
connection.

This works just fine, but I would like to now have multiple instances
of my sniffer/connection class because I want multiple JMS
connections. But it trips up on the file locking because the file lock
sees it all as one process. Ideally if I had two classes sniffing the
same directory, both would try to lock the file and one would succeed,
and the other would not and would ignore it and grab the next file.
But both are able to get the lock because the lock makes no
distinction between the processes and all sorts of bad things happen.

So what should I do? I could make the snifferclass open several
connections and have it assigns them round robin style to the parse
threads, but that would involve changing a bunch of things (and
potentially breaking things). The point of all of this is to be able
to open more connections when there is heavy traffic. And the easiest
way to be able to do that would be to start multiple insrtances of my
existing program when needed.

Any suggestions?

Thanks,

Joseph
 
G

Giridhar Pendyala

Hi Joseph,

tryLock throws OverlappingFileLockException - If a lock that overlaps
the requested region is already held by this Java virtual machine, or if
another thread is already blocked in this method and is attempting to
lock an overlapping region.

Are you not getting this exception?

regds,
Giri
 
J

Joseph

Hi Giri,

No exceptions get thrown by the tryLock statement.

What if it is a different Java process? That would not be considered
the same virtual machine, right?

$ java myProg &
$ java myProg &

And sit and watch what happens.

First I listFiles() in a directory, then with each file I find I
create a RandomAccessFile then a FileChannel and then do tryLock on
the channel. If the lock is null, I continue to the next file. If it
is not null, I rename the file (moving it to a spool dir) and unlock
it. I then create an instance of my parser class passing among other
things the newly renamed File to its constructor then start() it.

Nothing bad happens until I see that sometimes the same file gets
passed by each process to 2 new parser threads. And then of course
problems occur when thet both try to do things to the file.

The programs continue to function as they are supposed to, but the
whole point of this is to be able to increase capacity, and having
processes falling overthemselves isn't really a solution.

Any suggestions?

Thanks,

Joseph
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

locking files on Linux 10
Solution 0
Locking objects in an array 25
Odd File-locking problem 3
file locking... 4
Locking around 10
File Locking Question 16
How do I add an .EXE project to a solution of DLL's? 0

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top