-
Notifications
You must be signed in to change notification settings - Fork 9
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
Dead lettering support #15
Merged
Merged
Changes from 20 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
a8bf177
Add fanout, direct and topic exchanges type support
KinaneD 3c2d9ee
add `nack` consumer support
KinaneD 48cdcf3
minor improvements
KinaneD 93c80f3
fix typo
KinaneD 9388a6c
add typehinting and set default value to routingKeys
KinaneD 6c75803
improve command description
KinaneD de86ad2
add consume command options shortcuts
KinaneD 6504890
add dead lettering command options
KinaneD 4d19383
if dead lettering options are set, configure
KinaneD 6284d37
implement dead lettering
KinaneD 2b24bb3
add missing `;`
KinaneD 6345623
remove command options shortcuts
KinaneD 531393d
remove producer's construct typehinting
KinaneD 7057ec7
set command options one letter shortucts
KinaneD 6a8573e
add dead lettering command options
KinaneD 4b25e8f
implement dead lettering
KinaneD 7bacd9f
set default producer exchange type to fanout
KinaneD 58f48bc
assure backward compatibility by setting a default to exchangeName
KinaneD 71faf4e
improve messageTtl description
KinaneD ad58b63
update readme
KinaneD 438ec0e
change to use [] array instead [null]
KinaneD 8ce886a
minor improvements
KinaneD 273df2c
comment on why we declare queues in the producer
KinaneD 8df3fc4
Move ack, nack, reject message in addition to new ones to trait
KinaneD 77ef842
add compileParameters() to trait
KinaneD 2a9de24
add DeclarationMismatchException
KinaneD a639236
wrap with try catch exchange and queue declarations
KinaneD 2c4d03d
set exception properties to protected
KinaneD 11a7227
fix exception
KinaneD 85af874
unify variables name
KinaneD 6e3f226
Move message actions back to consumer
KinaneD 9730f32
Move queue and exchange actions to a new AdminTrait
KinaneD 8e29578
add author
KinaneD d57758a
move rabbitmq configuration from connection to service provider
KinaneD d6e398d
move helper methods to HelperTrait
KinaneD d814eb5
update readme
KinaneD 9eca608
fix typo
KinaneD bcd3f05
allow producer to only set one routing key on a message
KinaneD 83413cd
add `criticalDelivery` note
KinaneD 4084bf6
revoke producer's capability to declare and bind queues
KinaneD c9d6c2a
minor updates
KinaneD 00dc109
fix typo
KinaneD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
namespace Vinelab\Bowler; | ||
|
||
use PhpAmqpLib\Message\AMQPMessage; | ||
use Vinelab\Bowler\Traits\DeadLetteringTrait; | ||
use Vinelab\Bowler\Contracts\BowlerExceptionHandler as ExceptionHandler; | ||
|
||
/** | ||
|
@@ -13,20 +14,36 @@ | |
*/ | ||
class Consumer | ||
{ | ||
use DeadLetteringTrait; | ||
|
||
/** | ||
* the main class of the package where we define the channel and the connection. | ||
* The main class of the package where we define the channel and the connection. | ||
* | ||
* @var Vinelab\Bowler\Connection | ||
*/ | ||
private $connection; | ||
|
||
/** | ||
* the name of the exchange where the producer sends its messages to. | ||
* The name of the queue bound to the exchange where the producer sends its messages. | ||
* | ||
* @var string | ||
*/ | ||
private $queueName; | ||
|
||
/** | ||
* The name of the exchange where the producer sends its messages to. | ||
* | ||
* @var string | ||
*/ | ||
private $exchangeName; | ||
|
||
/** | ||
* The binding keys used by the exchange to route messages to bounded queues. | ||
* | ||
* @var string | ||
*/ | ||
private $bindingKeys; | ||
|
||
/** | ||
* type of exchange: | ||
* fanout: routes messages to all of the queues that are bound to it and the routing key is ignored. | ||
|
@@ -69,22 +86,31 @@ class Consumer | |
*/ | ||
private $deliveryMode; | ||
|
||
private $msgProcessor; | ||
/** | ||
* The arguments that should be added to the `queue_declare` statement for dead lettering | ||
* | ||
* @var array | ||
*/ | ||
private $arguments = []; | ||
|
||
/** | ||
* @param Vinelab\Bowler\Connection $connection | ||
* @param string $queueName | ||
* @param string $exchangeName | ||
* @param string $exchangeType | ||
* @param array $bindingKeys | ||
* @param bool $passive | ||
* @param bool $durable | ||
* @param bool $autoDelete | ||
* @param int $deliveryMode | ||
*/ | ||
public function __construct(Connection $connection, $exchangeName, $exchangeType = 'fanout', $passive = false, $durable = true, $autoDelete = false, $deliveryMode = 2) | ||
public function __construct(Connection $connection, $queueName, $exchangeName, $exchangeType = 'fanout', $bindingKeys = [null], $passive = false, $durable = true, $autoDelete = false, $deliveryMode = 2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
$this->connection = $connection; | ||
$this->queueName = $queueName; | ||
$this->exchangeName = $exchangeName; | ||
$this->exchangeType = $exchangeType; | ||
$this->bindingKeys = $bindingKeys; | ||
$this->passive = $passive; | ||
$this->durable = $durable; | ||
$this->autoDelete = $autoDelete; | ||
|
@@ -94,15 +120,22 @@ public function __construct(Connection $connection, $exchangeName, $exchangeType | |
/** | ||
* consume a message from a specified exchange. | ||
* | ||
* @param string $data | ||
* @param string $handlerClass | ||
* @param Vinelab\Bowler\Contracts\BowlerExceptionHandler $exceptionHandler | ||
*/ | ||
public function listenToQueue($handlerClass, ExceptionHandler $exceptionHandler) | ||
{ | ||
$this->connection->getChannel()->exchange_declare($this->exchangeName, $this->exchangeType, $this->passive, $this->durable, $this->autoDelete); | ||
list($queue_name) = $this->connection->getChannel()->queue_declare($this->exchangeName, $this->passive, $this->durable, false, $this->autoDelete); | ||
$this->connection->getChannel()->queue_bind($queue_name, $this->exchangeName); | ||
$channel = $this->connection->getChannel(); | ||
|
||
$channel->exchange_declare($this->exchangeName, $this->exchangeType, $this->passive, $this->durable, $this->autoDelete); | ||
$channel->queue_declare($this->queueName, $this->passive, $this->durable, false, $this->autoDelete, false, $this->arguments); | ||
|
||
|
||
echo ' [*] Listening to Queue: ' . $this->exchangeName . ' To exit press CTRL+C', "\n"; | ||
foreach ($this->bindingKeys as $bindingKey) { | ||
$channel->queue_bind($this->queueName, $this->exchangeName, $bindingKey); | ||
} | ||
|
||
echo " [*] Listening to Queue: ", $this->queueName, " To exit press CTRL+C", "\n"; | ||
|
||
$handler = new $handlerClass; | ||
|
||
|
@@ -124,31 +157,44 @@ public function listenToQueue($handlerClass, ExceptionHandler $exceptionHandler) | |
} | ||
}; | ||
|
||
$this->connection->getChannel()->basic_qos(null, 1, null); | ||
$this->connection->getChannel()->basic_consume($queue_name, '', false, false, false, false, $callback); | ||
$channel->basic_qos(null, 1, null); | ||
$channel->basic_consume($this->queueName, '', false, false, false, false, $callback); | ||
|
||
while (count($this->connection->getChannel()->callbacks)) { | ||
$this->connection->getChannel()->wait(); | ||
while (count($channel->callbacks)) { | ||
$channel->wait(); | ||
} | ||
} | ||
|
||
/** | ||
* acknowledge a messasge. | ||
* Acknowledge a messasge. | ||
* | ||
* @param PhpAmqpLib\Message\AMQPMessage $msg | ||
*/ | ||
public function ackMessage($msg) | ||
{ | ||
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); | ||
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag'], 0); | ||
} | ||
|
||
/** | ||
* Negatively acknowledge a messasge. | ||
* | ||
* @param PhpAmqpLib\Message\AMQPMessage $msg | ||
* @param bool $multiple | ||
* @param bool $requeue | ||
*/ | ||
public function nackMessage($msg, $multiple = false, $requeue = false) | ||
{ | ||
$msg->delivery_info['channel']->basic_nack($msg->delivery_info['delivery_tag'], $multiple, $requeue); | ||
} | ||
|
||
/** | ||
* reject a messasge. | ||
* Reject a messasge. | ||
* | ||
* @param PhpAmqpLib\Message\AMQPMessage $msg | ||
* @param bool $requeue | ||
*/ | ||
public function rejectMessage($msg) | ||
public function rejectMessage($msg, $requeue = false) | ||
{ | ||
$msg->delivery_info['channel']->basic_reject($msg->delivery_info['delivery_tag'], false); | ||
$msg->delivery_info['channel']->basic_reject($msg->delivery_info['delivery_tag'], $requeue); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename the var
$xName