2014-10-29 52 views
1

有没有任何方法可以将响应dto的类型添加到rabbitmq响应邮件的标头集合中?Servicestack-RabbitMq:邮件标题中的返回响应类型

(我的消费者使用,这似乎反序列化时,依赖于MQ水箱内显式类型信息春天的RabbitMQ的处理程序。)

目前servicestack的MQ制片人已经返回serveral的标题,如“CONTENT_TYPE ='应用/ JSON ”。

我需要额外的标题,例如“typeId”=“HelloResponse”,以便消费Web应用程序知道如何反序列化消息,即使在响应队列名称是某种GUID的RPC情况下也是如此。

是否有某种配置可以使我实现这样的行为?或者在发布消息之前有一些钩子,以便我可以自己添加标头?

+0

您是否可以提供更多关于如何使用MQ服务的上下文?并在什么时候你需要类型名称。你是否正在阅读来自'mq:HelloResponse.inq'的消息?在这种情况下,Type是mq的名字。 – mythz 2014-10-29 14:21:44

+0

我们没有使用默认响应队列(eqq mq:HelloResponse.inq),而是使用临时队列(amq。*)。因此我们无法从响应队列名称中推断响应类型。 我们需要提供一个类型名称才能选择正确的json解串器。 – celper 2014-10-29 15:55:51

回答

2

我已经添加了对RabbitMQ的IBasicProperties.Type中的消息体类型自动填充的支持,并添加了对发布和GetMessage过滤器in this commit的支持。

这里的定制处理器在当其发布和接收您可以修改消息及其元数据的属性配置RabbitMqServer的例子:

string receivedMsgApp = null; 
string receivedMsgType = null; 

var mqServer = new RabbitMqServer("localhost") 
{ 
    PublishMessageFilter = (queueName, properties, msg) => { 
     properties.AppId = "app:{0}".Fmt(queueName); 
    }, 
    GetMessageFilter = (queueName, basicMsg) => { 
     var props = basicMsg.BasicProperties; 
     receivedMsgType = props.Type; //automatically added by RabbitMqProducer 
     receivedMsgApp = props.AppId; 
    } 
}; 

mqServer.RegisterHandler<Hello>(m => 
    new HelloResponse { Result = "Hello, {0}!".Fmt(m.GetBody().Name) }); 

mqServer.Start(); 

一旦配置的任何消息公布或接收将经过上述处理如:

using (var mqClient = mqServer.CreateMessageQueueClient()) 
{ 
    mqClient.Publish(new Hello { Name = "Bugs Bunny" }); 
} 

receivedMsgApp.Print(); // app:mq:Hello.In 
receivedMsgType.Print(); // Hello 

using (IConnection connection = mqServer.ConnectionFactory.CreateConnection()) 
using (IModel channel = connection.CreateModel()) 
{ 
    var queueName = QueueNames<HelloResponse>.In; 
    channel.RegisterQueue(queueName); 

    var basicMsg = channel.BasicGet(queueName, noAck: true); 
    var props = basicMsg.BasicProperties; 

    props.Type.Print(); // HelloResponse 
    props.AppId.Print(); // app:mq:HelloResponse.Inq 

    var msg = basicMsg.ToMessage<HelloResponse>(); 
    msg.GetBody().Result.Print(); // Hello, Bugs Bunny! 
} 

这种变化可以从ServiceStack v4.0.33 +这就是现在available on MyGet

+0

工程就像一个魅力。非常感谢。 – celper 2014-10-30 11:00:10