本文整理汇总了PHP中Aws\Sqs\SqsClient::sendMessageBatch方法的典型用法代码示例。如果您正苦于以下问题:PHP SqsClient::sendMessageBatch方法的具体用法?PHP SqsClient::sendMessageBatch怎么用?PHP SqsClient::sendMessageBatch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Aws\Sqs\SqsClient
的用法示例。
在下文中一共展示了SqsClient::sendMessageBatch方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addMessages
/**
* @inheritdoc
*
* @throws \InvalidArgumentException
* @throws InvalidMessageException
* @throws QueueAccessException
*/
public function addMessages($queueName, $messages, Priority $priority = null)
{
if (empty($queueName)) {
throw new \InvalidArgumentException('Queue name empty or not defined.');
}
if (null === $priority) {
$priority = $this->priorityHandler->getDefault();
}
$i = 0;
$batch = [];
$totalMessagesCount = count($messages);
foreach ($messages as $index => $message) {
if (empty($message)) {
throw new InvalidMessageException($message, 'Message empty or not defined.');
}
$messageData = ['Id' => (string) $index, 'MessageBody' => serialize($message)];
$batch[] = $messageData;
if (++$i < $totalMessagesCount && count($batch) < self::SENT_MESSAGES_BATCH_SIZE) {
continue;
}
try {
$queueUrl = $this->sqsClient->getQueueUrl(['QueueName' => $this->getQueueNameWithPrioritySuffix($queueName, $priority)])->get('QueueUrl');
$this->sqsClient->sendMessageBatch(['QueueUrl' => $queueUrl, 'Entries' => $batch]);
} catch (SqsException $e) {
throw new QueueAccessException('Cannot add messages in queue.', 0, $e);
}
$batch = [];
}
return $this;
}
示例2: batchPush
/**
* Valid option is:
* - delay_seconds: the duration (in seconds) the message has to be delayed
*
* Please note that for this to work, the index for the job AND the option must match
*
* {@inheritDoc}
*/
public function batchPush(array $jobs, array $options = array())
{
// SQS can only handle up to 10 jobs, so if we have more jobs, we handle them in slices
if (count($jobs) > 10) {
do {
$splicedJobs = array_splice($jobs, 0, 10);
$this->batchPush($splicedJobs, $options);
} while (count($splicedJobs) >= 10);
return;
}
// SQS throws an exception if no jobs are inserted, which can happen due to the slicing method
if (empty($jobs)) {
return;
}
$parameters = array('QueueUrl' => $this->queueOptions->getQueueUrl(), 'Entries' => array());
/** @var $job JobInterface */
foreach ($jobs as $key => $job) {
$jobParameters = array('Id' => $key, 'MessageBody' => $this->serializeJob($job), 'DelaySeconds' => isset($options[$key]['delay_seconds']) ? $options[$key]['delay_seconds'] : null);
$parameters['Entries'][] = array_filter($jobParameters, function ($value) {
return $value !== null;
});
}
$result = $this->sqsClient->sendMessageBatch($parameters);
$messages = $result['Successful'];
foreach ($messages as $message) {
$batchId = $message['Id'];
$jobs[$batchId]->setMetadata(array('__id__' => $message['MessageId'], 'md5' => $message['MD5OfMessageBody']));
}
}
示例3: sendMessageBatch
/**
* This can be used to send up to 10 entries and up to a total of 264kb
*
* {@inheritDoc}
*/
public function sendMessageBatch($messageBodies = [], $queueId = null)
{
if (!is_array($messageBodies)) {
throw new \Exception("MessageBodies must be an array");
}
$queueUrl = $this->getQueueUrl($queueId);
$entries = [];
foreach ($messageBodies as $key => $body) {
$entries[] = ["Id" => $key, "MessageBody" => json_encode($body)];
}
$this->queueClient->sendMessageBatch(["QueueUrl" => $queueUrl, "Entries" => $entries]);
}
示例4: batchPublish
/**
* @see http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#sendmessagebatch
* @param array $messages
*/
public function batchPublish(array $messages)
{
$j = 0;
for ($i = 0; $i < count($messages); $i += 10) {
$entries = array();
$toSend = array_slice($messages, $i, 10);
foreach ($toSend as $message) {
$entries[] = array_merge(array('MessageBody' => $message['msgBody'], 'Id' => $j++), $this->getClientParams(@$message['routingKey'], @$message['additionalProperties']));
}
$result = $this->client->sendMessageBatch(array('QueueUrl' => $this->queueUrl, 'Entries' => $entries));
if (($ok = count($result->get('Successful'))) != ($tot = count($toSend))) {
throw new \RuntimeException("Batch sending of messages failed - {$ok} ok out of {$tot}");
}
}
}
示例5: enqueueAll
/**
* Message batch processing
*
* @param $messages
* @return mixed
*/
public function enqueueAll(MessageCollectionInterface $messages)
{
$serializer = $this->getEncoder();
$request = ['QueueUrl' => $this->getUrl(), 'Entries' => []];
$messages = $messages->getMessages();
foreach (array_chunk($messages, $this->config->getBatchSize()) as $chunk) {
foreach ($chunk as $id => $message) {
$request['Entries'][$id] = ['Id' => $id, 'DelaySeconds' => $this->getMessageDelay($message), 'QueueUrl' => $this->getUrl(), 'MessageBody' => $serializer->encode($message)];
}
$result = $this->client->sendMessageBatch($request);
$items = $result->getPath('Successful') ?: [];
foreach ($items as $item) {
$messages[$item['Id']]->setId($item['MessageId']);
}
}
}
示例6: doFlush
/**
* Flush a queue
*
* @param bool $async
* @return void
*/
private function doFlush(bool $async)
{
// SQS only supports batch of 10, so we need to splice like that
while (!empty($this->messages)) {
$messagesToPush = array_splice($this->messages, 0, 10);
$parameters = ['QueueUrl' => $this->url, 'Entries' => []];
foreach ($messagesToPush as $key => $message) {
$messageParameters = ['Id' => $key, 'MessageBody' => json_encode($message['body'], self::DEFAULT_JSON_FLAGS), 'DelaySeconds' => $message['options']['delay_seconds'] ?? null];
$parameters['Entries'][] = array_filter($messageParameters, function ($value) {
return $value !== null;
});
}
if ($async) {
$this->sqsClient->sendMessageBatchAsync($parameters);
} else {
$this->sqsClient->sendMessageBatch($parameters);
}
}
}
示例7: sendMessageBatch
protected function sendMessageBatch(array $payrolls, array $attributes, $delay)
{
$entries = [];
foreach ($payrolls as $idx => $payroll) {
$entry = ["Id" => "buf_{$idx}", "MessageBody" => $payroll];
if ($delay) {
$entry['DelaySeconds'] = $delay;
}
if ($attributes) {
$entry['MessageAttributes'] = $attributes[$idx];
}
$entries[] = $entry;
}
$args = ["QueueUrl" => $this->getQueueUrl(), "Entries" => $entries];
$result = $this->client->sendMessageBatch($args);
if ($result['Failed']) {
foreach ($result['Failed'] as $failed) {
mwarning(sprintf("Batch sending message failed, code = %s, id = %s, msg = %s, senderfault = %s", $failed['Code'], $failed['Id'], $failed['Message'], $failed['SenderFault']));
}
throw new \RuntimeException("Cannot send some messages, consult log for more info!");
}
}