2013-04-22 2 views
1

나는 대기열을 구독하고 다른 대기열에 게시하는 안드로이드 응용 프로그램을 개발했습니다. 한 번에 두 개의 다른 대기열에 같은 메시지를 게시합니다. 그 중 하나는 "대기열"이라는 대기열이고 이제는 "대기열"에 가입하고 메시지를 소비하고이를 mysql db에 삽입해야하는 appfog 인스턴스입니다.codeigniter에서 rabbitmq php 소비자 콜백에서 모델을 호출하는 방법은 무엇입니까?

위의 목적으로 codeigniter를 사용하여 PHP 독립 실행 형 응용 프로그램을 만들었습니다. 어떤 이유로 작업자 응용 프로그램이 rabbitmq와의 연결을 끊습니다. 나는 이것을하는 가장 좋은 방법을 알고 싶다. appfog에있는 작업자 응용 프로그램은 어떻게 응용 프로그램을 다시 시작할 수 있습니까?

내가 위의 문제를 해결하는 데 사용해야하는 것은 무엇인가.

나는 rabbitmq 연결에 문제가 없다고 생각했다. mysql 인서트와 관련된 코드가있다. 내 응용 프로그램의 오류 로그를 확인하고 오류는 "MySQL을 멀리 갔다"입니다. php rabbitmq 소비자의 예는 수신 메시지와 register_shutdown에 대한 콜 백 (call backs)을 가지고있다. 수신 전화를 다시 나는 그것의 범위와 내가 get_instance()를 사용했기 때문에 나는이 코드 점화 장치를 $ 사용할 수 없다. 나는 좋은 아이디어를 제외하지 않을 수 CodeIgniter의 및 PHP와 소비자를 만드는 말을 컨트롤러는

<?php 

if (!defined('BASEPATH')) 
    exit('No direct script access allowed'); 

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

use PhpAmqpLib\Connection\AMQPConnection; 

class Welcome extends CI_Controller { 

public function __construct() { 
    parent::__construct(); 
} 

public function index() { 
    //connect to rabbitmq and consume messages 
    //insert messages to mysql 
    //$this->messages = array(); 
    $exchange = "router"; 
    $queue = "abbiya"; 

    $conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST); 
    $ch = $conn->channel(); 

    /* 
     name: $queue 
     passive: false 
     durable: true // the queue will survive server restarts 
     exclusive: false // the queue can be accessed in other channels 
     auto_delete: false //the queue won't be deleted once the channel is closed. 
    */ 
    $ch->queue_declare($queue, false, true, false, false); 

    $ch->queue_bind($queue, $exchange, $queue); 

    /* 
     queue: Queue from where to get the messages 
     consumer_tag: Consumer identifier 
     no_local: Don't receive messages published by this consumer. 
     no_ack: Tells the server if the consumer will acknowledge the messages. 
     exclusive: Request exclusive consumer access, meaning only this consumer can access the queue 
     nowait: 
     callback: A PHP Callback 
    */ 
    $consumer_tag = "abbiya"; 

    $ch->basic_recover(true); 
    $ch->basic_consume($queue, $consumer_tag, false, false, false, false, function($msg) { 
       $message_body = json_decode($msg->body); 
       $msg->delivery_info['channel']-> 
         basic_ack($msg->delivery_info['delivery_tag']); 

       // Send a message with the string "quit" to cancel the consumer. 
       if ($msg->body === 'quit') { 
        $msg->delivery_info['channel']-> 
          basic_cancel($msg->delivery_info['consumer_tag']); 
       } 
       $data = array(
        'sender_id' => $message_body->r, 
        'receiver_id' => $message_body->s, 
        'message_content' => $message_body->m, 
        // 'sent_time' => $message_body->t, 
        'status' => 0 
       ); 
       $ci =& get_instance(); 
       $ci->Message_model->newMessage($data); 
      } 
    ); 

    // Loop as long as the channel has callbacks registered 
    while (count($ch->callbacks)) { 
     $ch->wait(); 
    } 

    register_shutdown_function(function() use ($ch, $conn) { 
       $ch->close(); 
       $conn->close(); 
       $this->index(); 
      } 
    ); 
} 

} 

/* End of file welcome.php */ 
/* Location: ./application/controllers/welcome.php */ 

답변

2

우선이다 기능을 다시

전화 rabbitmq 클라이언트에서 방법을 수신 호출하는 방법을 잘 모르겠습니다

if(!$this->input->is_cli_request()){ 
     show_404(); // So they dont know this is an actual script 
     die(); 
    } 

을 추가하고 nohup을 사용하여 명령 줄에서 실행하거나 daemonize하십시오.

codeigniter의 $ this-> db-> reconnect();를 사용할 수 있습니다. DB를 정상적으로 다시 연결하십시오.

우리는 php와 CI를 사용하여 테스트 용으로 약간의 소비자를 만들었습니다. 우리는 콜백 함수를 도우미에 넣고 call_back 함수가 false를 반환하는 방법을 추가하여 소비자가 끝날 때 청취를 중단합니다.

우리의 생산 시스템을 위해 우리는 python, java 또는 낙타 소비자를 사용하는데 이는 작업에 훨씬 적합합니다.

관련 문제