有some documentation关于将Amazon SQS用作ServiceStack Messaging API的MQ服务器
但是消息发布者是前端Web代码,当您深入研究Email Contacts demo应用时,它使用的是Rabbit MQ。
有一个ServiceStack Email Contacts AWS App demo,但未使用Messaging API。
以电子邮件联系人中的Rabbit MQ集成测试为例:
[Test]
public void Can_Send_Email_via_MqClient()
{
var mqFactory = new RabbitMqMessageFactory();
using (var mqClient = mqFactory.CreateMessageQueueClient())
{
mqClient.Publish(new EmailContact { ContactId = 1, Subject = "UnitTest MQ Email #1", Body = "Body 1" });
mqClient.Publish(new EmailContact { ContactId = 1, Subject = "UnitTest MQ Email #2", Body = "Body 2" });
}
}
我很快感到困惑并误入歧途,用于Sq的ServiceStack API似乎与RabbitMQ完全不同。我什至似乎无法使用强类型POCO作为消息:
[Fact(DisplayName = "Tests that a successful message is published and received")]
public async void TestMessage()
{
var mqFactory = new SqsConnectionFactory("awskey", "awssecret", RegionEndpoint.USWest1);
using (IAmazonSQS mqClient = mqFactory.GetClient())
{
var req = new SendMessageRequest("query", "hello");
await mqClient.SendMessageAsync(req);
//mqClient.Publish(new Hello { Name = "World" });
//var rec = new ReceiveMessageRequest();
//await mqClient.Re
//var responseMsg = mqClient.Get<HelloResponse>(QueueNames<HelloResponse>.In);
//mqClient.Ack(responseMsg);
//responseMsg.GetBody().Result //= Hello, World!
}
}
是否有示例应用程序使用ServiceStack Messaging API和SQS作为MQ服务器?
答案 0 :(得分:2)
有一个ServiceStack Email Contacts AWS App demo,但未使用Messaging API。
注意,AWS Apps确实注册了AWS SqsMqServer:
//EmailContacts
ConfigureSqsMqServer(container);
//..
private void ConfigureSqsMqServer(Container container)
{
container.Register<IMessageService>(c => new SqsMqServer(
AwsConfig.AwsAccessKey, AwsConfig.AwsSecretKey, RegionEndpoint.USEast1) {
DisableBuffering = true,
});
var mqServer = container.Resolve<IMessageService>();
mqServer.RegisterHandler<EmailContacts.EmailContact>(ExecuteMessage);
mqServer.Start();
}
SqsMqServerTests.cs中还有许多示例。
如果要使用ServiceStack MQ's high-level APIs,则需要直接使用ServiceStack的MQ类而不是AWS的SQS类。
基本上,它与其他所有MQ服务器一样,您可以从IMessageFactory
或IMessageService
(在AppHost中注册)获取MQ客户端,并使用它来发布DTO:
var mqFactory = HostContext.TryResolve<IMessageFactory>(); //or
//var mqFactory = HostContext.TryResolve<IMessageService>().MessageFactory;
using (var mqClient = mqFactory.CreateMessageQueueClient())
{
mqClient.Publish(new Hello { Name = "World" });
}
尽管ServiceStack Service中首选的API是使用PublishMessage()
:
PublishMessage(new Hello { Name = "World" });
对
/oneway
预定义端点的注释请求为automatically published to the registered MQ Server。
在没有注册SqsMqServer
的客户端应用中,您将创建SqsMqMessageFactory
:
var mqFactory = new SqsMqMessageFactory(new SqsQueueManager(...));
using (var mqClient = mqFactory.CreateMessageQueueClient())
{
mqClient.Publish(new Hello { Name = "World" });
}