Listening for events |
For an object to listen for events published by the Ftp class 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 Ftp 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.*;
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 { Ftp ftp = new Ftp("ftp.myserver.com", "jsmith", "secret");
// subscribe MyFtpListener to events published ftp.addFtpListener(new MyFtpListener()); ftp.connect(); ftp.disconnect(); } catch (Exception e) { e.printStackTrace(); } } }
Example
The example below demonstrates using the FtpAdapter class.
import com.jscape.inet.ftp.*;
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 { Ftp ftp = new Ftp("ftp.myserver.com","jsmith","secret");
// subscribe MyFtpAdapter to events published ftp.addFtpListener(new MyFtpAdapter()); ftp.connect(); ftp.disconnect(); } catch(Exception e) { e.printStackTrace(); } } }
|