ã¯ããã«
ãæ¬é£è¼ã§ã¯ãPHPä¸ã§åä½ããã¢ããªã±ã¼ã·ã§ã³ãã¬ã¼ã ã¯ã¼ã¯ã§ããZend Frameworkã«ã¤ãã¦ç´¹ä»ãã¦ããã¾ããä»åã¯åæ£ã¢ããªã±ã¼ã·ã§ã³éã§ã®ãã¼ã¿ã®ããã¨ããè¡ãããã®æ¹æ³ã®ä¸ã¤ã§ãããã¡ãã»ã¼ã¸ãã¥ã¼ããå©ç¨ããããã®Zend_Queueã³ã³ãã¼ãã³ããç´¹ä»ãã¾ãã
ãã¦ã¼ã¶ãããã¼ã¿ãåãåããããã®å¦çããããããªãµã¼ãã¹ããã£ãã¨ãã¾ãããã®å¦çã«æéããããããã§ããã°ãä¾ãã°ããã³ãã¨ã³ãã§ã¯ããã¼ã¿ãåãä»ãã¾ãããã¨ã ã表示ããããã¯ã¨ã³ãã§ãã¼ã¿ãå¦çããä»çµã¿ãå¿ è¦ã§ãããã®ããã³ãã¨ã³ãã¨ããã¯ã¨ã³ãã®ããã»ã¹ã®éã§ã®éä¿¡ã管çããã®ãã¡ãã»ã¼ã¸ãã¥ã¼ã§ãããã³ãã¨ã³ãããåãåã£ãå¦çã®ä¾é ¼ï¼ã¡ãã»ã¼ã¸ï¼ãåä»é ã«ä¸¦ã¹ã¦ãããè¦æ±ã«å¿ãã¦å é ããé ã«ããã¯ã¨ã³ãã«éä¿¡ãã¾ãã
ãZend_Queueã³ã³ãã¼ãã³ãã¯ããã®ã¡ãã»ã¼ã¸ãã¥ã¼ãä½æããæ©è½ãæä¾ãã¾ããä»åã¯ãã¾ãåºæ¬çãªZend_Queueã³ã³ãã¼ãã³ãã®ä½¿ãæ¹ã«ã¤ãã¦è¦ãå¾ããã¥ã¼ã®æ ¼ç´ã«MySQLã¨Apache ActiveMQãå©ç¨ããæ¹æ³ã«ã¤ãã¦è¦ã¦ããããã¨æãã¾ãã
対象èªè
ãPHPã®åºæ¬æ§æã«ã¤ãã¦ã¯ä¸éãç解ãã¦ããæ¹ã対象ã¨ãã¦ãã¾ãã
å¿ è¦ãªç°å¢
ãZend Frameworkã¯PHP 5.2.4以éã¨Webãµã¼ããã¤ã³ã¹ãã¼ã«ããã¦ããç°å¢ã§å©ç¨å¯è½ã§ããæ¬ç¨¿ã§ã¯Webãµã¼ãã¨ãã¦Apache 2.2ããOSã«Windows XPãæ¡ç¨ããã¢ããªã±ã¼ã·ã§ã³ãä½æãã¦ããã¾ãã
ã以ä¸ã«ãä»åã¢ããªã±ã¼ã·ã§ã³ä½æï¼åä½ç¢ºèªã«ç¨ããç°å¢ã示ãã¾ãï¼ã¤ã³ã¹ãã¼ã«ã«ããã£ã¦ã¯ææ°å®å®çã®ä½¿ç¨ãæ¨å¥¨ãã¾ãï¼ãåé ç®ã®è©³ç´°ãªã¤ã³ã¹ãã¼ã«æé ã¯ãããµã¼ããµã¤ãæè¡ã®å¦ã³è - WINGSãããï½¢ãµã¼ããµã¤ãç°å¢æ§ç¯è¨å®æé ï½£ãåç §ãã ããã
- Windows XP SP3
- PHP 5.3.3
- Apache 2.2.16
ãLinuxãFreeBSDãªã©UNIXç³»OSãã使ãã®æ¹ãã³ãã³ãã¯ã»ã¼ä¸ç·ã§ãã®ã§ããã¹ãªã©ã¯é©å®èªã¿æ¿ãã¦ãã ããã
ã¡ãã»ã¼ã¸ãã¥ã¼ã¨Zend_Queueã®åºæ¬
ãZend_Queueã¯ããã»ã¹ãã¹ã¬ããã®éã§ãã¼ã¿ã®éä¿¡ãããããã®ãã¡ãã»ã¼ã¸ãã¥ã¼ãä½æããã³ã³ãã¼ãã³ãã§ããã¡ãã»ã¼ã¸ãã¥ã¼ãå©ç¨ãããã¼ã¿ã®éä¿¡ã§ã¯ããã¼ã¿ï¼ã¡ãã»ã¼ã¸ï¼ãéããéä¿¡è ããåãåããåä¿¡è ãã¨ãã¡ãã»ã¼ã¸ã管çãããã¡ãã»ã¼ã¸ãã¥ã¼ããåå¨ãã¾ãï¼å³1ï¼ã
ãã¡ãã»ã¼ã¸ãã¥ã¼å ã§ã¯ã¡ãã»ã¼ã¸ã¯åºæ¬çã«ã¯ç»é²ãããé çªã«ä¸¦ãã§ãã¦ãæåã«è¿½å ããããã®ããé ã«åå¾ãããããã«ãªã£ã¦ãã¾ãããã®ãããããã«å¯¾å¿ããå¿ è¦ã¯ãªãããé çªã«å¯¾å¦ãã¦æ¬²ããå¦çã管çãããããªå ´é¢ã«åãã¦ãã¾ãã
ããªããå³1ãè¦ã¦åããã¨ãããéä¿¡è ã¨åä¿¡è ã¯ç´æ¥éä¿¡ãããããããã¡ãã»ã¼ã¸ãã¥ã¼ã®ã¿éä¿¡ããã¾ããé常ã®åæåããã»ã¹ééä¿¡ã§ã¯ããã¼ã¿ãéä¿¡ããããã»ã¹ã¨ãã¼ã¿ãåä¿¡ããããã»ã¹ã®åæ¹ãåãã¿ã¤ãã³ã°ã§éä¿¡ããå¿ è¦ããããããéä¿¡å¾ ã¡æéãªã©ã®ç¡é§ãçºçãã¾ããä¸æ¹ãã¡ãã»ã¼ã¸ãã¥ã¼ãå©ç¨ããå ´åã¯éä¿¡è ã¨åä¿¡è ãåæãåãå¿ è¦ããªãããï¼éåæéä¿¡ï¼ãç¡é§ãªå¦çãæ¸ãããã¨ãã§ãã¾ãã
é åãå©ç¨ããç°¡åãªãµã³ãã«
ãããã§ã¯ãã¾ãä¸ã¤ã®ããã»ã¹å ã§ã¡ãã»ã¼ã¸ãã¥ã¼ã使ãä¾ããè¦ã¦ããã¾ããããã¡ãã»ã¼ã¸ãã©ã®ããã«æ ¼ç´ãããã«ã¯ããã¤ãã®æ¹æ³ãé¸ã¹ãã®ã§ãããããã§ã¯é åï¼Arrayï¼ã«ã¡ãã»ã¼ã¸ãæ ¼ç´ããæ¹æ³ãå©ç¨ãããã¨ã«ãã¾ããé åãå©ç¨ããæ¹æ³ã¯ããã»ã¹ééä¿¡ã«ã¯ä½¿ãã¾ããããç°¡åã«å©ç¨ã§ãããããã¹ããªã©ã®ç¨éã«ã¯åãã¦ãã¾ãï¼ãªããæ ¼ç´æ¹å¼ã®ä¸è¦§ã«ã¤ãã¦ã¯æ¬¡ã®ã¢ããã¿ã®èª¬æãåç §ãã¦ä¸ããï¼ã
ãã¡ãã»ã¼ã¸ãã¥ã¼ã¸ã®æä½ã¯å¤§ããï¼1ï¼ã¡ãã»ã¼ã¸ãã¥ã¼ã®ä½æãï¼2ï¼ã¡ãã»ã¼ã¸ãã¥ã¼ã¸ã®ã¡ãã»ã¼ã¸ã®éä¿¡ãï¼3ï¼ã¡ãã»ã¼ã¸ãã¥ã¼ããã¡ãã»ã¼ã¸ã®åä¿¡ã®3種é¡ã«åãããã¨ãã§ãã¾ãï¼ãªã¹ã1-3ï¼ã
<?php require 'Zend/Queue.php'; /* ã¡ãã»ã¼ã¸ãã¥ã¼ã®ä½æ */ /* ãªãã·ã§ã³ã®æå® */ $options = array('name' => 'codezine_queue');//ï¼1ï¼ /* Arrayã¢ããã¿ãå©ç¨ãããã¥ã¼ã®ä½æ */ $queue = new Zend_Queue('Array', $options);//ï¼2ï¼
ãã¾ãã¡ãã»ã¼ã¸ãã¥ã¼ãä½æãã¦ãã¾ããï¼1ï¼ã§ã¡ãã»ã¼ã¸ãã¥ã¼ã¸ã®ãªãã·ã§ã³ã¨ãã¦ååãcodezine_queueããæå®ããä¸ã§ãï¼2ï¼ã§é åãå©ç¨ããã¡ãã»ã¼ã¸ãã¥ã¼ãä½æãã¦ãã¾ãã
/* ï¼3ï¼ã¡ãã»ã¼ã¸ã®éä¿¡ */ $queue->send('job 1'); $queue->send('job 2');
ãï¼3ï¼ã§ã¯ï¼1ï¼ã§ä½æããã¡ãã»ã¼ã¸ãã¥ã¼ã«Zend_Queueã¯ã©ã¹ã®sendã¡ã½ããã§ã¡ãã»ã¼ã¸ãéä¿¡ãã¦ãã¾ããããã§ã¯ã¡ãã»ã¼ã¸ãã¥ã¼ã«ãjob 1ãã¨ãjob 2ãã® 2ã¤ã®ã¡ãã»ã¼ã¸ãç»é²ããã¾ããã
/* ã¡ãã»ã¼ã¸ãæ大 5ã¤ã¾ã§åä¿¡ */ $messages = $queue->receive(5);//ï¼4ï¼ /* ããããã®ã¡ãã»ã¼ã¸ã«ã¤ã㦠*/ foreach ($messages as $i => $message) { /* ï¼5ï¼å®éã®å¦ç */ echo $message->body, "\n";//ããã§ã¯ã¡ãã»ã¼ã¸ã®ä¸èº«ã®ç¢ºèª /* ã¡ãã»ã¼ã¸ããã¥ã¼ããåé¤ */ $queue->deleteMessage($message);//ï¼6ï¼ }
ãâ¼
job 1 job 2
ãï¼4ï¼ï½ï¼6ï¼ã§ã¯ï¼3ï¼ã§éä¿¡ãããã¡ãã»ã¼ã¸ãåä¿¡ããå¦çãã¦ãã¾ããã¾ãï¼4ï¼ã§ã¯Zend_Queueã¯ã©ã¹ã®receiveã¡ã½ããã§ã¡ãã»ã¼ã¸ãåä¿¡ãã¦ãã¾ããããã§ä¸ãã¦ããå¼æ°ã®ã5ãã¯ãæ大5ã¤ã®ã¡ãã»ã¼ã¸ã¾ã§åä¿¡ããããã«æ示ãã¦ãã¾ãããã®æç¹ã§åä¿¡ãããã¡ãã»ã¼ã¸ã¯ãåä¿¡æ¸ã¿ã¨ãã¦ãã¼ã¯ããã次ã«reciveã¡ã½ãããå¼ã°ããæã«ã¯ãããã«ç¶ãã¡ãã»ã¼ã¸ãåä¿¡ããã¾ãã
ãreceiveã¡ã½ããã®è¿ãå¤ã¯SPLã¤ãã¬ã¼ã¿ãå®è£ ãã¦ããã®ã§ãforeachæã§ä¸èº«ã®åã¡ãã»ã¼ã¸ã«ã¢ã¯ã»ã¹ãããã¨ãã§ãã¾ããï¼5ï¼ã¯åã¡ãã»ã¼ã¸ãå®éã«å¦çããé¨åã§ããããã§ã¯ã¡ãã»ã¼ã¸æ¬æã«ã¢ã¯ã»ã¹ãåºåãã¦ãã¾ãããå®éã«ã¯ããè¤éãªå¦çãè¡ãã¾ããã¾ãï¼6ï¼ã§ã¯å¦çæ¸ã¿ã®ã¡ãã»ã¼ã¸ãã¡ãã»ã¼ã¸ãã¥ã¼ããåé¤ãã¦ãã¾ãã
ããªããä¸åreceiveã¡ã½ããã§åä¿¡ãããã¡ãã»ã¼ã¸ã§ããä¸å®æédeleteMessageã¡ã½ããã§åé¤ãããªãå ´åã«ã¯åä¿¡ããããã¨ãåãæ¶ããã¾ãããã®æéï¼ã¿ã¤ã ã¢ã¦ãæéï¼ã¯ãã¥ã¼ãã¨ã«ãã£ãã©ã«ãå¤ãæå®ãããã¨ãã§ãã¾ãããreceiveã¡ã½ããã§æ¸¡ããã¨ãã§ãã¾ããreceiveã¡ã½ããã§æå®ããå ´åã«ã¯2ã¤ç®ã®å¼æ°ã§æå®ãã¾ããä¾ãã°ã¿ã¤ã ã¢ã¦ãæéã10ç§ã«æå®ããå ´åã«ã¯ã$queue->receive(5, 10)ãã¨ãã¾ãã