• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton and FTP  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to call a class (SendFile) which will transfer the file to the ftp server through a scheduler in unix.
This class is called by a scheduler every half an hour. I have used singleton pattern.

The class SendFile have method which reads files from Local Folder and copies it to FTP Folder.

public class SendFile extends SendFileImpl {

private static SendFile sendFileObj = new SendFile();

public static SendFile getInstance() {
if (sendFileObj == null ) {
sendFileObj = new SendFile();
}
return sendFileObj;
}

public static void main(String[] args) {
SendFileImpl sendFileObj = SendFile.getInstance();
sendFileObj.sendFilesToFtp();
}

public void sendFilesToFtp(){
// send files to FTP code
}

}

public abstract class SendFileImpl {
public abstract void sendFilesToFtp();
}



1. Should I make the object of this class a singleton?
If the process of copying the file from the Local Folder to the FTP Folder is continued even after half an hour.
When the scheduler runs next time what will happen will it call SendFile method again on the same instance. As this could be fatal if the 1st process is still
reading the file from local and transferring it to the ftp.

Can you suggest me a way in which I should design this to ensure
1. That every time the scheduler calls SendFiles it should insure that there is no other instance active which is executing sendFilesToFtp method.
2. Also if we do not use Singleton here how can we avoid making multiple instances.As that may cause a lot of instance to be in memory if the file size is large .
 
Bartender
Posts: 9444
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sorabh jaiswal:
Can you suggest me a way in which I should design this to ensure


The common idiom I've seen with this functionality is to make a directory just for outgoing files. A scheduled task moves files to be FTP'd to that file (maybe renaming them with a date format so we can tell them apart).
A second task runs as a daemon (long-running program) and observes the upload directory. When a file appears in that directory, it is FTP'd. When the FTP is successful, the file is deleted.
 
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have not mentioned the scheduler you are using.
The behavior depends on the scheduler also, for instance, if you are using java.util.Timer with a fixed delay(normal schedule methods) then all the executions will happen with a fixed delay and not concurrently.
Secondly, the instance sharing is totally dependent on the task that you are submitting to the scheduler. No scheduler can itself decide how to call your singleton(obviously if it does not implement an interface know to the scheduler)

About the singleton question. Is there any state that the class holds?
By your description, it does not look like. If that is the case then you do not even need a singleton, just make the send method static and call it from your task.
 
sorabh jaiswal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using cronjob in unix to schedule the SendFile class. The cron executes for every half an hour and send the file. I agree with you that I should make a simple object instead of Singleton.

But can you clarify below.

Suppose the cronjob executes for 1st time and it makes an instance A. A will execute sendFilesToFtp() method. This method will read the file and starts moving it to the FTP location. While this is in process the cronjob executes for second time making an instace B. B will then try to access the same file which is in process this will give (IO error/access denied err).
Q> How can I avoid this? How can I check if the file is being processed when I have one more instance trying to send the file?(I am not allowed to use any java based scheduler or timertask)
Q> I was thinking to synchronize the method but it will only add more delay to each execution, can you suggest me any solution.

Thanks in advance.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just rename the file before starting to process it.
This will make the other process to not pick the file. When you are done, delete the file, This is on the same lines as Joe specified in his post. (Provided the ftp file send depends on the name. If not, then copy the files to a new dir and process from the new location.)
Regarding synchronization, it will not work as a new invocation of the cron job will launch a new JVM and hence the object for synchronization will not be the same.
 
Ranch Hand
Posts: 35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cron job will make a new java jvm so there is no point using singleton which is there only in particular instance of a JVM.

instead use the File in the outgoing folder ... and can keep a log to know when you tried to transfer and if success
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!