issue with php-amqplib

131 views
Skip to first unread message

gmau...@me.com

unread,
Jun 11, 2015, 7:01:52 PM6/11/15
to rabbitm...@googlegroups.com
This is the problem.



root@sis-mqueue11:/var/apache2/2.2/htdocs/amqp# php amqp_publisher_with_confirms.php 

PHP Catchable fatal error:  Argument 1 passed to PhpAmqpLib\Channel\AMQPChannel::set_ack_handler() must be an instance of PhpAmqpLib\Channel\Callable, instance of Closure given, called in /var/apache2/2.2/htdocs/amqp/amqp_publisher_with_confirms.php on line 17 and defined in /var/apache2/2.2/htdocs/amqp/vendor/videlalvaro/php-amqplib/PhpAmqpLib/Channel/AMQPChannel.php on line 1045

PHP Stack trace:

PHP   1. {main}() /var/apache2/2.2/htdocs/amqp/amqp_publisher_with_confirms.php:0

PHP   2. PhpAmqpLib\Channel\AMQPChannel->set_ack_handler() /var/apache2/2.2/htdocs/amqp/amqp_publisher_with_confirms.php:17

root@sis-mqueue11:/var/apache2/2.2/htdocs/amqp# cat amqp_publisher_with_confirms.php 

<?php


use PhpAmqpLib\Connection\AMQPConnection;

use PhpAmqpLib\Message\AMQPMessage;


include(__DIR__ . '/config.php');


$exchange = 'someExchange';


$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);

$ch = $conn->channel();


$ch->set_ack_handler(

    function (AMQPMessage $message) {

        echo "Message acked with content " . $message->body . PHP_EOL;

    }

);


$ch->set_nack_handler(

    function (AMQPMessage $message) {

        echo "Message nacked with content " . $message->body . PHP_EOL;

    }

);


/*

 * bring the channel into publish confirm mode.

 * if you would call $ch->tx_select() befor or after you brought the channel into this mode

 * the next call to $ch->wait() would result in an exception as the publish confirm mode and transactions

 * are mutually exclusive

 */

$ch->confirm_select();


/*

    name: $exchange

    type: fanout

    passive: false // don't check is an exchange with the same name exists

    durable: false // the exchange won't survive server restarts

    auto_delete: true //the exchange will be deleted once the channel is closed.

*/


$ch->exchange_declare($exchange, 'fanout', false, false, true);


$i   = 1;

$msg = new AMQPMessage($i, array('content_type' => 'text/plain'));

$ch->basic_publish($msg, $exchange);


/*

 * watching the amqp debug output you can see that the server will ack the message with delivery tag 1 and the

 * multiple flag probably set to false

 */


$ch->wait_for_pending_acks();


while ($i <= 11) {

    $msg = new AMQPMessage($i++, array('content_type' => 'text/plain'));

    $ch->basic_publish($msg, $exchange);

}


/*

 * you do not have to wait for pending acks after each message sent. in fact it will be much more efficient

 * to wait for as many messages to be acked as possible.

 */

$ch->wait_for_pending_acks();


$ch->close();

$conn->close();

root@sis-mqueue11:/var/apache2/2.2/htdocs/amqp# 



Michael Klishin

unread,
Jun 11, 2015, 7:07:06 PM6/11/15
to rabbitm...@googlegroups.com, gmau...@me.com
On 12 June 2015 at 02:01:54, gmau...@me.com (gmau...@me.com) wrote:
> This is the problem.

Would you mind elaborating on this one liner  a bit? We are not great at mind reading
on this list.

We'd also appreciate if you use gist.github.com for code snippets
instead of posting green-on-black code walls to the list.
--
MK

Staff Software Engineer, Pivotal/RabbitMQ


gmau...@me.com

unread,
Jun 11, 2015, 7:12:40 PM6/11/15
to rabbitm...@googlegroups.com, gmau...@me.com
Yes, sorry. Tying to run the publisher with confirms got into problems registering the ack and nack callback functions.
Not to use my own code, i tried running the provided example in php-amqplib. The example fails indicating that for set_ack_handler(), and instance of Callable must be used, instead of Closure given.

