我已經添加了對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。
您是否可以提供更多關於如何使用MQ服務的上下文?並在什麼時候你需要類型名稱。你是否正在閱讀來自'mq:HelloResponse.inq'的消息?在這種情況下,Type是mq的名字。 – mythz 2014-10-29 14:21:44
我們沒有使用默認響應隊列(eqq mq:HelloResponse.inq),而是使用臨時隊列(amq。*)。因此我們無法從響應隊列名稱中推斷響應類型。 我們需要提供一個類型名稱才能選擇正確的json解串器。 – celper 2014-10-29 15:55:51