Seyi_Orion Posted July 18, 2018 Share Posted July 18, 2018 (edited) I made use of php-amqplib library Which I got from https://github.com/php-amqplib/php-amqplib but my rabbit MQ server keeps closing my connection once my queue messages are over 10,000 unprocessed messages but I have a script that makes use of the data but it seems the Rabbit MQ server can't verify that I am processing this messages. Here is the code sample i used: <?php date_default_timezone_set('Asia/Seoul'); require_once __DIR__ . '/vendor/autoload.php'; use PhpAmqpLib\Connection\AMQPStreamConnection; const WAIT_BEFORE_RECONNECT_uS = 3000; function cleanup_connection($connection) { // Connection might already be closed. // Ignoring exceptions. try { if($connection !== null) { $connection->close(); } } catch (\ErrorException $e) { } } $connection = null; while(true){ try { $connection = new AMQPStreamConnection( 'host goes here',// 5672, "guest", // Change to your user name 'guest' ); register_shutdown_function('shutdown', $connection); // Your application code goes here. consume_connection($connection); } catch(AMQPIOException $e) { echo "AMQP IO exception " . PHP_EOL; cleanup_connection($connection); usleep(WAIT_BEFORE_RECONNECT_uS); } catch(\RuntimeException $e) { echo "Runtime exception " . PHP_EOL; cleanup_connection($connection); usleep(WAIT_BEFORE_RECONNECT_uS); } catch(\ErrorException $e) { echo "Error exception " . PHP_EOL; cleanup_connection($connection); usleep(WAIT_BEFORE_RECONNECT_uS); } } function consume_connection($connection) { $queue = "****"; $channel = $connection->channel(); $channel->basic_qos(0,1000,false); $channel->basic_consume($queue, 'consumer', false, true,false,false,'process_message'); while (count($channel->callbacks)) { $channel->wait(); } } /** * @param \PhpAmqpLib\Message\AMQPMessage $msg */ function process_message($msg) { // $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); // i tried this.. still fails to work echo "\n--------\n"; echo $msg->body; echo "\n--------\n"; //Send data to database here!! } /** * @param \PhpAmqpLib\Connection\AbstractConnection $connection */ function shutdown($connection) { $connection->close(); } Edited July 18, 2018 by Seyi_Orion fixed typo Quote Link to comment Share on other sites More sharing options...
ginerjm Posted July 19, 2018 Share Posted July 19, 2018 I don't know anything about this amqplib software but some questions that I have are: 1 - How does your "while(true)" loop finish? Without understanding much of the code in this loop I must say that I don't see how you avoid making it an infinite loop. 2 - In this same loop you are doing some stuff that really should be done before beginning the loop. Why do a connect every time you iterate thru it? Do that up front and maintain the connection until the loop is complete (if it ever completes!). 3 - Turn on php error checking during your development cycle to help you debug any errors in your work. Quote Link to comment Share on other sites More sharing options...
Seyi_Orion Posted July 19, 2018 Author Share Posted July 19, 2018 This code is a cli script ( command line script) So the loop isn't really infinite but the program is never supposed to stop... Also in the while statement I am not reconnecting, I am requesting for data from a socket stream. There isn't any errors also in the script. There is a way to tell the socket server that I have processed the data I received but I seem not to be getting it... And because I am not getting it, the socket server disconnects after 10,000 data messages if it thinks I haven't processed the messages. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted July 19, 2018 Share Posted July 19, 2018 You are right in one thing. That loop will never stop unless something in it forces the exit. And I don't know what you are creating in the first line of your loop but it sure looks like a new instance of whatever. Is that really necessary instead of using one preset instance created prior to the loop as I mentioned? Good luck. As I said I don't know what this feature really is about. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.