Listening for events

Top  Previous  Next

For an object to listen for events published by the Sftp class the following steps are required:

 

1. Set object to implement SftpListener or extend SftpAdapter class.

2. Overload event handling methods.

3. Subscribe object to receive events published by Sftp instance.

 

The SftpListener class is a pure interface and can be used in cases where the class you defined to listen for events already extends another class type. The SftpAdapter class implements the SftpListener interface and can be used in cases where there is no need for class inheritance.

 

Note

 

Unless your class requires inheritance it is generally easier to use the SftpAdapter class as it provides default implementations for all the event handler methods defined in the SftpListener interface. This allows you to overload only the event handler methods that you are interested in.

 

Example

 

The example below demonstrates using the SftpListener class.

 

import com.jscape.inet.sftp.*;

import com.jscape.inet.sftp.events.*;

import com.jscape.inet.ssh.util.SshParameters;

 

public class MySftpListener implements SftpListener {

 

public void connected(SftpConnectedEvent event) {

   System.out.println("Connected to host: " + event.getHostname());

 

 }

 

public void disconnected(SftpDisconnectedEvent event) {

   System.out.println("Disconnected from host: " + event.getHostname());

 

 }

 

public void download(SftpDownloadEvent event) {

  // process event

 }

 

public void upload(SftpUploadEvent event) {

  //    process event

 }

 

public void progress(SftpProgressEvent event) {

  //    process event

 }

 

public void dirListing(SftpListingEvent event) {

  //    process event

 }

public static void main(String[] args) {

  try {

     String hostname = "ftp.host.com";

     String username = "jsmith";

     String password = "secret";

 

    // create connection parameters

     SshParameters sshParams = new SshParameters(hostname,username,password);

     Sftp sftp = new Sftp(sshParams);

   

    // subscribe listener to published SFTP events

     sftp.addSftpListener(new MySftpListener());

   

    // connect then disconnect

     sftp.connect();

     sftp.disconnect();

   

   } catch(SftpException e) {

     e.printStackTrace();

   }  

 }

}

 

Example

 

The example below demonstrates using the SftpAdapter class.

 

import com.jscape.inet.sftp.*;

import com.jscape.inet.sftp.events.*;

import com.jscape.inet.ssh.util.SshParameters;

 

public class MySftpAdapter extends SftpAdapter {

public void connected(SftpConnectedEvent evt) {

   System.out.println("Connected to host: " + evt.getHostname());

 }

public void disconnected(SftpDisconnectedEvent evt) {

   System.out.println("Disconnected from host: " + evt.getHostname());

 }

public static void main(String[] args) {

  try {

     String hostname = "ftp.host.com";

     String username = "jsmith";

     String password = "secret";

 

    // create connection parameters

     SshParameters sshParams = new SshParameters(hostname,username,password);

     Sftp sftp = new Sftp(sshParams);

   

    // subscribe listener to published SFTP events

     sftp.addSftpListener(new MySftpAdapter());

   

    // connect then disconnect

     sftp.connect();

     sftp.disconnect();

   

   } catch(SftpException e) {

     e.printStackTrace();

   }  

 }

}

 

 





Home | Company | Products | Solutions | Purchase | Support | Services | Blog

© 2021 JSCAPE LLC