php-amqplib with libevent

188 views
Skip to first unread message

cxf

unread,
Jan 3, 2012, 10:56:20 PM1/3/12
to php-amqplib-devel
hi,
I want use php-amqplib and libevent, in receiver, i get the socket
in AMQPConnection (just return $this->sock ) and set it in libevent,
when sending messaget to queue,the receiver has no action (the
callback is not even invoked at all ); is there anyting wrong, thanks
very much.

receiver code as followers:

$exchange = 'router';
$queue = 'msgs';
$consumer_tag = 'consumer';

function getMsg($fd, $events, $arg) {
$ch = $arg[0];
$queue = $arg[1];
$msg = $ch->basic_get($queue, true);
print_r($msg);
print("get a messge:\n");
}

$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$ch = $conn->channel();
$ch->queue_declare($queue, false, true, false, false);
$ch->exchange_declare($exchange, 'direct', false, true, false);
$ch->queue_bind($queue, $exchange);

$fd = $conn->get_socket();

//$ch->basic_consume($queue, $consumer_tag, false, true, false, false,
"getMsg1");

$base_fd = event_base_new();
$event_fd = event_new();
event_set($event_fd, $fd, EV_READ|EV_PERSIST, "getMsg", array($ch,
$queue));
event_base_set($event_fd, $base_fd);
event_add($event_fd);
event_base_loop($base_fd);

Jost Baron

unread,
Mar 24, 2016, 4:54:17 AM3/24/16
to php-amqplib-devel
I've had the same problem. The problem is that RabbitMQ only starts to deliver messages to the consumer if it told RabbitMQ that it wants to receive messages (i.e. it called basic_consume on the queue).

Solution: Call basic_consume on the queue in your initialization so you get messages on the socket, and then call $channel->wait() whenever the event fired. It should also be more efficient than using basic_get.

akayz...@gmail.com

unread,
Nov 29, 2017, 8:03:56 AM11/29/17
to php-amqplib-devel
hi,I've had the same problem.  do you know the answer now?

在 2012年1月4日星期三 UTC+8上午11:56:20,cxf写道:
Reply all
Reply to author
Forward
0 new messages