我想覆盖ServiceStack.RabbitMq.RabbitMqProducer.PublishMessage(),以便在服务器命名队列的情况下省略队列声明:
public void PublishMessage(string exchange, string routingKey, IBasicProperties basicProperties, byte[] body)
{
try
{
if (routingKey.IsServerNamedQueue()) // In case of server named queues (client declared queue with channel.declare()), assume queue already exists (redeclaration would result in error anyway since queue was marked as exclusive) and publish to default exchange
{
Channel.BasicPublish("", routingKey, basicProperties, body);
}
else
{
if (!Queues.Contains(routingKey))
{
Channel.RegisterQueueByName(routingKey);
Queues = new HashSet<string>(Queues) { routingKey };
}
Channel.BasicPublish(exchange, routingKey, basicProperties, body);
}
...
使用扩展方法
public static bool IsServerNamedQueue(this string queueName)
{
if (string.IsNullOrEmpty(queueName))
{
throw new ArgumentNullException("queueName");
}
return queueName.ToLower().StartsWith("amq.");
}
不幸的是,PublishMessage不是虚拟的,并且mq类之间存在一些依赖关系(例如RabbitMqQueueClient继承自RabbitMqProducer),这需要我自己重新实现整个IMessageService。
有没有更简单的方法来实现这一目标?