Пример #1
0
    private $counter = 0;
    private function count()
    {
        return $this->counter++;
    }
    public function consume($payload, AMQPEnvelope $envelope, AMQPQueue $queue)
    {
        if (rand(0, 100) > 90) {
            // throw exception with probability 0.1
            throw new Exception('Random exception');
        }
        echo "Received payload # {$this->count()} ", gettype($payload), PHP_EOL;
    }
    public function failure(Exception $e, AMQPEnvelope $envelope, AMQPQueue $queue)
    {
        echo "Failed to process payload # {$this->count()} due to exception: {$e->getMessage()}", PHP_EOL;
    }
    public function before(AMQPEnvelope $envelope, AMQPQueue $queue)
    {
        echo "Method ", __METHOD__, " called before consuming", PHP_EOL;
    }
    public function after(AMQPEnvelope $envelope, AMQPQueue $queue)
    {
        echo "Method ", __METHOD__, " called after consuming", PHP_EOL;
        $queue->cancel();
        // stop consumer to receive envelopes from server
    }
}
$exchange = new Generic('example.fanout', $config);
$exchange->listen(new EchoConsumer(), 'example.fanout.default');
Пример #2
0
    }
    public function getRadioStation()
    {
        return $this->radio_station;
    }
    public function consume($payload, AMQPEnvelope $envelope, AMQPQueue $queue)
    {
        if (rand(0, 100) > 95) {
            throw new Exception('some atmospheric disturbances');
        } elseif (rand(0, 100) > 85) {
            throw new Exception('low battery charge');
        } elseif (rand(0, 100) > 75) {
            throw new Exception('low audio quality');
        }
        echo "Received forecast #{$this->count()} from {$this->getRadioStation()} radio station: {$payload}, route key({$envelope->getRoutingKey()})", PHP_EOL;
    }
    public function failure(Exception $e, AMQPEnvelope $envelope, AMQPQueue $queue)
    {
        echo "Failed to receive forecast #{$this->count()} from {$this->getRadioStation()} radio station due to {$e->getMessage()}", PHP_EOL;
    }
    public function preConsume(AMQPQueue $queue)
    {
    }
    public function postConsume(AMQPQueue $queue)
    {
    }
}
$exchange = new Generic('example.topic.weather', Config::get('amqp_params'));
$receiver = new DemoReceiver();
$exchange->listen($receiver, "example.topic.weather.{$receiver->getRadioStation()}");