我已经添加了对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