Curator源码阅读 - ConnectionState的管理与监听

本文深入探讨了Curator框架中对连接状态的管理与监听机制,包括2.13版本的接口Listener、Listenable和ConnectionStateManager的使用,以及4.2.0版本的改进,如ConnectionStateListenerDecorator接口和CircuitBreakingConnectionStateListener装饰器,用于在网络不稳定时避免频繁触发监听。同时,文章还介绍了ListenerContainer的重构,引入了ListenerManager以增强功能。通过对源码的分析,揭示了Curator在监听和管理方面的设计模式和技巧,如生产者消费者模式和装饰器模式的应用。
摘要由CSDN通过智能技术生成

查看Curator框架 为实现对 连接状态ConnectionState的管理与监听是怎么构造的。后面我们也可以应用到业务的各种监听中。

Curator2.13实现

接口 Listener

Listener接口,给用户实现stateChange()传入新的状态,用户实现对这新的状态要做什么逻辑处理。

public interface ConnectionStateListener
{
   
    /**
     * Called when there is a state change in the connection
     * @param client the client
     * @param newState the new state
     */
    public void stateChanged(CuratorFramework client, ConnectionState newState);
}

接口 Listenable

提供一个监听对象容器的接口

// Abstracts a listenable object
public interface Listenable<T>
{
   
    /**
     * Add the given listener. The listener will be executed in the containing instance's thread.
     *
     * @param listener listener to add
     */
    public void     addListener(T listener);

    public void     addListener(T listener, Executor executor);

    public void     removeListener(T listener);
}

ListenerContainer<T> implements Listenable<T>

/**
 * Abstracts an object that has listeners 装Listener的容器
 * <T> Listener类型
 */
public class ListenerContainer<T> implements Listenable<T>
{
   
    private final Map<T, ListenerEntry<T>> listeners = Maps.newConcurrentMap();

    @Override
    public void addListener(T listener)
    {
   
        addListener(listener, MoreExecutors.sameThreadExecutor());
    }

    @Override
    public void addListener(T listener, Executor executor)
    {
   
        listeners.put(listener, new ListenerEntry<T>(listener, executor));
    }
    
    /**
     * 对 Listener 列表的遍历进行封装
     * Utility - apply the given function to each listener. 
     * @param function function to call for each listener
     */
    public void forEach(final Function<T, Void> function)
    {
   
        for ( final ListenerEntry<T> entry : listeners.values() )
        {
   
            entry.executor.execute
            (
                new Runnable()
                {
   
                    @Override
                    public void run()
                    {
   
                        try
                        {
   
                            function.apply(entry.listener);
                        }
                        catch ( Throwable e )
                        {
   
                            ThreadUtils.checkInterrupted(e);
                            log.error(String.format("Listener (%s) threw an exception", entry.listener), e);
                        }
                    }
                }
            );
        }
    }
    
    
    public void clear()
    {
   
        listeners.clear();
    }

    public int size(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值