当与Apache Ignite C# thick client建立连续查询时,如果客户端与服务器的连接意外中断,则需要重新连接并恢复查询。以下是一个示例代码,显示如何在重新连接后恢复查询。
// 创建一个新的ThinClient连接
using (var ignite = Ignition.Start(IsDebug ? GetIgniteConfiguration() : GetIgniteConfiguration().SetGridName(AppName)))
{
// 获取缓存
var cache = ignite.GetCache(CacheName);
// 创建一个新的连续查询过滤器
var fltr = new ContinuousQuerySingleFilter(e => e.Value.Name.StartsWith(FilterStarter));
// 创建一个新的连续查询
var qry = new ContinuousQuery(cache, fltr);
// 在新线程上侦听新的查询事件
Task.Factory.StartNew(() =>
{
foreach (var change in qry.GetInitialQueryCursor())
{
ProcessChange(change);
}
foreach (var change in qry.GetContinuousQueryCursor())
{
ProcessChange(change);
}
});
// 循环直到收到关闭信号
while (!Token.IsCancellationRequested)
{
// 检查连接是否有效
if (!ignite.GetCluster().IsActive())
{
Console.WriteLine("Cluster is not active. Trying to reconnect...");
// 创建一个新的ThinClient连接
ignite.Dispose();
ignite = Ignition.Start(IsDebug ? GetIgniteConfiguration() : GetIgniteConfiguration().SetGridName(AppName));
// 获取缓存
cache = ignite.GetCache(CacheName);
// 需要重新创建和注册过滤器才能从Ignite服务器检索数据。
// 我们将存储相同的过滤器作为一个缓存变量,并在重新连接时使用它。
fltr = new ContinuousQuerySingleFilter(e => e.Value.Name.StartsWith(FilterStarter));
qry = new ContinuousQuery(cache, fl