{ return true; } public function getFalse() { return false; } public function getNull() { return null; } public function getZero() { return 0; } public function getEmptystr() { return ''; } } $exchange = new Generic('example.fanout', $config); $producer = new EchoProducer(); $methods = get_class_methods($producer); $count = 0; while (true) { $method = $methods[array_rand($methods)]; $exchange->send($producer->{$method}()); echo "Sent message #{$count} ({$method})" . PHP_EOL; $count++; usleep(500000); }
{ $continent_temperature = 'get' . ucfirst($continent) . 'Temperature'; return "{$this->{$continent_temperature}()} weather every week in {$continent}"; } public function getDaily($continent) { $continent_temperature = 'get' . ucfirst($continent) . 'Temperature'; return "{$this->{$continent_temperature}()} weather every day in {$continent}"; } private function getAfricaTemperature() { return 'warm'; } private function getEuropeTemperature() { return 'cold'; } } $exchange = new Generic('example.topic.weather', Config::get('amqp_params')); $forecast = new DemoStation(); $count = 0; while (true) { $continent = $forecast->continents[array_rand($forecast->continents)]; $term = $forecast->terms[array_rand($forecast->terms)]; $method = 'get' . ucfirst($term); $weather = $forecast->{$method}($continent); $exchange->send($weather, "example.topic.weather.{$continent}.{$term}"); echo "Sent {$continent} {$term} forecast #{$count}: {$weather}" . PHP_EOL; $count++; usleep(500000); }
{ 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');
echo "Receiver listen to {$this->radio_station} radio station", PHP_EOL; } 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()}");