note 107682 deleted from amqp.examples by sobak

From: Date: Thu, 24 Nov 2016 20:34:54 +0000
Subject: note 107682 deleted from amqp.examples by sobak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-207592@lists.php.net to get a copy of this message
Note Submitter: info at eeasoftware dot com ---- I spent several hours looking for some examples of sending messages to worker queues vs. fan queues. The documentation isn't too clear and I had to consult many sources, so here are simple methods for both types of 'producers'. For brevity I've left out 'safety' checks in the code dealing with whether or not a connection was established, error checking, cleanup, etc. As further clarification, I'm using rabbitmq as my service, with java daemons running on the server as 'consumers'. These examples are current with the new 1.0.0 stable AMQP release in February of 2012. <?php //set your connection arguments and connect to the server $conn_args = array('host' => 'your_host', 'port' => 'your_host_port', 'login' => 'your_username', 'password' => 'your_password'); $conn = new AMQPConnection($conn_args); $conn->connect(); //create your message $message = 'Hello World!'; //create a channel and exchange $channel = new AMQPChannel($conn); $exchange = new AMQPExchange($channel); //Here is where the code splits for the different types of queues: //------------------------------------------------- //For Fan Type Queues (One message will be consumed by many consumers - each on a queue of their own) //set the exchange name and publish the message $exchange->setName('exchange_name'); $exchange->publish($message, 'your_routing_key'); //for Fan Queue calls not bound to a particular key, the 'routing_key' will simply be ignored //------------------------------------------------- //For Worker Type Queues (Many consumers are listening to a single queue, and a single message will be passed only to the next available consumer waiting in line) //start a transaction $channel->startTransaction(); //publish your message with 'your_queue_name' as the 'routing_key' $exchange->publish($message, 'your_queue_name'); //commit your transaction $channel->commitTransaction(); //------------------------------------------------- //psuedo code: //clean up $channel; - probably just unset($channel) will do. //clean up $exchange; - probably just unset($exchange) will do. //disconnect from server $conn->disconnect(); ?>

« previous php.notes (#207592) next »