有没有办法将响应dto的类型添加到rabbitmq响应消息的headers集合中?
(我的消费者正在使用spring的rabbitmq处理程序,它在反序列化时似乎依赖于mq头中的显式类型信息。)
目前,servicestack的mq生产者已经返回了多个标题,例如“content_type ='application / json”。
我需要一个额外的标题,例如“ typeId ”=“HelloResponse”,以便消费的Web应用程序知道如何反序列化消息,即使在响应队列名称是某种GUID的RPC情况下也是如此。
是否有某种配置可以让我实现这样的行为?或者在消息发布之前有一些钩子,以便我可以自己添加标题?
答案 0 :(得分:2)
我已经添加了对在RabbitMQ的IBasicProperties.Type
中自动填充消息正文类型以及添加对Publish和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。