PipeStateListener和PipeEventListener

先看二者的定义
/**
 *  The listener interface for receiving JxtaBiDiPipe
 *  events.
 *
 *  The following example illustrates how to implement a {@link net.jxta.util.PipeStateListener}:
 *<pre><tt>
 * PipeStateListener myListener = new PipeStateListener() {
 *
 *   public void pipeEvent(int event) {
 *        if (event == PipeStateListener.PIPE_CLOSED_EVENT) {
 *          .....
 *        }
 *   }
 * }
 *
 * InputPipe pipeIn = pipe.createInputPipe(pipeAdv, myListener);
 * </tt></pre>
 *
 */
public interface PipeStateListener extends EventListener {

    /**
     * Pipe close Event
     */
    public static final int PIPE_CLOSED_EVENT = 1;

    /**
     * Pipe opened Event
     */
    public static final int PIPE_OPENED_EVENT = 2;

    /**
     * Pipe failed Event
     */
    public static final int PIPE_FAILED_EVENT = 4;

    /**
     * Called for each pipe mode event that occurs.
     *
     * @param source the source of the event (JxtaBiDiPipe)
     * @param event The event being received.
     */
    void stateEvent(Object source, int event);

}

/**
 *  The listener interface for receiving JxtaBiDiPipe
 *  events.
 *
 *  The following example illustrates how to implement a {@link net.jxta.util.PipeEventListener}:
 *<pre><tt>
 * PipeEventListener myListener = new PipeEventListener() {
 *
 *   public void pipeEvent(int event) {
 *        if (event == JxtaBiDiPipe.PIPE_CLOSED_EVENT) {
 *          .....
 *        }
 *   }
 * }
 *
 * InputPipe pipeIn = pipe.createInputPipe(pipeAdv, myListener);
 * *</tt></pre>
 *
 */
public interface PipeEventListener extends EventListener {

    /**
     * Called for each pipe message event that occurs.
     *
     * @param event The event being received.
     */
    void pipeEvent(int event);

}
上述两个监听器都用来监听管道事件。从字面上看,一个用于监听管道状态(实际上也是事件,),一个用于监听管道事件。
在PipeStateListener中定义了三种管道事件,分别是
管道关闭事件/管道打开事件/管道失败事件(该事件目前没有使用)
同时注意到JxtaBiDiPipe类中也定义了一个管道关闭事件
/**
* Pipe close Event
*/
public static final int PIPE_CLOSED_EVENT = 1;


再看JxtaBiDiPipe的notifyListeners方法,该方法会在适当的时候通知监听器。
    private void notifyListeners(int event) {
        try {
            if (eventListener != null) {
                eventListener.pipeEvent(event);
            } else if (stateListener != null) {
                stateListener.stateEvent(this, event);
            }
        } catch (Throwable th) {
            if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
                LOG.log(Level.FINE, "error during pipe event callback", th);
            }
        }
    }
从该方法的定义中可以看出,如果注册了事件监听器(PipeEventListener),则会通知事件监听器,否则才会通知状态监听器(PipeStateListener)。
因此,如果二者都注册了的话,只有事件监听器会收到通知,而状态监听器是不会收到任何通知的。

在JxtaBiDiPipe类中共有三处调用了notifyListeners方法(2.7中,2.5、2.6中只有两处),其中
一处用于管道异常关闭:
//Pipe broken, just notify close and deal later...
pipe.notifyListeners(PipeStateListener.PIPE_CLOSED_EVENT);

一处用于管道正常关闭(通过调用close方法):
notifyListeners(PIPE_CLOSED_EVENT);

一处用于管道打开事件:
notifyListeners(PipeStateListener.PIPE_OPENED_EVENT);


第一处和第三处使用的是PipeStateListener定义的事件,而第二处使用的是JxtaBiDiPipe类中定义的事件。
注意PipeStateListener和JxtaBiDiPipe虽然都定义了PIPE_CLOSED_EVENT,但二者的值相同,都是1。

总结:在管道事件的监听方面,目前的JXSE的实现有些混乱。实际处理的时候只要注册其中一个监听器即可。而PipeStateListener定义的事件比较全,所以可以使用PipeStateListener中定义的事件进行处理,PipeStateListener的回调函数中还多了一个事件源的信息,所以建议统一使用PipeStateListener,而忽略PipeEventListener。







posted on 2011-10-18 12:29  网络大豆  阅读(320)  评论(0编辑  收藏  举报