我在这里有一个问题 但我仍然缺少了解如何使用SQS的要点,请任何人都可以帮助解决一些代码。 问题: SQS中的一个地方会是什么? 我已经阅读了amazons教程,这个概念看起来很可爱,但是我缺少实际的一面。 例如,这个图很棒: http : //awsmedia.s3.amazonaws.com/catalog/images/159-for-madhu.png SQS内部会发生什么? 我知道如何上传到S3,但仍然灰色关于SQS部分
require_once('sdk-1.5.6.2/sdk.class.php'); //random strings $AWS_KEY = "MY_ACCESS_KEY"; $AWS_SECRET_KEY = "MY_SECRET_KEY"; //create a new SQS queue and grab the queue URL $sqs = new AmazonSQS(array( "key" => $AWS_KEY, "secret" => $AWS_SECRET_KEY )); $response = $sqs->create_queue('test-topic-queue'); $queue_url = (string) $response->body->CreateQueueResult->QueueUrl; $queue_arn = 'arn:aws:sqs:us-east-1:ENCQ8gqrAcXv:test-topic-queue'; $topic_arn = 'arn:aws:sns:us-east-1:ENCQ8gqrAcXv:test-topic'; $response = $sns->subscribe($topic_arn, 'sqs', $queue_arn); $subscription_arn = (string) $response->body->SubscribeResult->SubscriptionArn; /* * * * * * * * * * * * * * * * * * THIS IS THE BIG GREY AREA, WHAT HAPPENS HERE ??? * * * * * * * * * * * * * * * * * */ // delete SQS queue $queue_url = 'https://sqs.us-east-1.amazonaws.com/ENCQ8gqrAcXv/test-topic-queue'; $response = $sqs->delete_queue($queue_url);i had a question here but i'm still missing the point understanding how to use SQS, could anyone help out with some code please. question: what would one place inside a SQS?i've read through amazons tutorial and the concept seems lovely but im missing the practical side of things. for instance, this diagram is great: http://awsmedia.s3.amazonaws.com/catalog/images/159-for-madhu.png what would go inside the SQS? I understand how to upload to S3, but still grey about the SQS part
require_once('sdk-1.5.6.2/sdk.class.php'); //random strings $AWS_KEY = "MY_ACCESS_KEY"; $AWS_SECRET_KEY = "MY_SECRET_KEY"; //create a new SQS queue and grab the queue URL $sqs = new AmazonSQS(array( "key" => $AWS_KEY, "secret" => $AWS_SECRET_KEY )); $response = $sqs->create_queue('test-topic-queue'); $queue_url = (string) $response->body->CreateQueueResult->QueueUrl; $queue_arn = 'arn:aws:sqs:us-east-1:ENCQ8gqrAcXv:test-topic-queue'; $topic_arn = 'arn:aws:sns:us-east-1:ENCQ8gqrAcXv:test-topic'; $response = $sns->subscribe($topic_arn, 'sqs', $queue_arn); $subscription_arn = (string) $response->body->SubscribeResult->SubscriptionArn; /* * * * * * * * * * * * * * * * * * THIS IS THE BIG GREY AREA, WHAT HAPPENS HERE ??? * * * * * * * * * * * * * * * * * */ // delete SQS queue $queue_url = 'https://sqs.us-east-1.amazonaws.com/ENCQ8gqrAcXv/test-topic-queue'; $response = $sqs->delete_queue($queue_url);最满意答案
你应该有两个进程,一个将消息插入队列和你的工作线程。 典型的工作线程将如下所示:
while(true) { $res = $client->receiveMessage(array( 'QueueUrl' => $url, 'WaitTimeSeconds' => 1 )); if ($res->getPath('Messages')) { foreach ($res->getPath('Messages') as $msg) { echo "Received Msg: ".$msg['Body']; // Do something useful with $msg['Body'] here $res = $client->deleteMessage(array( 'QueueUrl' => $url, 'ReceiptHandle' => $msg['ReceiptHandle'] )); } } }WaitTimeSeconds参数意味着执行“长轮询”并具有各种优点(请参阅http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html )。
您应该使用监督或监视来确保您的工作线程保持运行。
更新:对于API v3使用get()而不是getPath() - getPath现在已被弃用
You should have two processes, one that inserts messages into the queue and your worker threads. A typical worker thread will look something like this:
while(true) { $res = $client->receiveMessage(array( 'QueueUrl' => $url, 'WaitTimeSeconds' => 1 )); if ($res->getPath('Messages')) { foreach ($res->getPath('Messages') as $msg) { echo "Received Msg: ".$msg['Body']; // Do something useful with $msg['Body'] here $res = $client->deleteMessage(array( 'QueueUrl' => $url, 'ReceiptHandle' => $msg['ReceiptHandle'] )); } } }The WaitTimeSeconds parameters means to do "long polling" and has various benefits (see http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html).
You should use supervise or monit to make sure your worker threads stay running.
UPDATE: for api v3 use get() instead of getPath() - getPath is now deprecated
更多推荐
发布评论