有点旧的帖子,但以防其他人来寻求帮助:
您似乎正在使用 old_sound 的 rabbitmq 捆绑包。它在这里有一些有用的教程类型的文档:https://github.com/videlalvaro/RabbitMqBundle
它帮助我在 symfony 中使用 rabbitmq。
简而言之:
1:您需要在 config.yml 文件中进行一些配置。例如:
# RabbitMQ Configuration
old_sound_rabbit_mq:
connections:
default:
host: 'localhost'
port: 5672
user: 'guest'
password: 'guest'
vhost: '/'
lazy: true
connection_timeout: 3
read_write_timeout: 3
# requires php-amqplib v2.4.1+ and PHP5.4+
keepalive: false
# requires php-amqplib v2.4.1+
heartbeat: 0
producers:
task_example:
connection: default
exchange_options: {name: 'task_example', type: direct}
consumers:
task_example:
connection: default
exchange_options: {name: 'task_example', type: direct}
queue_options: {name: 'task_example'}
callback: test_class
这里定义了连接,一个生产者和一个消费者。两者都使用相同的“默认”连接。
您还需要将回调定义为服务:
# My services
services:
test_class:
class: AppBundle\Testclasses\rabbittest\testclass
arguments: [@logger]
2:现在你需要有消费者,也就是这里的回调选项,“test_class”。简单的消费者可能如下所示:
namespace AppBundle\Testclasses\rabbittest;
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
use PhpAmqpLib\Message\AMQPMessage;
class testclass implements ConsumerInterface
{
private $logger; // Monolog-logger.
// Init:
public function __construct( $logger )
{
$this->logger = $logger;
echo "testclass is listening...";
}
public function execute(AMQPMessage $msg)
{
$message = unserialize($msg->body);
$userid = $message['userid'];
// Do something with the data. Save to db, write a log, whatever.
}
}
3:现在您已经拥有的制作人:
$msg = array('userid' => $someid);
$this->get('old_sound_rabbit_mq.task_example_producer')->publish(serialize($msg));
4:最后一个难题是运行消费者。消费者是从控制台启动的,我是在 Windows 机器上开发的,并且使用了 Windows PowerShell。你可以这样启动消费者:
php app/console rabbitmq:consumer task_example
它应该给你文本:
testclass 正在听...
,如果你从这个例子中复制了它。该文本不是必需的,没有它,控制台将不会输出任何内容,但可以正常工作。除非出现一些错误。
但请记住,您必须位于 symfony 应用程序所在的目录中。例如:
C:\wamp\www\symfony\my_project