How to wait on a file to populate?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ndedhia1
    New Member
    • Jan 2009
    • 112

    How to wait on a file to populate?

    Hi,
    I need help writing code to wait for a file to populate on another machine.
    I am doing a cattail on a log file on another machine, and when the file gets written to, then I need to perform some action in my java code, but I need the code to wait till something on the log file does get populated.
    Right now, my code seems to fly by the section that has the cattail command and I need for it to stop and wait.

    I have tried creating a thread and giving the thread the max priority, but i still can not get the method to wait for my cattail.

    The code below shows the class and methods involved that i have written that doesnt work:

    Code:
    public void run()
    	{
    		try
    		{
    			System.out.println("YOU ARE IN DESKTOPSHMTRANSLATOR RUN()");
    			this.listen();
    		}
    		catch(Exception e)
    		{
    			System.out.println("RUN EXCEPTION");
    		}
    	}
    	
    	public void listen() throws Exception
    	{
    		System.out.println("YOU ARE IN DESKTOPSHMTRANSLATOR LISTEN()");
    		
    		String remoteCommand = "cattail";
    		String remoteDir = "/infra/sbt-support/supportTools/hsqldb/ics/tmp/";
    		String remoteFile = "alarmNotification.log.orig";
    		
    		String server = "server";
    		String userName = "username";
    		String password = "password";
    		
    		
    		StringBuffer rcmd = new StringBuffer();
    		rcmd.append(remoteCommand);
    		rcmd.append(" ");
    		rcmd.append(remoteDir);
    		rcmd.append(remoteFile);
    		
    		System.out.println("RCMD IS: " + rcmd);
    		
    	
    		IgnoreHostKeyVerification hostKey = new IgnoreHostKeyVerification();
            SshClient ssh = new SshClient();
            ssh.connect(server, 22, hostKey);
            //Authenticate
            PasswordAuthenticationClient passwordAuthenticationClient = new PasswordAuthenticationClient();
            passwordAuthenticationClient.setUsername(userName);
            passwordAuthenticationClient.setPassword(password);
            int result = ssh.authenticate(passwordAuthenticationClient);
            if(result != AuthenticationProtocolState.COMPLETE){
                    System.out.println("Login to " + server + ":" + " " + userName + "/" + password + " failed");
            }
            
            SessionChannelClient sshChannel = new SessionChannelClient();
            sshChannel = ssh.openSessionChannel();
            sshChannel.executeCommand(rcmd.toString());
            BufferedReader reader = new BufferedReader(new InputStreamReader(sshChannel.getInputStream()));
            System.out.println("READER IS: " + reader.readLine());
    
    		String currentLine;
    		
    		Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    		System.out.println("PRIOTIRY IS: " + Thread.currentThread().getPriority());
    		while((currentLine = reader.readLine()) != null)
    		{
    			System.out.println("YOU ARE IN LISTEN");
    		}
    		
    		sshChannel.close();
            ssh.disconnect();
    
    	}//listen

    Thanks very much for your help!!!
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Why can't you implements lock on the files?
    Check this link.
    Check this link too.

    Regards
    Dheeraj Joshi
    Last edited by Dheeraj Joshi; Feb 15 '11, 06:40 AM. Reason: Adding links.

    Comment

    • ndedhia1
      New Member
      • Jan 2009
      • 112

      #3
      When and how do i use the filelock? By the time i get to the line
      Code:
      System.out.println("READER IS: " + reader.readLine());
      the value is null, which means that my while loop will never be true.

      thanks again for the help!!

      Comment

      • ndedhia1
        New Member
        • Jan 2009
        • 112

        #4
        Hi Dheeraj,
        Thanks for your help. I was using the wrong cattail command and once i fixed that, i got it to work.

        thanks

        Comment

        Working...