/** * Publish an order confirmation message to the queue. * * @param $observer Either a varien object or an event object, depending on where it's coming from * @param $key Routing key to where the message will be published * @param $type Content type of the message */ public function publishMessage($observer, $key, $type = "application/json") { //Converting observer to a proper order object if ($observer instanceof Varien_Event_Observer) { $order = $observer->getEvent()->getOrder(); } else { if ($observer instanceof Varien_Object) { $order = $observer; } } //Set mqmodule_status to 0 - indicating confirmation has not been sent yet $order->setMqmoduleStatus(0); $msg_body = $this->orderToJSON($order); extract($this->getConfigs()); try { //Establishing connection and channel with appropriate configs $conn = new PhpAmqpLib_Connection_AMQPConnection($host, $port, $user, $password, $vhost); $ch = $conn->channel(); //Set up and send the message $msg = new PhpAmqpLib_Message_AMQPMessage($msg_body, array('content_type' => $type, 'delivery_mode' => 2)); $ch->basic_publish($msg, $exchange, $key); $order->setMqmoduleStatus(1); //Closing the channel and connection $ch->close(); $conn->close(); } catch (Exception $e) { Mage::logException($e); Mage::log("Order confirmation failed to publish - mqmodule_status still set to 0. "); } }
public function listen() { //Configs from RabbitMQ Module extract(Mage::helper('mqmodule')->getConfigs()); if ($active == 1) { $consumer_tag = 'consumer'; //create connection to rabbit and open a channel $conn = new PhpAmqpLib_Connection_AMQPConnection($host, $port, $user, $password, $vhost); $ch = $conn->channel(); //define channel and attach queue and exchange $ch->queue_declare($queue, false, true, false, false); $ch->exchange_declare($exchange, 'topic', false, true, false); $ch->queue_bind($queue, $exchange); register_shutdown_function('shutdown', $ch, $conn); //create consumer on the queue that passes messages to callback function $ch->basic_consume($queue, $consumer_tag, false, false, false, false, 'process_message'); while (count($ch->callbacks)) { $ch->wait(null, false, 10); } $ch->basic_cancel($consumer_tag); } }
/** * @inheritdoc */ public function channel($channel_id = null) { $this->initLazyConnection(); return parent::channel($channel_id); }