为 Apache Geode Client 设置超时连接

Set up a connection that times out for Apache Geode Client

我在 .NET Framework 上有一个 windows 桌面应用程序,用户可以与之交互。有一个“连接”按钮可以设置 Apache Geode 客户端与 Geode 服务器的连接。

当 Geode 服务器关闭时(定位器 - 只有 1 个)桌面应用程序无限期挂起,需要强行关闭。

这就是我们连接到服务器并无限期挂在最后一行的方式:

                PoolFactory poolFactory = _cache.GetPoolManager()
                .CreateFactory()
                .SetSubscriptionEnabled(true)
                .AddLocator(_host, _port);

            return poolFactory.Create(_serviceName);

我想为这个方法添加一个超时时间 return 或抛出异常或任何东西。

或者,我将它包装在除此之外的另一种计时器上,仅 return,但是当再次尝试 运行 上述代码时(尝试再次连接应用程序) 池仍在尝试连接。 我试过这样摧毁它:

              //destroy the pool
            var poolManager = _cache.GetPoolManager();
            if (poolManager != null)
            {
                var pool = poolManager.Find(_serviceName);
                if (pool != null && pool.Destroyed == false)
                    pool.Destroy();
            }

但是 pool.Destroy(); 不确定地挂起

我怎样才能有一种机制来尝试连接到 Geode 服务器,return 如果在 10 秒内没有连接,然后离开并能够使用相同的缓存再次尝试连接?

上面的注意事项是尝试重新使用相同的缓存,可能将缓存设置为 null 并重新尝试,但我正在寻找更正确的方法。

取决于您使用的 .NET 本机客户端版本。 10.x connect-timeout 中的 属性 应该有效。此外,您可以将连接代码包装在 try{...}catch{} 块下并处理 Apache.Geode.Client.TimeoutException and/or ": No locators available".

的异常

示例:

    var _cache = new CacheFactory()
                        .Set("log-level", "config")
                        .Set("log-file", "C:\temp\test.log")
                        .Set("connect-timeout", "26000ms")
                        .Create();

    _cache.GetPoolManager()
        .CreateFactory()
        .SetSubscriptionEnabled(true)
        .AddLocator(hostname-or-ip", port-number)
        .Create("TestPool");