ServiceStack.Mq: Why isn't the IRedisSubscription.OnMessage fired when manually adding data to the channel?

一笑奈何 提交于 2021-01-29 05:43:16

问题


I am using ServiceStack and the IRedisSubscriber. I have gotten it to work, triggering the OnMessage. However, sometimes it does not trigger, and I am trying to figure out why.

The basic setup is:

        RedisClientManager = new PooledRedisClientManager("localhost:6379");

        _mqServer = new RedisMqServer(RedisClientManager, retryCount: 2)
        {
            RequestFilter = RequestFilter
        };

        _mqServer.Start(); //Starts listening for messages

In a separate class, I have:

    public MqChannelSubscriber(string eventChannelName, Action<CoreRequest> onMessageReceived)
    {
        _redisClient = MqClientFactory.Instance.GetRedisClient();
        _subscription = _redisClient.CreateSubscription();

        _subscription.OnSubscribe = channel => Log.Instance.LogInfo($"Subscription started on {eventChannelName}");
        _subscription.OnUnSubscribe = channel => Log.Instance.LogWarning($"Unsubscribed from {eventChannelName}");

        _subscription.OnMessage = (channel, msg) =>
        {
            try
            {
                onMessageReceived(GetRequest(msg));
            }
            catch (Exception ex)
            {
                Log.Instance.LogException(ex);
            }
        };
        Task.Run(() => _subscription.SubscribeToChannels(eventChannelName));
    }

In this case, the eventChannelName is "ObjectBroadcast".

Question 1: If I manually, using for example Redis Insight, add a new key called "ObjectBroadcast" of type List and add one entry, I would expect the OnMessage to be fired above, but it is not. Why is that?

Question 2: I have another application that produces these "broadcasts", by doing this:

    public static void Broadcast<T>(T coreBroadcast) where T : CoreBroadcast
    {
        // Option 1: this will trigger the OnMessage above
        using (var redisClient = MqClientFactory.Instance.GetRedisClient())
        {
            string json = JsonConvert.SerializeObject(coreBroadcast, new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.All
            });
            redisClient.PublishMessage(coreBroadcast.BroadcastChannel, json);
        }

        // Option 2: this will not trigger the OnMessage above
        using (var messageQueueClient = MqClientFactory.Instance.CreateMessageQueueClient())
        {
            messageQueueClient.Publish(coreBroadcast.BroadcastChannel, new Message<T>(coreBroadcast));
        }
    }

Above are two ways to send, IRedisClient.PublishMessage and and IMessageQueueClient.Publish. As noted in the comments, Option 1 will trigger the OnMessage, but Option 2 will not. Why is that?

In Option 2, I see the produced JSON in the ObjectBroadcast in Redis, lingering and is never retrieved:

来源:https://stackoverflow.com/questions/63409285/servicestack-mq-why-isnt-the-iredissubscription-onmessage-fired-when-manually

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!