/** * Execute the console command. * * @return mixed */ public function handle() { // $queue = 'cloudstack'; $conn = new AMQPStreamConnection(Config::get('rabbitmq.host'), Config::get('rabbitmq.port'), Config::get('rabbitmq.user'), Config::get('rabbitmq.pass'), Config::get('rabbitmq.vhost')); $channel = $conn->channel(); while ($message = $channel->basic_get($queue)) { $messageData = json_decode($message->body); // Don't think we care about messages that have no status or event if (empty($messageData->status) || empty($messageData->event)) { $channel->basic_ack($message->delivery_info['delivery_tag']); continue; } // For the moment, we don't care about non-Completed messages. if (!in_array($messageData->status, ['Completed', 'Created'])) { $channel->basic_ack($message->delivery_info['delivery_tag']); continue; } if (in_array($messageData->event, ['SNAPSHOT.CREATE', 'SNAPSHOT.DELETE', 'VM.CREATE', 'VM.DESTROY'])) { $messageHandled = $this->parseMessage($messageData); } else { $messageHandled = true; } if ($messageHandled == true) { $channel->basic_ack($message->delivery_info['delivery_tag']); } } $channel->close(); $conn->close(); }
protected function getChannel() { if (null === $this->channel) { $this->channel = $this->connection->channel(); } return $this->channel; }
/** * @return ChannelInterface */ public function getChannel() { if (is_null($this->channel)) { $this->channel = new Channel($this, $this->connection->channel()); } return $this->channel; }
/** * @param AMQPStreamConnection $connection * @param string $queueName */ public function __construct(AMQPStreamConnection $connection, $queueName) { $this->connection = $connection; $this->channel = $this->connection->channel(); $this->queueName = $queueName; $this->channel->queue_declare($this->queueName, false, true, false, false); }
/** * */ private function connect() { if (!$this->connection) { $this->connection = new AMQPStreamConnection($this->host, $this->port, 'guest', 'guest'); $this->channel = $this->connection->channel(); $this->channel->exchange_declare($this->channel_name, 'fanout', false, false, false); } }
/** * Reconnect amqp connection */ public function reconnect() { if (!$this->amqpConnection->isConnected()) { $this->amqpConnection->reconnect(); $this->channel = $this->amqpConnection->channel(); $this->declaredQueues = []; } }
public function setUp() { $this->conn = $this->createConnection(); $this->ch = $this->conn->channel(); $this->ch->exchange_declare($this->exchange_name, 'direct', false, false, false); list($this->queue_name, , ) = $this->ch->queue_declare(); $this->ch->queue_bind($this->queue_name, $this->exchange_name, $this->queue_name); }
/** * @param string $exchangeName */ public function open($exchangeName) { if (null !== $this->channel) { return; } $channel = $this->connection->channel(); $channel->queue_declare($exchangeName, false, true, false, false); $this->channel = $channel; }
/** * Create a new RabbitMQ queue instance. * * @param \PhpAmqpLib\Connection\AMQPStreamConnection $amqpConnection * @param array $config */ public function __construct(AMQPStreamConnection $amqpConnection, $config) { $this->connection = $amqpConnection; $this->default = $config['queue']; $this->configQueue = $config['queue_params']; $this->configExchange = $config['exchange_params']; $this->declareExchange = $config['exchange_declare']; $this->declareBindQueue = $config['queue_declare_bind']; $this->channel = $this->connection->channel(); }
/** * @param $exchange_name */ private function connect($exchange_name) { if (null !== $this->channel) { return; } $this->channel = $this->connection->channel(); $this->channel->exchange_declare($exchange_name, 'fanout', false, true, false); $this->channel->queue_declare($exchange_name, false, true, false, false); $this->channel->queue_bind($exchange_name, $exchange_name); }
/** * @param AMQPStreamConnection $amqpConnection * @param string $queue Queue name * @param string $exchange Exchange name * @param null $key Routing key */ public function __construct(AMQPStreamConnection $amqpConnection, $queue, $exchange = 'jsonbus', $key = null) { $this->connection = $amqpConnection; $this->channel = $this->connection->channel(); $this->channel->exchange_declare($exchange, 'direct', false, true); $this->channel->queue_declare($queue, false, true); if (!$key) { $key = $queue; } $this->channel->queue_bind($queue, $exchange, $key); $this->exchange = $exchange; $this->queue = $queue; }
/** * First tries to create exchange using passive flag. * If exchange does not exist, creates new with provided settings. * * @param \PhpAmqpLib\Connection\AMQPStreamConnection $connection AMQP connection * @param string $name Exchange name * @param string $type Exchange type * @param array $params optional declare exchange parameters * @see self::$defaultDeclareExchangeParams */ public static function declareExchange($connection, $name, $type, $params = []) { $params = array_replace(self::$defaultDeclareExchangeParams, $params); $ch = $connection->channel(); try { $ch->exchange_declare($name, $type, true); } catch (AMQPProtocolChannelException $e) { $ch = $connection->channel(); if ($params['arguments'] and is_array($params['arguments'])) { $params['arguments'] = new AMQPTable($params['arguments']); } $ch->exchange_declare($name, $type, false, $params['durable'], $params['auto_delete'], $params['internal'], $params['nowait'], $params['arguments']); } }
/** * @param AMQPStreamConnection $conn */ public function __construct(AMQPStreamConnection $conn) { $this->conn = $conn; $this->channel = $conn->channel(); list($this->queueName) = $this->channel->queue_declare('', false, true, true, false); $this->channel->exchange_declare(self::TOPIC_NAME, self::MESSAGE_TYPE, false, true, true, false); }
/** * @param bool $forceNewChannell * * @return void */ protected function openChannel($forceNewChannell = false) { if (true == $forceNewChannell || false == $this->channel instanceof AMQPChannel) { $this->channel = $this->connection->channel(); $this->logger->info('New channel opened!', [$this->channel->getChannelId()]); } }
/** * Send an activity notice using AMQP * @param ActivityNotice $notice * @return bool */ public function sendActivityNotice($notice) { if (!isset($notice)) { return false; } /** @var array $setting */ $setting = $this->params['amqpSetting']; try { if ($this->amqpClientLibrary == "PhpAmqpLib") { $connection = new AMQPStreamConnection($setting['host'], $setting['port'], $setting['user'], $setting['password']); $channel = $connection->channel(); $msg = new AMQPMessage(JsonHelper::encode($notice)); $channel->basic_publish($msg, $setting['exchangeName'], $setting['routingKey']); $channel->close(); $connection->close(); } elseif ($this->amqpClientLibrary == "PECL") { $connection = new \AMQPConnection(['host' => $setting['host'], 'port' => $setting['port'], 'login' => $setting['user'], 'password' => $setting['password']]); $connection->connect(); if ($connection->isConnected()) { $channel = new \AMQPChannel($connection); $exchange = new \AMQPExchange($channel); $exchange->setName($setting['exchangeName']); $exchange->publish(JsonHelper::encode($notice), $setting['routingKey']); $connection->disconnect(); } } else { return false; } } catch (\Exception $e) { return false; } return true; }
protected function execute(InputInterface $input, OutputInterface $output) { // Connection a RabbitMQ $connection = new AMQPStreamConnection('rabbitmq', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->basic_qos(null, 1, null); // On ne traite pas plus de 1 message à la fois // Déclaration de la queue $channel->queue_declare($queue = 'tasks', $passive = false, $durable = true, $exclusive = false, $auto_delete = false); $output->writeln("<fg=black;bg=cyan>####################################################</>"); $output->writeln("<fg=black;bg=cyan>#### Waiting for messages. To exit press CTRL+C ####</>"); $output->writeln("<fg=black;bg=cyan>####################################################</>"); $callback = function ($msg) use($output) { $output->writeln(""); $output->writeln("--------------------------"); $output->writeln("<comment>Data reçu : </comment>"); $task = json_decode($msg->body, true); $output->writeln("<info>" . json_encode($task, JSON_PRETTY_PRINT) . "</info>"); $output->writeln(""); $output->writeln("<comment>Build de l'objet " . $task['class'] . " avec l'id " . $task['id'] . "</comment>"); $object = new $task['class']($task['id']); $output->writeln("<comment>Execution de la méthode</comment>"); $object->{$task}['methode'](); $output->writeln(""); $output->writeln("<comment>Fin de l'execution de la méthode</comment>"); $output->writeln("<comment>Envoi a RabbitMQ que la message à était traité</comment>"); $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); }; $channel->basic_consume($queue = 'tasks', $consumer_tag = '', $no_local = false, $no_ack = false, $exclusive = false, $nowait = false, $callback); while (count($channel->callbacks)) { $channel->wait(); } }
/** * @Route("/register", name="security_register") * @Method("POST") */ public function registerAction(Request $request) { $em = $this->getDoctrine()->getManager(); $request = Request::createFromGlobals(); $email = $request->request->get('email'); $password = $request->request->get('password'); $ico = trim($request->request->get('ico')); $user = new User(); $encoder = $this->container->get('security.encoder_factory')->getEncoder($user); $user->setPassword($encoder->encodePassword($password, $user->getSalt())); $user->setEmail($email); $user->setIco($ico); $backgroundImages = ['animal', 'corn', 'farming', 'chicken']; shuffle($backgroundImages); $user->setBackgroundImage($backgroundImages[0]); $profileImages = ['animal', 'corn', 'farming', 'chicken']; shuffle($profileImages); $user->setProfileImage($profileImages[0]); $em->persist($user); $em->flush(); $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles()); $this->get('security.token_storage')->setToken($token); //rabbit MQ $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->queue_declare('ico_queue', false, false, false, false); $json = json_encode(["userId" => $user->getId(), "ico" => $ico]); $msg = new AMQPMessage($json); $channel->basic_publish($msg, '', 'ico_queue'); dump(" [x] Sent '{$ico}'\n"); $channel->close(); $connection->close(); return $this->redirect($this->generateUrl('main_overview')); }
function forward_to_router($msg, $routing_key) { $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->queue_declare($routing_key, false, false, false, false); $channel->basic_publish($msg, '', $routing_key); $channel->close(); $connection->close(); }
function forward_to_translator($msg, $routing_key) { $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); $channel->exchange_declare('bank_translators_exchange', 'direct', false, false, false); $channel->basic_publish($msg, 'bank_translators_exchange', $routing_key); $channel->close(); $connection->close(); }
public function sendBuyServiceToRabbit($taskId, $resourceId) { $connection = new AMQPStreamConnection(RABBIT_URL, RABBIT_PORT, RABBIT_USER, RABBIT_PASSWORD); $channel = $connection->channel(); $channel->queue_declare($taskId, false, false, false, false); $msg = new AMQPMessage($resourceId); $channel->basic_publish($msg, '', $taskId); $channel->close(); $connection->close(); }
protected function initialize_translator() { $connection = new AMQPStreamConnection($this->translator_params['host'], $this->translator_params['port'], $this->translator_params['username'], $this->translator_params['password']); $channel = $connection->channel(); $channel->exchange_declare('bank_translators_exchange', 'direct', false, false, false); list($queue_name, , ) = $channel->queue_declare("", false, false, true, false); $this->translator_params['queue_name'] = $queue_name; $channel->queue_bind($queue_name, 'bank_translators_exchange', $this->translator_params['routing_key']); echo ' [*] Waiting for messages. To exit press CTRL+C', "\n"; $this->translator = array('connection' => $connection, 'channel' => $channel); }
public function it_properly_pops_job_off_of_rabbitmq(AMQPStreamConnection $connection, AMQPChannel $channel, AMQPMessage $message, Container $container) { $queue = 'default'; $connection->channel()->shouldBeCalled()->willReturn($channel); $channel->exchange_declare($queue, 'direct', false, true, false)->shouldBeCalled(); $channel->queue_declare($queue, false, true, false, false, false, null)->shouldBeCalled(); $channel->queue_bind($queue, $queue, $queue)->shouldBeCalled(); $channel->basic_get($queue)->shouldBeCalled()->willReturn($message); $this->setContainer($container); $this->pop()->shouldHaveType(RabbitMQJob::class); }
/** * Connects to AMQP and gets channel * * @return AMQPChannel */ private function getChannel() { if (!$this->channel) { $conn = new AMQPStreamConnection($this->connectionOptions['host'], $this->connectionOptions['port'], $this->connectionOptions['user'], $this->connectionOptions['pass'], $this->connectionOptions['vhost']); $ch = $conn->channel(); $ch->exchange_declare($this->exchange, $this->exchangeType, false, true, false); $ch->queue_declare($this->queue, false, true, false, false); $ch->queue_bind($this->queue, $this->exchange, $this->routingKey); $this->channel = $ch; } return $this->channel; }
/** * Initializes result objects: creates AMQP objects and binds result queue. * * @return void */ public function initialize() { $this->channel = $this->connection->channel(); // `auto-delete` argument makes sure that queue will be deleted after last queue // consumer disconnected. (Won't be deleted if there weren't any consumers) // `x-expires` queue option means that queue will be deleted if it's unused for the // specified time in milliseconds. Unused means that queue has no consumers, // the queue has not been redeclared, and basic.get has not been invoked. Util::declareQueue($this->connection, $this->taskId, ['exclusive' => false, 'durable' => true, 'auto_delete' => true, 'arguments' => ['x-expires' => self::EXPIRE_TIME]]); Util::declareExchange($this->connection, 'celeryresults', 'direct', ['exclusive' => false]); $this->channel->queue_bind($this->taskId, 'celeryresults', $this->taskId); }
/** * @param string $exchange * @param string $type * @param array $config * @throws ConnectionException */ public function __construct($exchange, $type = 'direct', $config = []) { $this->config = $this->buildConfig($config); $this->exchange = $exchange; $this->type = $type; try { $connection = new AMQPStreamConnection($this->config['host'], $this->config['port'], $this->config['username'], $this->config['password'], $this->config['vhost'], $this->config['connection']['insist'], $this->config['connection']['login_method'], $this->config['connection']['login_response'], $this->config['connection']['locale'], $this->config['connection']['timeout'], $this->config['connection']['read_write_timeout'], $this->config['connection']['context'], $this->config['connection']['keepalive'], $this->config['connection']['heartbeat']); $this->channel = $connection->channel(); $this->channel->exchange_declare($exchange, $type, $this->config['exchange']['passive'], $this->config['exchange']['durable'], $this->config['exchange']['auto_delete'], $this->config['exchange']['internal'], $this->config['exchange']['no_wait'], $this->config['exchange']['arguments'], $this->config['exchange']['ticket']); } catch (\Exception $e) { throw new ConnectionException('Carrot failed to build connection: ' . $e->getMessage()); } }
protected function getChannel() { if (empty($this->channel)) { try { $connection = new AMQPStreamConnection($this->config['host'], $this->config['port'], $this->config['user'], $this->config['password']); } catch (\ErrorException $ee) { sleep(2); return $this->getChannel(); } $this->channel = $connection->channel(); } return $this->channel; }
/** * First tries to create exchange using passive flag. * If exchange does not exist, creates new with provided settings. * * @param AMQPStreamConnection $connection AMQP connection. * @param string $name Exchange name. * @param string $type Exchange type. * @param array $params Optional declare exchange parameters. * * @return void * * @see self::$defaultDeclareExchangeParams */ public static function declareExchange(AMQPStreamConnection $connection, $name, $type, array $params = []) { $ch = $connection->channel(); $params = array_merge(self::$defaultDeclareExchangeParams, $params); if ($params['arguments'] and is_array($params['arguments'])) { $params['arguments'] = new AMQPTable($params['arguments']); } try { $ch->exchange_declare($name, $type, false, $params['durable'], $params['auto_delete'], $params['internal'], $params['nowait'], $params['arguments']); } finally { $ch->close(); } }
public function __construct() { $this->connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $this->channel = $this->connection->channel(); list($this->callback_queue, , ) = $this->channel->queue_declare("", false, false, true, false); $this->channel->basic_consume($this->callback_queue, '', false, false, false, false, [$this, 'on_response']); }
/** * @param AMQPStreamConnection $connection * @param string $exchange * @param array $defaultMessageParams */ public function __construct(AMQPStreamConnection $connection, $exchange, array $defaultMessageParams = null) { $this->connection = $connection; $this->exchange = $exchange; $this->defaultMessageParams = $defaultMessageParams; $this->channel = $this->connection->channel(); }
function forward_to_normalizer($response) { $connection = new AMQPStreamConnection('localhost', 5672, 'test', 'test'); $channel = $connection->channel(); $channel->queue_declare("bank_normalizer", false, false, true, false); $msg = new AMQPMessage($response); $channel->basic_publish($msg, '', 'bank_normalizer'); $channel->close(); $connection->close(); }