Listening for events

Top  Previous  Next

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

 

1. Set object to implement ImapListener or extend ImapAdapter class.

2. Overload event handling methods.

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

 

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

 

Example

 

The example below demonstrates using the ImapListener class.

 

import com.jscape.inet.imap.*;

 

public class MyImapListener implements ImapListener {

 

 public void connected(ImapConnectedEvent event) {

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

 }

 

 public void disconnected(ImapDisconnectedEvent event) {

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

 

 }

 

 public void messageRetrieved(ImapMessageEvent event) {

         // process event

 

 }

 

 public void commandSent(ImapCommandEvent event) {

         //  process event

 

 }

 

 public void responseReceived(ImapResponseEvent event) {

         // process event

 

 }

 

 public static void main(String[] args) {

         try {

                 // create new Imap instance

                 Imap imap = new Imap("imap.myserver.com","jsmith","secret");

                 

                 // register listener for Imap instance

                 imap.addImapListener(new MyImapListener());

                 

                 // establish connection

                 imap.connect();

                 

                 // release connection

                 imap.disconnect();                        

         } catch(Exception e) {

                 e.printStackTrace();

         }

 }

 

}

 

 

Example

 

The example below demonstrates using the ImapAdapter class.

 

import com.jscape.inet.imap.*;

 

public class MyImapAdapter extends ImapAdapter {        

 

 public void connected(ImapConnectedEvent event) {

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

 }

 

 public void disconnected(ImapDisconnectedEvent event) {

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

 }        

 

 public static void main(String[] args) {

         try {

                 // create new Imap instance

                 Imap imap = new Imap("imap.myserver.com","jsmith","secret");

                 

                 // register listener for Imap instance

                 imap.addImapListener(new MyImapAdapter());

                 

                 // establish connection

                 imap.connect();

                 

                 // release connection

                 imap.disconnect();                        

         } catch(Exception e) {

                 e.printStackTrace();

         }

 }

}

 

 





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

© 2021 JSCAPE LLC