gmau...@me.com

unread,
Jun 11, 2015, 7:14:38 PM6/11/15
to rabbitm...@googlegroups.com

Michael Klishin

unread,
Jun 11, 2015, 7:15:53 PM6/11/15
to rabbitm...@googlegroups.com, gmau...@me.com
On 12 June 2015 at 02:12:42, gmau...@me.com (gmau...@me.com) wrote:
> The example fails indicating that for set_ack_handler(),
> and instance of Callable must be used, instead of Closure given.

What version of PHP is used?

gmau...@me.com

unread,
Jun 11, 2015, 7:19:07 PM6/11/15
to rabbitm...@googlegroups.com, gmau...@me.com
Version of PHP is 5.3.8

admin@sis-mqueue11:~$ php -version

PHP 5.3.28 (cli) (built: Jun 17 2014 13:24:21) 

Copyright (c) 1997-2013 The PHP Group

Zend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies

    with Xdebug v2.2.0, Copyright (c) 2002-2012, by Derick Rethans

    with Suhosin v0.9.33, Copyright (c) 2007-2012, by SektionEins GmbH

admin@sis-mqueue11:~$ 



On Thursday, June 11, 2015 at 7:07:06 PM UTC-4, Michael Klishin wrote:

Alvaro Videla

unread,
Jun 12, 2015, 1:41:40 AM6/12/15
to gmau...@me.com, rabbitm...@googlegroups.com
The code here, works perfectly with PHP 5.4.6: https://github.com/videlalvaro/php-amqplib/blob/master/demo/amqp_publisher_with_confirms.php

Also as MK said, please try to share Github Gists, not this code copy&pasted with format; it's quite hard for us to read that.

If your function is not treated as an Anon Function in PHP, then that has to do with your PHP version probably, not with the library.

The functions on php-amqplib setting callback handlers don't type hint, they just check for is_callable: https://github.com/videlalvaro/php-amqplib/blob/master/PhpAmqpLib/Channel/AMQPChannel.php#L1441

This class error you get is because PHP doesn't scope the "Callable" interface properly for some reason. That's a PHP interface, it has nothing to do with this PHP library.

Can you use Anon Functions that way in your code with say, array_map?


--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-user...@googlegroups.com.
To post to this group, send email to rabbitm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

gmau...@me.com

unread,
Jun 12, 2015, 7:34:33 AM6/12/15
to rabbitm...@googlegroups.com, gmau...@me.com
Sorry for the copy and paste. This is standard out of the box solaris PHP V5.3. Not a PHP expert, but what is the difference between Callable and callable? 

gmau...@me.com

unread,
Jun 12, 2015, 7:40:25 AM6/12/15
to rabbitm...@googlegroups.com, gmau...@me.com
I just checked and both is_callable and array map work perfectly fine.I tried all the examples for the manual http://php.net/manual/en/function.is-callable.php and http://php.net/manual/en/function.array-map.php and they work.
The prototype in AQMPChannle.php referers to a Callable type. Where is that defined? I think the problem might be that V5.3, which is mentioned as required by the package php-amqplib, does not implement that Callable type? 


On Friday, June 12, 2015 at 1:41:40 AM UTC-4, Alvaro Videla wrote:

Alvaro Videla

unread,
Jun 12, 2015, 8:05:48 AM6/12/15
to gmau...@me.com, rabbitm...@googlegroups.com
The Callable interface came and went in the 5.3.x released I think. Perhaps upgrading PHP solves that.

gmau...@me.com

unread,
Jun 12, 2015, 8:08:32 AM6/12/15
to rabbitm...@googlegroups.com, gmau...@me.com
Unfortunately solaris supports (by Oracle) is limited to 5.3 of PHP. While we could download and compile, it would not be supported.
Is there a workaround?

Alvaro Videla

unread,
Jun 12, 2015, 8:09:43 AM6/12/15
to gmau...@me.com, rabbitm...@googlegroups.com
Try defining your callable in any of the other callable formats. The PHP docs on callable explain that.
Reply all
Reply to author
Forward
0 new messages