[rabbitmq-discuss] how to call a model from rabbitmq php consumer's callback in codeigniter?

Seshachalam Malisetti abbiya at gmail.com
Thu Apr 25 07:30:05 BST 2013



I developed an android app where it subscribes to a queue and also 
publishes to other queues. At a time it publishes the same message to two 
different queues, one of them is a queue named "Queue" and now from a 
appfog instance i need to subscribe to the "Queue" and consume messages and 
insert them in a mysql db.

I created a php standalone app for the above purpose with codeigniter. By 
some reason the worker app loses its connection to rabbitmq. i would like 
to know the best way to do this. How can a worker app on appfog can sustain 
the application restarts.

what of kind of thing i need to use to solve the above problem.

I figured that the problem is not with rabbitmq connection. it is with the 
code related to mysql inserts. i checked the crash logs of my app and the 
error is "Mysql gone away". an example of php rabbitmq consumer has call 
backs for receive message and register_shutdown. in receive call back i can 
not use $this of code igniter because its out of scope and i was using 
get_instance(). i am not sure how to call a method from rabbitmq client 
receive call back function

The controller is 


<?php
if (!defined('BASEPATH'))
  exit('No direct script access allowed');

include(__DIR__ . '/php-amqplib/config.php');
use PhpAmqpLib\Connection\AMQPConnection;
class Welcome extends CI_Controller {
public function __construct() {
    parent::__construct();}
public function index() {
    //connect to rabbitmq and consume messages
    //insert messages to mysql
    //$this->messages = array();
    $exchange = "router";
    $queue = "abbiya";

    $conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
    $ch = $conn->channel();

    /*
      name: $queue
      passive: false
      durable: true // the queue will survive server restarts
      exclusive: false // the queue can be accessed in other channels
      auto_delete: false //the queue won't be deleted once the channel is closed.
     */
    $ch->queue_declare($queue, false, true, false, false);

    $ch->queue_bind($queue, $exchange, $queue);

    /*
      queue: Queue from where to get the messages
      consumer_tag: Consumer identifier
      no_local: Don't receive messages published by this consumer.
      no_ack: Tells the server if the consumer will acknowledge the messages.
      exclusive: Request exclusive consumer access, meaning only this consumer can access the queue
      nowait:
      callback: A PHP Callback
     */
    $consumer_tag = "abbiya";

    $ch->basic_recover(true);
    $ch->basic_consume($queue, $consumer_tag, false, false, false, false, function($msg) {
                $message_body = json_decode($msg->body);
                $msg->delivery_info['channel']->
                        basic_ack($msg->delivery_info['delivery_tag']);

                // Send a message with the string "quit" to cancel the consumer.
                if ($msg->body === 'quit') {
                    $msg->delivery_info['channel']->
                            basic_cancel($msg->delivery_info['consumer_tag']);
                }
                $data = array(
                    'sender_id' => $message_body->r,
                    'receiver_id' => $message_body->s,
                    'message_content' => $message_body->m,
                    // 'sent_time' => $message_body->t,
                    'status' => 0
                );
                $ci =& get_instance();
                $ci->Message_model->newMessage($data);
            }
    );

    // Loop as long as the channel has callbacks registered
    while (count($ch->callbacks)) {
        $ch->wait();
    }

    register_shutdown_function(function() use ($ch, $conn) {
                $ch->close();
                $conn->close();
                $this->index();
            }
    );}
}
/* End of file welcome.php *//* Location: ./application/controllers/welcome.php */

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20130424/5393b3fc/attachment.htm>


More information about the rabbitmq-discuss mailing list