Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Connection.php #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/Connection/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
use PhpAmqpLib\Connection\AMQPSSLConnection;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exception\AMQPTimeoutException;
use PhpAmqpLib\Exchange\AMQPExchangeType;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Wire\AMQPTable;
use Swoft\Amqp\Client;
use Swoft\Amqp\Contract\ConnectionInterface;
use Swoft\Amqp\Exception\AMQPException;
Expand Down Expand Up @@ -119,11 +121,13 @@ public function channel($channelId = 'default'): Connection
$channels = $this->client->getChannels();
$exchange = $this->client->getExchange();
$queue = $this->client->getQueue();
$route = $this->client->getRoute();

if (!isset($channels['default'])) {
if ($exchange && $queue) {
$channels['default']['exchange'] = $exchange;
$channels['default']['queue'] = $queue;
$channels['default']['route'] = $route;
$this->client->setChannels($channels);
} else {
$channels['default'] = Arr::first($channels);
Expand All @@ -137,8 +141,10 @@ public function channel($channelId = 'default'): Connection
if (!isset($channels[$channelId]['queue'])) {
throw new AMQPException('RabbitMQ connect error is Queue empty');
}
$route = $channels[$channelId]['route'] ?? '';
$this->client->setExchange($channels[$channelId]['exchange']);
$this->client->setQueue($channels[$channelId]['queue']);
$this->client->setRoute($route);
} else {
throw new AMQPException('RabbitMQ connect error is Channel has not exists');
}
Expand Down Expand Up @@ -308,6 +314,70 @@ public function push(string $message, array $prop = [], string $route = ''): voi
$this->release();
}

/**
* delay
*
* @param string $message
* @param int $delay
* @param string $route
* @throws AMQPException
*/
public function delay(string $message, int $delay = 0, string $route = ''): void
{
$exchange = $this->client->getExchange();
$delayExchange = 'delay-exchange';
$queue = 'delay-' . $exchange . '-' . $delay;

try {
$this->channel->exchange_declare(
$delayExchange,
AMQPExchangeType::DIRECT,
false,
true,
false,
false,
false,
[],
null
);

$this->channel->queue_declare(
$queue,
false,
true,
false,
true,
false,
new AMQPTable([
'x-message-ttl' => $delay * 1000,
'x-dead-letter-exchange' => $exchange,
'x-dead-letter-routing-key' => $route
]),
null,
);

$this->channel->queue_bind(
$queue,
$delayExchange,
$queue,
false,
[],
null
);

$body = new AMQPMessage($message, ['delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]);
$this->channel->basic_publish($body, $delayExchange, $queue);
$this->release();
} catch (Exception $e) {
throw new AMQPException(sprintf(
'RabbitMQ delay queue error is %s file=%s line=%d',
$e->getMessage(),
$e->getFile(),
$e->getLine()
));
}
}

/**
* pull
*
Expand Down