[rabbitmq-discuss] how to call a model from rabbitmq php consumer's callback in codeigniter?
Alvaro Videla
videlalvaro at gmail.com
Mon Apr 29 15:46:18 BST 2013
Hi,
The code for register_shutdown_function is used to call a function when PHP
is shutting down. For example if you want to do some resources clean up
(like closing database connections and so on), you could do it there.
If inside that shutdown function you need to call methods in the
controller, that is, your $this object, then you need to pass it as an
extra argument, as explained here:
http://php.net/manual/en/function.register-shutdown-function.php
On the other hand if you need to pass more arguments to the RabbitMQ
callback, the one that you use in your basic_consume function, the you need
to pass them to the callback function there, for example by using the 'use'
keyword as explained here: http://php.net/manual/en/functions.anonymous.php
Besides that I recommend you to refactor that consumer code out of the
controller into a consumer class or something similar. Keep in mind to
always keep thin controllers.
Also if your consumer callbacks need to use a lot of extra arguments, maybe
is better to create a class for them, instantiate an object and pass the
data inside via the constructor for example.
Regarding MySQL going away, you probably need to search in a php-mysql
forum to see how to solve that MySQL timeout. You could either increase the
timeout value in the server conf, issue a simple query every now and then
to keep the connection alive or implement a reconnection strategy, for
example by catching an exception.
Regards,
Alvaro
On Thu, Apr 25, 2013 at 8:30 AM, Seshachalam Malisetti <abbiya at gmail.com>wrote:
> 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 */
>
>
> _______________________________________________
> rabbitmq-discuss mailing list
> rabbitmq-discuss at lists.rabbitmq.com
> https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20130429/c08995e1/attachment.htm>
More information about the rabbitmq-discuss
mailing list