-
How to use Java to lock a file
My Java program is packaged as an .exe and will run on a Web server. If more
than one user is utilizing our Web site, more than one instance of this .exe
file might be running concurrently.
The program needs to read information from some files on the Web server.
The Java code that opens and reads the files does not lock the files. I'll
need to lock the files while they are being read so that two instances of
the .exe don't try to read the same file at the same time.
Here is the code that opens the file, reads it, and closes it:
String metricsFilePath = "MyMetricsFile.afm";
File metricsFile = null;
FileReader In = null;
try
{
metricsFile = new File(metricsFilePath);
In = new FileReader(metricsFilePath);
... Read the file ...
}
catch(FileNotFoundException e)
{
throw new FileNotFoundException("\r\nCan't find file \"" + metricsFilePath
+ "\", which is the font-metrics file for the " + fontName + " font");
}
catch(IOException e)
{
throw new IOException("\r\nCan't read file \"" + metricsFilePath + "\",
which is the font-metrics file for the " + fontName + " font");
}
finally
{
if(In != null) {In.close();}
}
Can somebody tell me how to enhance this code so that it locks and unlocks
the file?
Thanks.
-
Re: How to use Java to lock a file
The easiest way is to create a handler for your files, just a class with static
synchronized methods to access the file (ie read and write ) then have all
the instances of your application requesting read and write operations from
your handler.
Having mutually exclusive readers is not a good idea generally, because that
will slow down your application. If you implement this design you can have
different locks like read and write locks and if you want a better level
of concurrency you can have intentional read and write locks and so on.
Good luck,
Ako
"Cynthia Leslie" <cynthia.leslie@citationsoftware.com> wrote:
>
>My Java program is packaged as an .exe and will run on a Web server. If
more
>than one user is utilizing our Web site, more than one instance of this
.exe
>file might be running concurrently.
>
>The program needs to read information from some files on the Web server.
>The Java code that opens and reads the files does not lock the files. I'll
>need to lock the files while they are being read so that two instances of
>the .exe don't try to read the same file at the same time.
>
>Here is the code that opens the file, reads it, and closes it:
>
> String metricsFilePath = "MyMetricsFile.afm";
> File metricsFile = null;
> FileReader In = null;
>
> try
> {
> metricsFile = new File(metricsFilePath);
> In = new FileReader(metricsFilePath);
>
> ... Read the file ...
> }
> catch(FileNotFoundException e)
> {
> throw new FileNotFoundException("\r\nCan't find file \"" + metricsFilePath
>+ "\", which is the font-metrics file for the " + fontName + " font");
> }
> catch(IOException e)
> {
> throw new IOException("\r\nCan't read file \"" + metricsFilePath + "\",
>which is the font-metrics file for the " + fontName + " font");
> }
> finally
> {
> if(In != null) {In.close();}
> }
>
>Can somebody tell me how to enhance this code so that it locks and unlocks
>the file?
>
>Thanks.
>
>
>
-
Re: How to use Java to lock a file
Your problem is a typical OS level locks. If you are running different JVM
instances i.e exe files no java threading model and synchronization works
for you. In JDK1.4 there is a facility of obtaining file lock while reading
also If you are not using are planning to use 1.4 you need to implement something
similar to that. Use a temp file in which you write a falg indicating the
another process is reading the required file. In your code before reading
the actual file check the flag in the temp file if there is no flag write
a flag in the temp file and read the actual file. While one process reading
the actual file if second process starts execution it finds flag in temp
file and it will wait.
There is no good solution till how long to wait for the temp file flag etc.
You need to take some arbitary decisions regarding the wait time.
The other alternative solution is use native code like c/c++ in which you
can obtain system level locks and write inter process communication through
which you can notify another process to continue. Any way you are preparing
exe which means its platform dependent so why not go for native code which
may yield better performance.
"Cynthia Leslie" <cynthia.leslie@citationsoftware.com> wrote:
>
>My Java program is packaged as an .exe and will run on a Web server. If
more
>than one user is utilizing our Web site, more than one instance of this
.exe
>file might be running concurrently.
>
>The program needs to read information from some files on the Web server.
>The Java code that opens and reads the files does not lock the files. I'll
>need to lock the files while they are being read so that two instances of
>the .exe don't try to read the same file at the same time.
>
>Here is the code that opens the file, reads it, and closes it:
>
> String metricsFilePath = "MyMetricsFile.afm";
> File metricsFile = null;
> FileReader In = null;
>
> try
> {
> metricsFile = new File(metricsFilePath);
> In = new FileReader(metricsFilePath);
>
> ... Read the file ...
> }
> catch(FileNotFoundException e)
> {
> throw new FileNotFoundException("\r\nCan't find file \"" + metricsFilePath
>+ "\", which is the font-metrics file for the " + fontName + " font");
> }
> catch(IOException e)
> {
> throw new IOException("\r\nCan't read file \"" + metricsFilePath + "\",
>which is the font-metrics file for the " + fontName + " font");
> }
> finally
> {
> if(In != null) {In.close();}
> }
>
>Can somebody tell me how to enhance this code so that it locks and unlocks
>the file?
>
>Thanks.
>
>
>
-
Re: How to use Java to lock a file
"Cynthia Leslie" <cynthia.leslie@citationsoftware.com> wrote:
>
>My Java program is packaged as an .exe and will run on a Web server. If
more
>than one user is utilizing our Web site, more than one instance of this
.exe
>file might be running concurrently.
>
>The program needs to read information from some files on the Web server.
>The Java code that opens and reads the files does not lock the files. I'll
>need to lock the files while they are being read so that two instances of
>the .exe don't try to read the same file at the same time.
>
>Here is the code that opens the file, reads it, and closes it:
>
> String metricsFilePath = "MyMetricsFile.afm";
> File metricsFile = null;
> FileReader In = null;
>
> try
> {
> metricsFile = new File(metricsFilePath);
> In = new FileReader(metricsFilePath);
>
> ... Read the file ...
> }
> catch(FileNotFoundException e)
> {
> throw new FileNotFoundException("\r\nCan't find file \"" + metricsFilePath
>+ "\", which is the font-metrics file for the " + fontName + " font");
> }
> catch(IOException e)
> {
> throw new IOException("\r\nCan't read file \"" + metricsFilePath + "\",
>which is the font-metrics file for the " + fontName + " font");
> }
> finally
> {
> if(In != null) {In.close();}
> }
>
>Can somebody tell me how to enhance this code so that it locks and unlocks
>the file?
>
>Thanks.
>
The responses you've received will work. However, if it is true that you
are only reading the file (i.e. not updating/writing to it) then you don't
need locking at all and are only adding unnecessary processing overhead to
your application.
-ww
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|