JAVA MAIL之简单事件

JAVA MAIL之简单事件

JAVA MAIL 中包含了连接、计数等事件机制,下面的对连接事件的一个简单实现:
1、事件基类:
package  myevent;

import  java.util.EventObject;

public   abstract   class  MyMailEvent  extends  EventObject  {
    
private static final long serialVersionUID = 2183013741510840197L;

    
public MyMailEvent(Object source) {
        
super(source);
    }

    
    
public abstract void dispatch(Object listener);
}


2、连接事件类:
package  myevent;

public   class  MyConnEvent  extends  MyMailEvent  {
    
public static final int OPENED = 1;
    
public static final int CLOSED = 2;
    
protected int type;
    
private static final long serialVersionUID = 6177016117289759191L;

    
public MyConnEvent(Object source, int type) {
        
super(source);
        
this.type = type;
    }


    @Override
    
public void dispatch(Object listener) {
        
switch (type) {
        
case OPENED:
            ((MyConnListener) listener).opened(
this);
            
break;
        
case CLOSED:
            ((MyConnListener) listener).closed(
this);
        
default:
            
break;
        }


    }


    
public int getType() {
        
return type;
    }

}


3、连接事件侦听类:
package  myevent;

import  java.util.EventListener;

public   interface  MyConnListener  extends  EventListener  {
    
public void opened(MyConnEvent e);

    
public void closed(MyConnEvent e);
}


4、连接事件适配器类:
package  myevent;

/** */ /** 采用Adapter的为了使用时只需重载部分方法 */
public   abstract   class  MyConnAdapter  implements  MyConnListener  {
    
public void opened(MyConnEvent e) {

    }


    
public void closed(MyConnEvent e){
        
    }

}


5、测试:
package  myevent;

import  java.util.ArrayList;
import  java.util.List;

public   class  EventTest  {
    
private List<MyConnListener> conns = new ArrayList<MyConnListener>();

    
public void addConnListener(MyConnListener lis) {
        conns.add(lis);
    }


    
public void notifyConn() {
        List
<MyMailEvent> list = new ArrayList<MyMailEvent>();
        list.add(
new MyConnEvent("source1", MyConnEvent.OPENED));
        list.add(
new MyConnEvent("source2", MyConnEvent.OPENED));
        list.add(
new MyConnEvent("source3", MyConnEvent.CLOSED));

        
for (MyMailEvent e : list) {
            
for (MyConnListener inst : conns) {
                e.dispatch(inst);
            }

        }

    }


    
public static void main(String[] args) {
        EventTest test 
= new EventTest();
        
// 只侦听open事件
        test.addConnListener(new MyConnAdapter() {
            
public void opened(MyConnEvent e) {
                System.out.println(e.getSource() 
+ ":listener 1 after open");
            }

        }
);
        
// 只侦听close事件
        test.addConnListener(new MyConnAdapter() {
            
public void closed(MyConnEvent e) {
                System.out.println(e.getSource() 
+ ":listener 2 after close");
            }

        }
);
        
// open、close都侦听
        test.addConnListener(new MyConnAdapter() {
            
public void opened(MyConnEvent e) {
                System.out.println(e.getSource() 
+ ":listener 3 after open");
            }


            
public void closed(MyConnEvent e) {
                System.out.println(e.getSource() 
+ ":listener 3 after close");
            }

        }
);
        test.notifyConn();
    }

}

输出:
source1:listener  1  after open
source1:listener 
3  after open
source2:listener 
1  after open
source2:listener 
3  after open
source3:listener 
2  after close
source3:listener 
3  after close

6、 点击下载代码

你可能感兴趣的:(JAVA MAIL之简单事件)