Giter Club home page Giter Club logo

Comments (1)

KinaneD avatar KinaneD commented on August 21, 2024

In order to support direct and topic exchanges while assuring that messages are not lost. We should figure out a way to bind a queue to an exchange specifying a binding key and to publish a message with a matching routing key.

Messages can be lost in the case where a producer is publishes messages to an exchange while there are no queue bound to that exchange yet.

Since a routing key is required on the producer side, and a matching binding key on the consumer's side, we should somehow provide a way to map the routing key to its corresponding binding key by either:
1- Leave it as the users responsibility.
2- Declaring the queues and exchanges on both the producer and consumer's side alike.

Producer

public function __construct(Connection $connection, $exchangeName, $exchangeType, $queueName, $routingKey, $passive = false, $durable = true, $autoDelete = false, $deliveryMode = 2)
{
   $this->connection = $connection;
   $this->exchangeName = $exchangeName;
   $this->exchangeType = $exchangeType;
   $this->queueName = $queueName;
   $this->passive = $passive;
   $this->durable = $durable;
   $this->autoDelete = $autoDelete;
   $this->deliveryMode = $deliveryMode;
   $this->routingKey = $routingKey;   //Since these should match
   $this->bindingKey = $routingKey;   //Since these should match
}

public function publish($data)
{
   $this->connection->getChannel()->exchange_declare($this->exchangeName, $this->exchangeType, $this->passive, $this->durable, $this->autoDelete);

   $this->connection->getChannel()->queue_declare($this->queueName, $this->passive, $this->durable, false, $this->autoDelete);

   $this->connection->getChannel()->queue_bind($this->queueName, $this->exchangeName, $this->bindingKey);

   $msg = new AMQPMessage($data, ['delivery_mode' => $this->deliveryMode]);

   $this->connection->getChannel()->basic_publish($msg, $this->exchangeName, $this->routingKey);
}

Consumer

public function __construct(Connection $connection, $exchangeName, $exchangeType, $queueName, $bindingKey, $passive = false, $durable = true, $autoDelete = false, $deliveryMode = 2)
{
   $this->connection = $connection;
   $this->exchangeName = $exchangeName;
   $this->exchangeType = $exchangeType;
   $this->queueName = $queueName;
   $this->passive = $passive;
   $this->durable = $durable;
   $this->autoDelete = $autoDelete;
   $this->deliveryMode = $deliveryMode;
   $this->bindingKey = $bindingKey;
}

public function listenToQueue($handlerClass, ExceptionHandler $exceptionHandler)
{
   $this->connection->getChannel()->exchange_declare($this->exchangeName, $this->exchangeType, $this->passive, $this->durable, $this->autoDelete);

   $this->connection->getChannel()->queue_declare($this->queueName, $this->passive, $this->durable, false, $this->autoDelete);

   $this->connection->getChannel()->queue_bind($this->queueName, $this->exchangeName, $this->bindingKey);

$handler = new $handlerClass;

$callback = function ($msg) use ($handler, $exceptionHandler) {
   //
};

$this->connection->getChannel()->basic_qos(null, 1, null);
$this->connection->getChannel()->basic_consume($queue_name, '', false, false, false, false, $callback);

while (count($this->connection->getChannel()->callbacks)) {
    $this->connection->getChannel()->wait();
}
}

the above implementation example doesn't account for DLX.

from bowler.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.