当前位置: 首页>>代码示例>>C#>>正文


C# Routee.Select方法代码示例

本文整理汇总了C#中Akka.Routing.Routee.Select方法的典型用法代码示例。如果您正苦于以下问题:C# Routee.Select方法的具体用法?C# Routee.Select怎么用?C# Routee.Select使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Akka.Routing.Routee的用法示例。


在下文中一共展示了Routee.Select方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Select

        /// <summary>
        /// Picks a <see cref="Routee" /> to receive the <paramref name="message" />.
        /// </summary>
        /// <param name="message">The message that is being routed</param>
        /// <param name="routees">A collection of routees to choose from when receiving the <paramref name="message" />.</param>
        /// <returns>A <see cref="Routee" /> that receives the <paramref name="message" />.</returns>
        public override Routee Select(object message, Routee[] routees)
        {
            if (message == null || routees == null || routees.Length == 0)
                return Routee.NoRoutee;

            Func<ConsistentHash<ConsistentRoutee>> updateConsistentHash = () =>
            {
                // update consistentHash when routees are changed
                // changes to routees are rare when no changes this is a quick operation
                var oldConsistHashTuple = _consistentHashRef.Value;
                var oldRoutees = oldConsistHashTuple.Item1;
                var oldConsistentHash = oldConsistHashTuple.Item2;

                if (oldRoutees == null || !routees.SequenceEqual(oldRoutees))
                {
                    // when other instance, same content, no need to re-hash, but try to set routees
                    var consistentHash = routees == oldRoutees
                        ? oldConsistentHash
                        : ConsistentHash.Create(routees.Select(x => new ConsistentRoutee(x, _selfAddress)), _vnodes);
                    //ignore, don't update, in case of CAS failure
                    _consistentHashRef.CompareAndSet(oldConsistHashTuple, Tuple.Create(routees, consistentHash));
                    return consistentHash;
                }
                return oldConsistentHash;
            };

            Func<object, Routee> target = hashData =>
            {
                try
                {
                    var currentConsistentHash = updateConsistentHash();
                    if (currentConsistentHash.IsEmpty) return Routee.NoRoutee;
                    else
                    {
                        if (hashData is byte[])
                            return currentConsistentHash.NodeFor(hashData as byte[]).Routee;
                        if (hashData is string)
                            return currentConsistentHash.NodeFor(hashData as string).Routee;
                        return
                            currentConsistentHash.NodeFor(
                                _system.Serialization.FindSerializerFor(hashData).ToBinary(hashData)).Routee;
                    }
                }
                catch (Exception ex)
                {
                    //serialization failed
                    _log.Value.Warning("Couldn't route message with consistent hash key [{0}] due to [{1}]", hashData,
                        ex.Message);
                    return Routee.NoRoutee;
                }
            };

            if (_hashMapping(message) != null)
            {
                return target(ConsistentHash.ToBytesOrObject(_hashMapping(message)));
            }
            else if (message is IConsistentHashable)
            {
                var hashable = (IConsistentHashable) message;
                return target(ConsistentHash.ToBytesOrObject(hashable.ConsistentHashKey));
            }
            else
            {
                _log.Value.Warning("Message [{0}] must be handled by hashMapping, or implement [{1}] or be wrapped in [{2}]",
                    message.GetType().Name, typeof (IConsistentHashable).Name, typeof (ConsistentHashableEnvelope).Name);
                return Routee.NoRoutee;
            }
        }
开发者ID:Micha-kun,项目名称:akka.net,代码行数:74,代码来源:ConsistentHashRouter.cs


注:本文中的Akka.Routing.Routee.Select方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。