AWSSQS尝试创建队列并获取消息

时间:2015-01-31 18:22:38

标签: objective-c amazon-web-services amazon-sqs

我正在尝试连接到AWS SQS以创建新队列,并且还获取我放置在另一个队列上的消息。这是我正在使用的代码:

    AWSSQS *sqs=[[AWSSQS alloc] initWithConfiguration:configuration];

    AWSSQSCreateQueueRequest *createQueue = [[AWSSQSCreateQueueRequest alloc] init];
    [createQueue setQueueName:@"TEST_Q2_NEW_3"];

    BFTask *answer = [sqs createQueue:createQueue];       
    NSLog(@"Status queue creation: %@ %@", answer.result, answer.error);

    AWSSQSReceiveMessageRequest *receiveMessageRequest = [[AWSSQSReceiveMessageRequest alloc] init];      
    receiveMessageRequest.QueueUrl = @"https://sqs.eu-west-1.amazonaws.com/xxxxxxxxxxxx/TEST_Q";

    answer = [sqs receiveMessage:receiveMessageRequest];
    NSLog(@"Status messages received %@ %@", answer.result, answer.error);

已创建队列TEST_Q2_NEW_3,但日志消息为:

状态队列创建:(null)(null)

未提取任何消息,但在SQS管理控制台中,某些消息的状态为“正在消息”。但是,日志消息是:

收到状态消息(null)(null)

我错过了什么?

1 个答案:

答案 0 :(得分:1)

原来我没有完全理解BFTasks。声明:

answer = [sqs receiveMessage:receiveMessageRequest];

实际上在后台启动一个进程,当打印日志消息时,此任务尚未完成。要继续处理结果,必须使用continueWithBlock。对于接收消息功能,工作代码如下所示:

answer = [sqs receiveMessage:receiveMessageRequest];
[answer continueWithBlock:^id(BFTask *task) {
    if (task.isCancelled) {
        NSLog(@"Status received messages  %@ %@", answer.result, answer.error);
    } else if (task.error) {
        NSLog(@"Status received messages  %@ %@", answer.result, answer.error);
    } else {
        NSLog(@"Status received messages  %@ %@", answer.result, answer.error);
    }
    return nil;
}];

这是我找到答案的地方: BoltsFramework/Bolts-iOS - GitHub