寻求对ServiceStack.Redis的理解:IRedisClient.PublishMessage vs IMessageQueueClient.Publish
Seeking an understanding of ServiceStack.Redis: IRedisClient.PublishMessage vs IMessageQueueClient.Publish
我很难区分 IRedisClient.PublishMessage
和 IMessageQueueClient.Publish
,我意识到我一定是搞混了。
ServiceStack 为我们提供了监听 pub/sub 广播的选项,如下所示:
static IRedisSubscription _subscription;
static IRedisClient redisClientSub;
static int received = 0;
static void ReadFromQueue()
{
redisClientSub = redisClientManager.GetClient();
_subscription = redisClientSub.CreateSubscription();
_subscription.OnMessage = (channel, msg) =>
{
try
{
received++;
}
catch (Exception ex)
{
}
};
Task.Run(() => _subscription.SubscribeToChannels("Test"));
}
看起来不错,简单明了。但是制作人呢?
在查看可用的 类 时,我认为可以使用 IRedisClient.PublishMessage(string toChannel, string message)
或 IMessageQueueClient.Publish(string queueName, IMessage message)
。
redisClient.PublishMessage("Test", json);
// or:
myMessageQueueClient.Publish("Test", new Message<CoreEvent>(testReq));
在这两种情况下,您都可以自己指定频道名称。这是我看到的行为:
- 上面的订阅者只有在我使用
IRedisClient.PublishMessage(string toChannel, string message)
时才会收到消息,如果我使用 IMessageQueueClient.Publish(string queueName, IMessage message)
则永远不会收到消息
- 如果我使用
IRedisClient.PublishMessage
发布,我希望填充“测试”频道(如果我使用 Redis 浏览器查看),但事实并非如此。我从来没有看到任何队列的痕迹(假设我没有开始订阅,但生产者添加了消息)
- 如果我使用
IMessageQueueClient.Publish(string queueName, IMessage message)
进行发布,则会创建频道“测试”并在那里保留消息,但不会 popped/fetched-and-deleted。
我想了解一下两者的区别。我查看了源代码并尽我所能阅读,但我没有找到任何关于 IRedisClient.PublishMessage
.
的文档
Mythz 在 ServiceStack 论坛上回答了这个问题,here。
他写道:
These clients should not be used interchangeably, you should only be
using ServiceStack MQ clients to send MQ Messages or the Message MQ
Message wrapper.
The redis subscription is low level API to create a Redis Pub/Sub
subscription, a more useful higher level API is the Managed Pub/Sub
Server which wraps the pub/sub subscription behind a managed thread.
Either way, MQ Server is only designed to process messages from MQ
clients, if you’re going to implement your own messaging
implementation use your own messages & redis clients not the MQ
clients or MQ Message class.
和
No IRedisClient (& ServiceStack.Redis) APIs are for Redis Server, the
PublishMessage API sends the redis PUBLISH command. IRedisSubscription
creates a Redis Pub/Sub subscription, see Redis docs to learn how
Redis Pub/Sub works. The ServiceStack.Redis library and all its APIs
are just for Redis Server, it doesn’t contain any
ServiceStack.Messaging MQ APIs.
So just use ServiceStack.Redis for your custom Redis Pub/Sub
subscription implementation, i.e. don’t use any ServiceStack.Messaging
APIs which is for ServiceStack MQ only.
我很难区分 IRedisClient.PublishMessage
和 IMessageQueueClient.Publish
,我意识到我一定是搞混了。
ServiceStack 为我们提供了监听 pub/sub 广播的选项,如下所示:
static IRedisSubscription _subscription;
static IRedisClient redisClientSub;
static int received = 0;
static void ReadFromQueue()
{
redisClientSub = redisClientManager.GetClient();
_subscription = redisClientSub.CreateSubscription();
_subscription.OnMessage = (channel, msg) =>
{
try
{
received++;
}
catch (Exception ex)
{
}
};
Task.Run(() => _subscription.SubscribeToChannels("Test"));
}
看起来不错,简单明了。但是制作人呢?
在查看可用的 类 时,我认为可以使用 IRedisClient.PublishMessage(string toChannel, string message)
或 IMessageQueueClient.Publish(string queueName, IMessage message)
。
redisClient.PublishMessage("Test", json);
// or:
myMessageQueueClient.Publish("Test", new Message<CoreEvent>(testReq));
在这两种情况下,您都可以自己指定频道名称。这是我看到的行为:
- 上面的订阅者只有在我使用
IRedisClient.PublishMessage(string toChannel, string message)
时才会收到消息,如果我使用IMessageQueueClient.Publish(string queueName, IMessage message)
则永远不会收到消息
- 如果我使用
IRedisClient.PublishMessage
发布,我希望填充“测试”频道(如果我使用 Redis 浏览器查看),但事实并非如此。我从来没有看到任何队列的痕迹(假设我没有开始订阅,但生产者添加了消息) - 如果我使用
IMessageQueueClient.Publish(string queueName, IMessage message)
进行发布,则会创建频道“测试”并在那里保留消息,但不会 popped/fetched-and-deleted。
我想了解一下两者的区别。我查看了源代码并尽我所能阅读,但我没有找到任何关于 IRedisClient.PublishMessage
.
Mythz 在 ServiceStack 论坛上回答了这个问题,here。
他写道:
These clients should not be used interchangeably, you should only be using ServiceStack MQ clients to send MQ Messages or the Message MQ Message wrapper.
The redis subscription is low level API to create a Redis Pub/Sub subscription, a more useful higher level API is the Managed Pub/Sub Server which wraps the pub/sub subscription behind a managed thread.
Either way, MQ Server is only designed to process messages from MQ clients, if you’re going to implement your own messaging implementation use your own messages & redis clients not the MQ clients or MQ Message class.
和
No IRedisClient (& ServiceStack.Redis) APIs are for Redis Server, the PublishMessage API sends the redis PUBLISH command. IRedisSubscription creates a Redis Pub/Sub subscription, see Redis docs to learn how Redis Pub/Sub works. The ServiceStack.Redis library and all its APIs are just for Redis Server, it doesn’t contain any ServiceStack.Messaging MQ APIs.
So just use ServiceStack.Redis for your custom Redis Pub/Sub subscription implementation, i.e. don’t use any ServiceStack.Messaging APIs which is for ServiceStack MQ only.