Saturday, June 23, 2012

Does Java FileLock work in Linux?

Before I write anything I want to mention that the OS is RHEL 4 and the java version is 1.4. Also, the application does not run under any application server - it is standalone console application with main method and there is no multithreading within the code.

The code uses FileLock's The zero-argument tryLock() . The logic is - the first instance of the application acquires lock on a file that either exists or tries to create if it does not and holds until execution finishes and releases it with release(). Other instances try to get the lock and if

Here's the code:

import java.nio.channels.*;

...
public static synchronized boolean isLockObtainedFromFile() throws IOException {
//try to get the lock
try {
while ((MailingThread.LCK = MailingThread.getLockFromFile()) == null) {
Logging.myLogger.info("A previous instance is already running....");
Thread.currentThread().sleep(300000l);
}
Logging.myLogger.info("This is the first instance of this program...");
return true;
} catch (Exception ex) {
Logging.myLogger.error("Locking file not found");
if (MailingThread.LCK != null) {
MailingThread.LCK.release();
}
return false;
}
}



...
public static synchronized FileLock getLockFromFile() {
try {
new DBConstants();
System.out.println(DBConstants.LOCKING_FILE + " this is the locking file");
File file = new File(DBConstants.LOCKING_FILE);
if (!file.exists()) {
file.createNewFile();
}
return new FileOutputStream(DBConstants.LOCKING_FILE).getChannel().
tryLock();
} catch (Exception ex) {
System.out.println("Lock File not found");
System.exit(0);

return null;
}
}
...


So far this looks good. On Windows, this works perfectly, and presumably it did on our Linux machine until we noticed what was in the log file..
2012-06-21 12:07:28,769;main;LOCK RELEASED.
2012-06-21 12:07:28,769;main;******The e-campaign engine executed in total time of - 1344 seconds with total size of - 1015 emails*****
2012-06-21 12:10:05,616;main;This is the first instance of this program...
2012-06-21 12:10:06,686;main;A previous instance is already running....
2012-06-21 12:10:06,914;main;A previous instance is already running....

While the above looks fine - An instance released lock and then an instance that was waiting executes while others that were waiting continues to wait, look below:
2012-06-21 11:55:03,552;main;Send the content to
2012-06-21 11:55:03,552;main;Email has been sent to:
2012-06-21 11:55:04,337;main;This is the first instance of this program...
2012-06-21 11:55:04,750;main;A previous instance is already running....
2012-06-21 11:55:04,773;main;A previous instance is already running....

....

2012-06-21 12:00:04,837;main;Send the content to
2012-06-21 12:00:04,837;main;Email has been sent to:
2012-06-21 12:00:04,837;main;The reply email in send email
2012-06-21 12:00:04,902;main;This is the first instance of this program...
2012-06-21 12:00:04,970;main;Send the content to

Notice the new instances start running when the running instance has not even finished and released the lock. This is a very random behavior and one that is clearly not desired!

Clearly, the lock file is not being used for anything but locking. We're not modifying the content within.

My verdit: Java FileLock does not work in Linux!

 
 

Saturday, June 2, 2012

Setting up FTP in Windows Server 2008

To set up the FTP site, follow these steps:

  1. In User Manager, create a user account for each of your users.

    Note These users need to have the local log on permission. By default, new users belong to the built-in Users group, which has local log on permission.

  2. Using the Windows NT Explorer, on a partition formatted with NTFS, give the FTP site's root folder the following security access types:

    • Administrators: Full Control

    • Everyone: Read

    • System: Read



  3. Create a subfolder for each user. (These subfolders will inherit the root folder's security settings). Make the following security changes:

    • Remove the Everyone group.

    • Change the System account's folder's access to Full Control (instead of Read).

    • Add the user who will use that folder, and give that user Full Control access.



  4. Configure the home page for the user. To do this, follow these steps:




  1. In Control Panel, open Administrative Tools.

  2. Double-click Computer Management.

  3. Expand Local Users and Groups, click Users, right-click the user name, and then click Properties.

  4. Click the Profile tab.

  5. Make sure that Local path is selected under Home Folder, and then type the appropriate path in the Local path box. For example, type the path as d:\Ftp\FolderName.

    Note The user name and the folder name must match. For example, if the user name is JoeUser, the FolderName should also be JoeUser. If the folder name is different, the user will see the same view that is available to Anonymous users. This view is the complete parent folder structure. The user is still denied access to folders to which they are not specifically granted NTFS permissions. However, the parent folder view may be undesirable

  6. Also Check to see FTP service is running.