Last active
August 29, 2015 13:56
-
-
Save nelsonsar/9213287 to your computer and use it in GitHub Desktop.
Producer e consumer esperados como resultado no "hands on" da Dafiti Tech Conference 2014
This file contains hidden or 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
| <?php | |
| $connection = new \AMQPConnection( | |
| array( | |
| 'host' => '69.195.223.58', | |
| 'port' => 5672, | |
| 'vhost' => "/", | |
| 'login' => "dafitconf", | |
| 'password' => "dafiti", | |
| ) | |
| ); | |
| if ($connection->connect()) { | |
| $channel = new \AMQPChannel($connection); | |
| $queue = new \AMQPQueue($channel); | |
| //Seu nome de usuário seria o nome da fila | |
| $queue->setName("nelson"); | |
| //Declaramos a fila como exclusiva para termos a capacidade | |
| //de ficar "online" quando o consumer estiver ativo, ou seja, | |
| //estamos recebendo mensagens e "offline" quando pararmos o | |
| //consumer. | |
| $queue->setFlags(\AMQP_EXCLUSIVE); | |
| $queue->declareQueue(); | |
| } | |
| while (true) { | |
| //Aqui na verdade deveríamos ou fazer um POST pra uma url | |
| //de callback ou simplesmente persistir a mensagem no banco | |
| $result = ''; | |
| $queue->consume(function(\AMQPEnvelope $message, \AMQPQueue $queue) use (&$result) { | |
| $result = $message->getBody(); | |
| if (false === empty($result)) { | |
| $queue->ack($message->getDeliveryTag()); | |
| } | |
| return false; | |
| }); | |
| echo $result . PHP_EOL; | |
| } |
This file contains hidden or 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
| <?php | |
| $connection = new \AMQPConnection( | |
| array( | |
| 'host' => '69.195.223.58', | |
| 'port' => 5672, | |
| 'vhost' => "/", | |
| 'login' => "dafitconf", | |
| 'password' => "dafiti", | |
| ) | |
| ); | |
| if ($connection->connect()) { | |
| $channel = new \AMQPChannel($connection); | |
| $exchange = new \AMQPExchange($channel); | |
| //Como usaremos a exchange padrão não precisamos | |
| //declarar nenhum nome e nem nada. A única coisa que | |
| //precisa ser definida é para qual usuário queremos mandar | |
| //a mensagem. | |
| $exchange->publish("Oi", "nelson"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment