📜 ⬆️ ⬇️

New ideas on the API of RabbitMQ AMQP for PHP

Recently published the previously developed PHP API for RabbitMQ "AMQP now for PHP"

When discussing, it was proposed to make the PHP API more object model,
closer to the model proposed in the AMQP Protocol.

The code will be a little more complicated, but the object model will be more beautiful,
')
Before moving on to coding, I submit a new API for discussion.

Class AMQPConnection

opening a logical connection, including a channel connection.
$ cnn = new APMQConection ([host = localhost], [port = 5672], [login = guest], [psw = guest], [vhost = /]);
$ cnn-> getResult (); - true / false result of the connection

AMQPExchange Exchange Class

$ exchange = new AMQPExchange ($ cnn, [name], [parms]) - create exchange, if the name is specified otherwise class initialization
$ res = $ exchange-> declare ([name]) - declare the exchange
$ res = $ exchange-> delete ([name]) - delete the exchange, if the name is not specified - is deleted with the current name
$ res = $ exchange-> publish (msg, routing_key, [parms])
$ res = $ exchange-> getResult (); - true / false result of the last operation

$ exchange = new AMQPExchange ($ cnn, name, [parms]) is equivalent to
$ exchange = new AMQPExchange ($ cnn);
$ res = $ exchange-> declare ();

Queue Class AMQPQueue

$ queue = new AMQPQueue (cnn, [name], [params]); - create a queue if the name is specified otherwise class initialization
$ res = $ queue-> delete ([name]) - delete the queue, returns the result of the operation
$ res = $ queue-> declaere ([name], [params]) - declaration of the queue, returns the result of the operation.
$ res = $ queue-> purge ([name]) - delete all the elements of the queue, returns the result of the operation
$ res = $ queue-> bind (exchange, routing_key, [parms]); - communication of queue and exchange
$ queueItem = $ queue-> getItem (); - get one item from the queue
$ arrayOfQueueItems = $ queue-> consume ([n]) - to receive an array of n-messages from the queue (all others are discarded) if n is not set - all messages are selected
$ queue-> getResult (); - true / false the result of the last operation, mainly intended for use of checking operations in the constructor

$ queue = new AMQPQueue (cnn, name, [params]); equivalent to:
$ queue = new AMQPQueue (cnn)
$ queue-> declaere (name, [params])

Although the object model does not quite correspond to the AMQP standard, but instead of one monolithic class, there are already four. In practice, the functionality has not changed, only methods for removing the queue and sharing have been added.

Source: https://habr.com/ru/post/70902/


All Articles