15 May 2012

RabbitMQ PHP in Ubuntu 12.04

Testing RabbitMQ PHP in Ubuntu 12.04
Examples for AMQP in official PHP site (last check 2012/05/15) not sync with API for PECL 1.0.1.
This is my version to test simple php amqp

emit_log.php:

// Create a connection
$cnn = new AMQPConnection(array('login'=>'guest', 'password'=>'abc123'));
$cnn->connect();

// Create a channel
$ch = new AMQPChannel($cnn);

// Declare a new exchange
$ex = new AMQPExchange($ch);
$ex->setName('logs');
$ex->setType(AMQP_EX_TYPE_FANOUT);
$ex->declare();

// Publish a message to the exchange with a routing key
$message = isset($argv[1]) ? $argv[1] : 'hello world message';
$ex->publish($message, 'routing.key');

receive_log.php:

// Create a connection
$cnn = new AMQPConnection(array('login'=>'guest', 'password'=>'abc123'));
$cnn->connect();


// Create a channel
$ch = new AMQPChannel($cnn);


// Declare a new exchange
$ex = new AMQPExchange($ch);
$ex->setName('logs');
$ex->setType(AMQP_EX_TYPE_FANOUT);


// Create a new queue
$q = new AMQPQueue($ch);
$q->declare();
$q->bind('logs', 'routing.key');


function cb($env, $q) {
    print $env->getBody()."\n";
}
// Read from the queue
$msg = $q->consume('cb');


Easier to manage rabbitmsq server with the rabbitmq_management than the rabbitmqctl
Example based on rabbitmq Publish/Subscribe tutorial

7 comments:

Anonymous said...

Glass Partitioning London | Glass Office Partitions in London

Anonymous said...

student trips europe

trybestthing said...

I really thanked to you that you given that much information for us. iam waiting for you next post.


i repair
i-repair

Unknown said...

MaxiMove LTD are a family run removal company offering home removals and same-day collection and delivery services at a competitive price. It's our goal to listen to our customers and put their requirements first and to provide you with an efficient, reliable and personalised service ensuring your moving day runs smoothly.
Moving company St Helens

imran khan said...

Excellent article. I'm sure the post will be good, and I'm astonished you wrote it, man! Excellent work, and please keep it up! I couldn't agree more, and I've saved this to Pocket as well! Keep it up, dude.

sobuj said...

Thanks for your information. Are you find best online store? ZarsBuy is an online store that offers the highest-quality goods at the most reasonable pricing.

Sarah J. said...

I really liked your article on how to use RabbitMQ with PHP. I've been looking for some more information on how to use this type of technology and it was really helpful to find such a well written article. The blog post has a lot of great content and I loved the code snippets, too. I learned a lot and it was really helpful. Social Media Marketing