示例#1
0
    public function __construct()
    {
        $this->connection = new AMQPConnection(['localhost', 5672, 'guest', 'guest']);
        $this->connection->connect();
        $this->channel = new AMQPChannel($this->connection);
        $this->queue = new AMQPQueue($this->channel);
        $this->queue->setName("queue-hello-world");
        $this->queue->declareQueue();
        $this->exchange = new AMQPExchange($this->channel);
        $this->exchange->setName("exchange-hello-world");
        $this->exchange->setType(AMQP_EX_TYPE_FANOUT);
        $this->exchange->declareExchange();
        $this->queue->bind($this->exchange->getName());
        $this->exchange->publish('Hello World!');
        $this->exchange->publish('QUIT');
    }
}
// We use PCNTL to create new processes to handle
// different ends of the queuing systems.
if (!extension_loaded("pcntl")) {
    die("PCNTL module not installed");
}
$pid = pcntl_fork();
if ($pid == -1) {
    die("Fork failed\n.");
} elseif ($pid) {
    $consumer = new TutorialConsumer();
    $consumer->consume();
} else {
    $producer = new TutorialProducer();
}
示例#2
0
$pid = pcntl_fork();
if ($pid == -1) {
    die("Fork failed\n.");
} elseif ($pid) {
    $logger = new TutorialProducer();
    sleep(1);
    // Wait for consumers to start and bind their queues.
    $logger->sendInfo("Info log 1");
    $logger->sendError("Error log 1");
    $logger->sendInfo("Info 1og 2");
    $logger->sendError("Error log 2");
    $logger->sendInfo("QUIT");
    $logger->sendError("QUIT");
    sleep(1);
} else {
    // Additional forks to create three consumers
    $pid = pcntl_fork();
    if ($pid) {
        if (pcntl_fork()) {
            $logger = new TutorialConsumer("anon.info");
            $logger->consume();
        } else {
            $logger = new TutorialConsumer("anon.error");
            $logger->consume();
        }
    } else {
        // This comsumer gets all logs
        $logger = new TutorialConsumer("anon.*");
        $logger->consume();
    }
}