• cometd源码阅读Listener监听器(十一)


    接口定义

    java.util.EventListener

    /**
     * A tagging interface that all event listener interfaces must extend.
     * @since JDK1.1
     */
    public interface EventListener {
    }

    org.cometd.bayeux.Bayeux.BayeuxListener

        /**
         * <p>The common base interface for Bayeux listeners.</p>
         * <p>Specific sub-interfaces define what kind of events listeners will be notified.</p>
         */
        interface BayeuxListener extends EventListener {
        }

    org.cometd.bayeux.Bayeux.BayeuxListener

        interface BayeuxServerListener extends BayeuxListener {
        }

    监听器实现

    ChannelListener

    使用

    BayeuxServer bayeuxServer = ...;
    bayeuxServer.addListener(new ChannelListener(){....});
    bayeuxServer.addListener(new ChannelListener(){....});

    接口定义

        public interface ChannelListener extends BayeuxServerListener, ConfigurableServerChannel.Initializer {
            /**
             * <p>Callback invoked when a {@link ServerChannel} has been added to a {@link BayeuxServer} object.</p>
             * <源码处>channel初始化成功
             * @param channel the channel that has been added
             */
            public default void channelAdded(ServerChannel channel) {
            }
    
            /**
             * <p>Callback invoked when a {@link ServerChannel} has been removed from a {@link BayeuxServer} object.</p>
             * <源码处>channel初始化移除成功
             * @param channelId the channel identifier of the channel that has been removed.
             */
            public default void channelRemoved(String channelId) {
            }
        }

    SessionListener

    使用

    BayeuxServer bayeuxServer = ...;
    bayeuxServer.addListener(new SessionListener(){....});
    bayeuxServer.addListener(new SessionListener(){....});

    接口定义

    public interface SessionListener extends BayeuxServerListener {
            /**
             * <p>Callback invoked when a {@link ServerSession} has been added to a {@link BayeuxServer} object.</p>
             * <源码调用处>握手成功 内部session成功
             * @param session the session that has been added
             * @param message the handshake message from the client
             */
            public default void sessionAdded(ServerSession session, ServerMessage message) {
            }
    
            /**
             * <p>Callback invoked when a {@link ServerSession} has been removed from a {@link BayeuxServer} object.</p>
             *<源码调用处> session移除后监听回调 比如我们手动调用remove 或者超时自动剔除
             * @param session the session that has been removed
             * @param message the message that caused the session removal, or null
             * @param timeout whether the session has been removed due to a timeout 可以判断是否是超时剔除
             */
            public default void sessionRemoved(ServerSession session, ServerMessage message, boolean timeout) {
                sessionRemoved(session, timeout);
            }
    
            /**
             * <p>Callback invoked when a {@link ServerSession} has been removed from a {@link BayeuxServer} object.</p>
             *
             * @param session the session that has been removed
             * @param timeout whether the session has been removed for a timeout or not
             * @deprecated use {@link #sessionRemoved(ServerSession, ServerMessage, boolean)} instead
             */
            @Deprecated
            public default void sessionRemoved(ServerSession session, boolean timeout) {
            }
        }

    SubscriptionListener

    全局使用

    BayeuxServer bayeuxServer = ...;
    bayeuxServer.addListener(new SubsriptionListener(){....});
    bayeuxServer.addListener(new SubscriptionListener(){....});

    channel级 利用ChannelListener全局监听器实现

          /**
             * <p>Callback invoked when a {@link ServerChannel} has been added to a {@link BayeuxServer} object.</p>
             * channel初始化成功
             * @param channel the channel that has been added
             */
            public void channelAdded(ServerChannel channel) {
                if(channel.isService()){
                    channel.addListener(new ServerChannel.SubscriptionListener() {
                        @Override
                        public void subscribed(ServerSession session, ServerChannel channel, ServerMessage message) {
    
                        }
    
                        @Override
                        public void unsubscribed(ServerSession session, ServerChannel channel, ServerMessage message) {
    
                        }
                    });
                }
            }

    接口定义

      public interface SubscriptionListener extends BayeuxServerListener {
            /**
             * <p>Callback invoked when a {@link ServerSession} subscribes to a {@link ServerChannel}.</p>
             * <源码处>订阅指定渠道
             * @param session the session that subscribes
             * @param channel the channel to subscribe to
             * @param message the subscription message sent by the client, or null in case of
             *                server-side subscription via {@link ServerChannel#subscribe(ServerSession)}
             */
            public default void subscribed(ServerSession session, ServerChannel channel, ServerMessage message) {
            }
    
            /**
             * <p>Callback invoked when a {@link ServerSession} unsubscribes from a {@link ServerChannel}.</p>
             * <源码处>取消订阅指定渠道
             * @param session the session that unsubscribes
             * @param channel the channel to unsubscribe from
             * @param message the unsubscription message sent by the client, or null in case of
             *                server-side unsubscription via {@link ServerChannel#unsubscribe(ServerSession)}
             */
            public default void unsubscribed(ServerSession session, ServerChannel channel, ServerMessage message) {
            }
        }
  • 相关阅读:
    事务/数据库操作之事务,开启回滚提交
    python连接数据库、cursor fetch语句处理
    order by 排序输出、插入
    表的查询,
    android权限大全
    uniapp ios端证书打包+开发环境
    uniapp在app端白屏报错
    数字递增动画js插件-countUp.js
    窗口滚动时,判断元素与视野的关系-js代码
    Linux安装Nginx并配置站点
  • 原文地址:https://www.cnblogs.com/LQBlog/p/16737737.html
Copyright © 2020-2023  润新知