Listening for events

Top  Previous  Next

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

 

1. Set object to implement PopListener or extend PopAdapter class.

2. Overload event handling methods.

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

 

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

 

Example

 

The example below demonstrates using the PopListener class.

 

import com.jscape.inet.pop.*;

 

public class MyPopListener implements PopListener {

 

 public void connected(PopConnectedEvent event) {

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

 }

 

 public void disconnected(PopDisconnectedEvent event) {

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

 }

 

 public void messageRetrieved(PopMessageEvent event) {

         // process event

 }

 

 public void commandSent(PopCommandEvent event) {

         // process event

 }

 

 public void responseReceived(PopResponseEvent event) {

         // process event

 }

 

 public static void main(String[] args) {

         try {

                 // create new Pop instance

                 Pop pop = new Pop("pop.myserver.com","jsmith","secret");

                 

                 // register listener for Pop instance

                 pop.addPopListener(new MyPopListener());

                 

                 // establish connection

                 pop.connect();

                 

                 // release connection

                 pop.disconnect();                        

         } catch(Exception e) {

                 e.printStackTrace();

         }

 }

}

 

 

Example

 

The example below demonstrates using the PopAdapter class.

 

import com.jscape.inet.pop.*;

 

public class MyPopAdapter extends PopAdapter {

 

 public void connected(PopConnectedEvent event) {

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

 }

 

 public void disconnected(PopDisconnectedEvent event) {

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

 }        

 

 public static void main(String[] args) {

         try {

                 // create new Pop instance

                 Pop pop = new Pop("pop.myserver.com","jsmith","secret");

                 

                 // register listener for Pop instance

                 pop.addPopListener(new MyPopAdapter());

                 

                 // establish connection

                 pop.connect();

                 

                 // release connection

                 pop.disconnect();                        

         } catch(Exception e) {

                 e.printStackTrace();

         }

 }

}

 

 

 

 





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

© 2021 JSCAPE LLC