Listening for events

Top  Previous  Next

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

 

1. Set object to implement NntpListener or extend NntpAdapter class.

2. Overload event handling methods.

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

 

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

 

Example

 

The example below demonstrates using the NntpListener class.

 

import com.jscape.inet.nntp.*;

 

public class MyNntpListener implements NntpListener {

 

 public void connected(NntpConnectedEvent event) {

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

 }

 

 public void disconnected(NntpDisconnectedEvent event) {

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

 }

 

 public void articlePosted(NntpArticlePostedEvent event) {

         // process event

 }

 

 public void articleRetrieved(NntpArticleEvent event) {

         // process event

 }

 

 public void articleHeaderRetrieved(NntpArticleHeaderEvent event) {

         // process event

 }

 

 public void progress(NntpProgressEvent event) {

         // process event

 }

 

 public static void main(String[] args) {

         try {

                 // create new Nntp instance

                 Nntp nntp = new Nntp("news.server.com");

                 

                 // register listener

                 nntp.addNntpListener(new MyNntpListener());

                 

                 // connect

                 nntp.connect();

                 

                 // disconnect

                 nntp.disconnect();                        

         } catch(Exception e) {

                 e.printStackTrace();

         }        

 }

}

 

 

 

Example

 

The example below demonstrates using the NntpAdapter class.

 

import com.jscape.inet.nntp.*;

 

public class MyNntpAdapter extends NntpAdapter {

 

 public void connected(NntpConnectedEvent event) {

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

 }

 

 public void disconnected(NntpDisconnectedEvent event) {

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

 }        

 

 public static void main(String[] args) {

         try {

                 // create new Nntp instance

                 Nntp nntp = new Nntp("news.server.com");

                 

                 // register listener

                 nntp.addNntpListener(new MyNntpAdapter());

                 

                 // connect

                 nntp.connect();

                 

                 // disconnect

                 nntp.disconnect();                        

         } catch(Exception e) {

                 e.printStackTrace();

         }        

 }

}





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

© 2021 JSCAPE LLC