2014-10-29 30 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