-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: исправил замечания, вынес зависимости в конструктор
- Loading branch information
Showing
9 changed files
with
91 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace app\components; | ||
|
||
use Exception; | ||
use PhpAmqpLib\Message\AMQPMessage; | ||
use Yii; | ||
|
||
class RabbitMqConsumer | ||
{ | ||
public function __construct(private RabbitMq $rabbitMq) | ||
{ | ||
} | ||
|
||
/** | ||
* Чтение сообщений из RabbitMQ | ||
* | ||
* @param string $queueName | ||
* @throws Exception | ||
*/ | ||
public function consume(string $queueName): void | ||
{ | ||
$channel = $this->rabbitMq->getChannel(); | ||
$channel->queue_declare($queueName, false, true, false, false); | ||
|
||
$channel->basic_consume($queueName, '', false, true, false, false, [$this, 'callback']); | ||
|
||
while ($channel->is_consuming()) { | ||
$channel->wait(); | ||
} | ||
} | ||
|
||
public function callback(AMQPMessage $message): void | ||
{ | ||
echo $message->body, "\n"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace app\components; | ||
|
||
use PhpAmqpLib\Message\AMQPMessage; | ||
use Yii; | ||
|
||
class RabbitMqProducer | ||
{ | ||
public function __construct(private RabbitMq $rabbitMq) | ||
{ | ||
} | ||
|
||
public function sendMessage($queueName, $message): bool | ||
{ | ||
try { | ||
$channel = $this->rabbitMq->getChannel(); | ||
$channel->queue_declare($queueName, false, true, false, false); | ||
$message = new AMQPMessage(json_encode($message)); | ||
$channel->basic_publish($message, '', $queueName); | ||
return true; | ||
} catch (\Exception $e) { | ||
Yii::error('Error sending message to RabbitMQ: ' . $e->getMessage()); | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.