Listening for events

Top  Previous  Next

For an object to listen for events published by the Ftps component the following steps are required:

 

1. Set object to implement FtpListener or extend FtpAdapter class.

 

2. Overload event handling methods.

 

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

 

The FtpListener 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 FtpAdapter class implements the FtpListener 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 FtpAdapter class as it provides default implementations for all the event handler methods defined in the FtpListener interface. This allows you to overload only the event handler methods that you are interested in.

 

Example

 

The example below demonstrates using the FtpListener class.

 

import com.jscape.inet.ftp.*;

import com.jscape.inet.ftps.*;

 

public class MyFtpListener implements FtpListener {

 

public void commandSent(FtpCommandEvent evt) {

  // process event

 }

 

public void connected(FtpConnectedEvent evt) {

  // capture connected event and print info to console

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

 }

 

public void disconnected(FtpDisconnectedEvent evt) {

  // capture disconnected event and print info to console

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

 }

 

public void download(FtpDownloadEvent evt) {

  // process event

 }

 

public void listing(FtpListingEvent evt) {

  // process event

 }

 

public void progress(FtpProgressEvent evt) {

  // process event

 }

 

public void responseReceived(FtpResponseEvent evt) {

  // process event

 }

 

public void upload(FtpUploadEvent evt) {

  // process event  

 }

public void connectionLost(FtpConnectionLostEvent evt) {

  // process event

 }

 

public static void main(String[] args) {

  try {

     Ftps ftps = new Ftps("ftp.myserver.com", "jsmith", "secret");

 

    // subscribe MyFtpListener to events published

     ftps.addFtpListener(new MyFtpListener());

     ftps.connect();

     ftps.disconnect();

   } catch (Exception e) {

     e.printStackTrace();

   }

 }

}

 

Example

 

The example below demonstrates using the FtpAdapter class.

 

import com.jscape.inet.ftp.*;

import com.jscape.inet.ftps.*;

 

public class MyFtpAdapter extends FtpAdapter {

 

public void connected(FtpConnectedEvent evt) {

  // capture connected event

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

 }

public void disconnected(FtpDisconnectedEvent evt) {

  // capture disconnected event

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

 }

public static void main(String[] args) {

  try {

     Ftps ftps = new Ftps("ftp.myserver.com","jsmith","secret");

 

    // subscribe MyFtpAdapter to events published

     ftps.addFtpListener(new MyFtpAdapter());  

     ftps.connect();

     ftps.disconnect();

   }

  catch(Exception e) {

     e.printStackTrace();

   }

 }

}

 

 





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

© 2021 JSCAPE LLC