AWS SQS实用代码PHP(AWS SQS practical code PHP)

系统教程 行业动态 更新时间:2024-06-14 16:57:18
AWS SQS实用代码PHP(AWS SQS practical code PHP)

我在这里有一个问题 但我仍然缺少了解如何使用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

更多推荐

本文发布于:2023-04-12 20:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/044b73342b624d081da2d5178d293363.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   SQS   AWS   code   practical

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!