Natty’s blog

Stay hungry. Stay foolish. — Steve Jobs

[PHP] การเพิ่ม Performance ให้กับ PHP+MySQL ด้วย xDebug,APC,memcache

Posted by natty on June 10, 2009

เขียนโปรแกรมเล็กๆ ไม่ค่อยมีปัญหาอะไร แต่เมื่อไหร่ก็ตาม ที่งานของเรา ต้องทำงานกับข้อมูลเยอะๆ และ Application ขนาดใหญ่ มีหลาย module โดยเฉพาะ หากใช้ object-oriented ด้วยแล้ว แม้จะมีประโยชน์ในด้านของการออกแบบก็จริง แต่ด้านของ performance ก็มีผลกระทบในแง่ร้ายเช่นกัน ดังนั้น จึงควรที่จะเรียนรู้เกี่ยวกับการ config ค่าต่าง ๆ ทั้งในส่วนของ PHP และ MySQL ค่ะ

blog นี้จะไม่กล่าวถึงการ install ในขั้นต้นนะคะ โดยเป็น Apache Server บน linux ค่ะ

MySQL

เริ่มกันก่อนในส่วน MySQL

ปกติ หากเราลง MySQL แบบ default เราจะใช้ my.ini แบบที่กำหนดมาเป็น default แต่หากเราต้องการจะ modify ให้ใช้งานและรองรับข้อมูลจำนวนมาก ให้เราลองสังเกตใน folder MySQL ของเรา จะมีไฟล์ config อีกมากมายเลย เช่น my-huge.ini, my-innodb-heavy-4G.ini, my-large.ini, my-medium.ini, my-small.ini, my-template.ini ซึ่งใน file พิเศษๆ เหล่านี้ จะมีการ set ค่าเหล่านี้ ยกตัวอย่างจาก my-huge.ini

key_buffer = 384M
max_allowed_packet = 1M
table_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M

ซึ่งแต่ละ File ก็จะมีการตั้งค่าแตกต่างกันไป วิธีการเอาไปใช้งาน ก็แค่เปลี่ยนชื่อไฟล์ที่เหมาะกับเราเป็นชื่อ my.ini (back up อันเดิมไว้ด้วยก็ดีค่ะ เผื่อมีอะไรผิดพลาด)

xDebug

xDebug คือ PHP profiler และช่วยจัด format PHP ให้อ่านง่ายๆ ได้ด้วย

ในส่วนของ profiler จะมีการเก็บ log ของ server เกี่ยวกับเวลาในการรันของแต่ละ class แต่ละ method กันเลยทีเดียว แต่ไฟล์ที่ได้ออกมาจะเป็น text file โง่ๆ ที่อ่านยากมากๆ ต้องลงพวก GUI เป็น WinCacheGrind สำหรับ Windows และ KCacheGrind สำหรับ Linux

ก่อนอื่น ต้องลง xDebug ไว้ที่ server ก่อน

#yum install php-pear
#pecl install xdebug
#
vim /etc/php.ini   <– เพิ่มบรรทัด extension = xdebug.so ลงไป และบรรทัดต่อไปนี้

[xdebug]
xdebug.remote_enable=On
xdebug.remote_autostart=On
xdebug.remote_handler=dbgp
xdebug.remote_host=zero.haadthip.com
xdebug.remote_port=9000
xdebug.remote_mode=req
xdebug.profiler_enable=1
xdebug.profiler_output_dir=”/tmp/” <– log เก็บที่นี่
xdebug.profiler_append=On
xdebug.profiler_enable_trigger=On
xdebug.profiler_output_name=%s

ตรวจสอบให้แน่ใจว่ามี file xdebug.so อยู่ใน /usr/lib/php/modules

และ restart mysql + Apache ด้วยค่ะ ลอง echo phpinfo หากสำเร็จแล้วจะได้ผลดังนี้

จากนั้น ก็ลง WinCacheGrind ซึ่งเอา log file ได้จาก path ที่เสร็จไว้ใน php.ini (xdebug.profiler_output_dir) โหลดไฟล์เข้าไป ก็จะเอาไปแสดงในโปรแกรม ดังรูป

คราวนี้เราก็วัด performance ก่อน และ หลัง ได้แล้วค่ะ

APC

คือ Alternative PHP cache เอาไว้ cache code โดยจะ cache มันไว้ ณ compile state ทำให้ overhead ลดลง เอาเป็นว่าไม่เกี่ยวข้องกับ database นะคะ

ดูกระบวนการทำงานของ APC กันสักหน่อย

ขั้นตอนการ install

#yum install httpd-devel

(php-devel ลงเพราะมันต้องการ apxs หากเราไม่มี apxs ก่อนนจะลง APC มันจะ error ว่า

Sorry, I was not able to successfully run APXS.  Possible reasons:

1.  Perl is not installed;
2.  Apache was not compiled with DSO support (–enable-module=so);
3.  ‘apxs’ is not in your path.  Try to use –with-apxs=/path/to/apxs
The output of apxs follows
/tmp/tmpe1LwOh/APC-3.0.19/configure: line 3232: apxs: command not found
configure: error: Aborting
ERROR: `/tmp/tmpe1LwOh/APC-3.0.19/configure –with-apxs’ failed

)
#yum install apc
#pecl install apc
vim /etc/php.ini  เพิ่ม extension=apc.so และเพิ่มบรรทัดต่อไปนี้

[APC]
apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 64
apc.max_file_size = 10M
apc.stat=1

ทำการ restart Apache และลองดู phpinfo.php

และควรจะ download package ของ APC มาด้วย เพราะจะได้ดูทางหน้าเว็บได้ โดยแตกตัว folder เอาไปวางใน root directory นะคะ จะได้หน้าตาเว็บแบบนี้เอาไว้ดู resource เกี่ยวกับ APC ของ server

Memcache

หลังจากที่มีการ cache หน้า page กันแล้ว ตรงส่วน database ก็มีความสำคัญเช่นกัน เพราะ connection database เป็นอะไรอีกอย่างที่ทำให้เกิดความช้า หากเรา query บ่อย ๆ ประกอบกับมี query ที่ซับซ้อนและมีจำนวนข้อมูลที่เป็นผลลัพธ์มาก ใช้ memcache นี่แหละค่ะ เป็นตัวช่วย

วิธีการติดตั้ง

#wget http://monkey.org/~provos/libevent-1.3e.tar.gz
#tar zxpfv libevent*
#cd libevent*
#./configure
#make install

#wget http://www.danga.com/memcached/dist/memcached-1.2.4.tar.gz
#tar zxpfv memcached*
#cd memcached*
#./configure
#make install

#adduser memcached

หากเกิดปัญหาเกี่ยวกับการหา file libevent ไม่เจอ แก้ไขดังนี้

ค่อยมาเขียน

จากนั้น ต้องลง php-memcache ด้วย

#yum install php-pecl-memcache.i386
#vi /etc/php.ini เพิ่มบรรทัดดังต่อไปนี้

[memcache]
memcache.allow_failover=1
memcache.max_failover_attempts=20
memcache.chunk_size=8192
memcache.default_port=11211

ทำการ restart server แล้วตรวจสอบ phpinfo

การใช้งาน memcache ต้อง run ให้มันทำงานอยู่ด้วย เหมือนกับ run server นั่นแหละ โดยใช้คำสั่ง
memcached -vv -u memcached

ถ้าได้ผลแบบนี้แสดงว่า memcache server ได้ run ขึ้นมาแล้ว พร้อมใช้งานค่ะ

ลองทดสอบว่าสามารถ cache ได้แล้วจริงหรือไม่ โดย copy code ต่อไปนี้ รอไปรันดู

<?php$memcache

= new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect"); $version = $memcache->getVersion();
echo
"Server's version: ".$version."<br/>\n"; $tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123; $memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo
"Store data in the cache (data will expire in 10 seconds)<br/>\n"; $get_result = $memcache->get('key');
echo
"Data from the cache:<br/>\n"; var_dump($get_result); ?>

นั่นแสดงว่า วิธีการใช้ memcache คือ เราต้อง new memcache ให้กับ query ที่เราต้องการจะ cache หากคุณมี code อยู่แล้ว และต้องการจะ cache มีทางเดียวที่ทำได้ คือ แก้ code ค่ะ ^O^ งานเข้าล่ะสิ

และจริงๆ memcache เอง ก็สามารถดูประสิทธิภาพทางหน้าเว็บได้เหมือน APC เลย ลอง download code จาก ลิงค์นี้ค่ะ http://livebookmark.net/journal/2008/05/21/memcachephp-stats-like-apcphp/

เรื่องการ config แต่ละตัว อาจจะไม่ละเอียดมาก เรียกได้ว่าพอใช้งานได้แล้วกันค่ะ หากใครอยากจะ modify กันมากกว่านี้ ก็ตามไปอ่าน official website กันได้ค่ะ

หากมีข้อสงสัย ติชม เพิ่มเติม ก็ comment เอาไว้ได้ หากเอาไปอ้างอิง อย่าลืม credit ให้ด้วยนะคะ

ลิงค์น่าอ่านเพิ่มเติม

http://www.thaicyberpoint.com/ford/blog/id/170/
http://www.pantz.org/software/php/phptuningtips.html
http://dev.mysql.com/doc/refman/5.1/en/query-cache.html

http://www.pantz.org/software/php/phptuningtips.html

Posted in PHP | 11 Comments »

[twitter] facebook-twitter

Posted by natty on May 17, 2009

ด้วยความพยายามจะ sync facebook กับ twitter เข้าด้วยกัน เพื่อที่จะอัพเดท status ครั้งเดียวแล้วไปอัพเดททั้ง facebook และ twitter  ซึ่งตอนนี้ก็ทำได้แล้วโดยที่

  • อัพเดทจาก twitter แล้วจะไปอัพเดท status ใน facebook อัตโนมัติ
  • อัพเดทจาก twitter application ใน facebook แล้วไปอัพเดทใน twitter อัตโนมัติ
  • อัพเดทจาก facebook status แล้วอัพเดทอัตโนมัติใน twitter <– ยังไม่แน่ใจว่าได้หรือยัง

อ่ะ ก่อนอื่น มาทำให้อัพเดทจาก twitter ไป facebok ก่อนละกัน ง่ายๆ เลย แค่ใช้ application:http://apps.new.facebook.com/twitter/ ใส่ user/pass ของ twitter ของคุณ แค่นี้ก็เสร็จแล้ว หากคุณอัพเดท twitter จากหน้า application ใน twitter ใน facebook นี้ ก็จะไปอัพเดทใน twitter ของคุณ และจากคุณอัพเดทจาก twitter นอก facebook มันก็จะอัพเดทไปถึง facebook status เลย

และวิธีทำให้ อัพเดทจาก facebook status แล้วไปอัพเดท twitter อัตโนมัติ ทำได้ดังนี้ค่ะ

  1. เข้า twitter feed:http://twitterfeed.com แล้ว login เข้า twitterfeed (กรณีนี้เก๋ใช้ openid ในการ login ค่ะ)
  2. เลือก go to my twitterfeeds
  3. เลือก create new feed
  4. เอาลิงค์ไปใส่ที่ RSS feed url โดยไปที่หน้า facebook ของตัวเอง เข้าลิงค์ http://www.facebook.com/notifications.php แล้วไปที่ your notification ด้านล่างขวา ให้ copy ลิงค์นั้นมา
  5. ตั้งค่าตามต้องการ

หวังว่าคงเป็นประโยชน์กับทุกท่านนะจ้ะ

ส่วนหนึ่ง อ้างอิงจากที่นี่ค่ะ http://blog.kristibarrow.com/2008/11/19/how-to-update-twitter-with-your-facebook-status/

Posted in configuration | Leave a Comment »

[drupal] ปฐมบท installation

Posted by natty on May 14, 2009

ช่วงนี้มีโอกาสได้ทำงานเล็กๆ ที่ได้หน้าได้ตา ให้กับบริษัท เกี่ยวกับการทำ community ก็เลือกเครื่องมืออยู่ หลายตัวทีเดียว มีทั้ง wordpress drupal joomla วันนั้น test ทีเดียว 3 ตัว ก็ได้ข้อสรุปมาว่า drupal เหมาะกับ requirement นี้ที่สุด (สนับสนุน drupal แต่เขียน blog ใน wordpress ฮ่าๆๆๆ) ก็เลยว่าจะมาบันทึกเรื่อง drupal ไว้ก่อน ระหว่างที่จำได้อยู่แบบนี้

ไม่ได้ลงรูปเลยนะคะ ให้จินตนาการกันเอาเอง ฮ่าๆๆ

สามารถดาวน์โหลด drupal ได้จาก http://drupal.org/project/drupal ซึ่งเวอร์ชั่นที่โหลดมาตอนนี้ เป็นเวอร์ชั่น 6.12 ถ้าดูขนาด จะเห็นว่า แค่ 1 mb กว่าๆ เอา ตัวเล็กมากๆ ไม่น่าเชื่อ
หลังจากนั้น ก็ extract ไฟล์ ไปวางใน web directory (/var/www/html อะไรประมาณนี้) ทีนี้ ก็ลองเข้า http://hostname/drupal (hostname ของคุณเองนะ)

ก็จะมีหน้า configuration มาให้เลือกภาษาที่จะลง พอ next ไป มันก็จะไป check environment ของเรา ว่ามีนั่นมีนี่ครบป่าว

- Copy the ./sites/default/default.

settings.php file to ./sites/default/settings.php.
- Change file permissions so that it is writable by the web server. โดยใช้คำสั่ง chmod -R 777 <directory name>

ถ้าผ่าน ก็จะเข้าถึงหน้า database configuration ก่อนทำขั้นนี้ เราก็ไปสร้าง database ชื่อ drupal เอาไว้แล้ว
จากนั้นก็ใส่ config ของ database ที่เราสร้างขึ้น

ก็ลองเข้าใช้งาน http://localhost/drupal

แค่นี้ก็เสร็จเรียบร้อยขั้นตอนการลง drupal แล้ว ง่ายมากๆ เลย

แต่การใช้ drupal ให้เกิดประโยชน์ แค่ใช้ ตัว default ก็จะยังไม่มีประโยชน์เท่าไหร่ สิ่งดีเด่นของ drupal เลย คือ มี module เสริมให้ลงมากมาย เช่น การจัดการ view การทำ automated logout พวก gallery และ module ต่างๆ ที่เพิ่มการ customize ตัว drupal ดังนั้น ในตอนต่อๆไป จะเอา module และวิธีการทำให้เว็บไซต์มีความน่าสนใจ มาเขียนต่อค่ะ

Posted in drupal | 3 Comments »

[blognone] ซอฟท์แวร์ก๊อปปี้ ดีจริงหรือ

Posted by natty on November 21, 2008

เพิ่งไปเขียนบทความไว้ที่ blognone ฝากด้วยค่ะ

ขณะที่กำลังเขียน blog นี้ บทความนั้นได้รับ comment ไป 186 comments และ view ไป 3822 ครั้ง ค่ะ ด้วยอายุบทความ 1 วันกับ 4 ชั่วโมง

http://www.blognone.com/node/9664

Posted in Work experience | 4 Comments »

[PHP] สิ่งหนึ่งที่ทำให้เกิด Call to a member function on a non-object

Posted by natty on November 19, 2008

วันนี้ แก้ error โง่ๆ ไปพักหนึ่ง ด้วยความมึน ตอน run PHP แล้วเกิด คือ พยายามจะ new object แต่ว่าไม่สามารถจะ new ได้ และหลังจาก new ก็ไปใช้ method ของมัน ทำให้มันบอกว่า ใช้ function จากตัวที่ไม่เป็น object ไม่ได้ อย่างเช่น


$test = new AR(new Ds());

$test->getDS();

มันจะเรียก getDS(); ไม่ได้ เพราะ $test ยังเป็น null

มาเล่น PhotoHunt กัน ให้ดูว่า class ของ Ds ทำอะไร


var $dsStrategy;
public function _construct (iDSStrategy $dsStrategy){
$this->dsStrategy = $dsStrategy;
}
public  function getValForCreditFromCore(){
$this->dsStrategy->getValForCreditFromCore();
} 

นี่แหละ ทำให้เกิด Call to a member function on a non-object งงเลยใช่ไหมว่าทำไม

เหตุผลสำหรับ code นี้ เพราะที่ใส่ underscore หน้า constructor ไปอันเดียว – -”

ที่ถูกต้องเป็น

public function __construct (iDSStrategy $dsStrategy)

Posted in PHP | Leave a Comment »

Protected: เมื่อเริ่มต้นชีวิตอาจารย์

Posted by natty on October 26, 2008

This post is password protected. To view it please enter your password below:


Posted in Work experience | Enter your password to view comments

[PHP] Simple Test

Posted by natty on October 22, 2008

เนื่องจากมีคนเรียกร้องอยากใช้งาน SimpleTest ใน PHP ขึ้นมาอย่างกระทันหัน ไม่ได้ตั้งตัว ก็เลยเอามาวางให้ที่นี่นะคะ

ก่อนอื่น ไป ดาวน์โหลดมาก่อนนะคะ

เมื่อได้มาแล้ว ก็แตกไฟล์ แล้วเอา folder ชื่อ simpletest ก็เอาไปวางใน apache ของตัวเอง สำหรับเก๋ เก๋จะวางไว้นอกสุด ก็คือใน htdocs เลย ใครจะมาเรียกใช้เมื่อไหร่ก็ได้

เก๋จะให้ตัวอย่างสำหรับการทำ SimpleTest ของ blog ตอนหนึ่ง คือ [PHP] Class สำหรับ convert DBF to MySQL

โดยเก๋จะสร้าง folder ชื่อ unittest และมีไฟล์ชื่อ TestDBFMySQL.php อยู่ การทำหน้า test เราต้องใส่ require_once(‘../../simpletest/autorun.php’); เพื่อ include ไปยัง lib ของ simpletest และก็ include ไฟล์ที่เราจะ test ด้วย ปกติจะ new เป็น test case และ test suite ใน class อื่นได้ แต่อันนี้ไม่ได้ new ไว้ไฟล์อื่น แบบว่าเรียกไฟล์นี้โดยตรงเลย

การ test ก็จะ assert ในหลายกรณี อันนี้ก็แล้วแต่เราว่าจะทำยังงัย อ่านได้ที่นี่

โดยหน้าตาของไฟล์ตัวอย่าง เป็นดังนี้


<?php
//DBFMySQL/unittest/TestDBFMySQL.php

require_once('../../simpletest/autorun.php');
require_once('../DBFMySQL.php');

class TestDBFMySQL extends UnitTestCase {
        //public $fromDBF='D:\PO_McDonald\INVORHD0.DBF';
        public static $fromDBF='D:\PO_McDonald\INVORHD0.DBF';
        public static $toMySQL='INVORHD0';
        public static $myDB='db_mcdonald';
        public static $myUser='root';
        public static $myPass='root';
        public static $myHost='localhost';

    function setUp() {

    }

    function tearDown() {
        //@unlink('/temp/test.log');
    }

    function testConvertDBFToMySQL(){
        $x = DBFMySQL::convertDBFToMySQL(self::$fromDBF, self::$toMySQL, self::$myDB, self::$myUser, self::$myPass, self::$myHost);
        $this->assertTrue($x);
    }
    function testReadDBFToArray(){
        $x = DBFMySQL::readDBFToArray(self::$fromDBF);
        $this->assertNotNull($x);
    }
    function testPersistToMySQL(){
        $arrTable = DBFMySQL::readDBFToArray(self::$fromDBF);
        $x = DBFMySQL::persistToMySQL($arrTable, self::$toMySQL, self::$myDB, self::$myUser, self::$myPass, self::$myHost);
        $this->assertTrue($x);
    }
}
?>

ลองเข้าไปที่ http://localhost/DBFMySQL/unittest/TestDBFMySQL.php นะคะ (เครื่องเก๋นะคะ ยกตัวอย่างให้ดู)

หากว่า Test ผ่าน จะได้ผลดังนี้ค่ะ

Image hosting by UpPicth.com

ถ้า fail

Image hosting by UpPicth.com

ถ้า connect db ไม่ผ่าน
Image hosting by UpPicth.com

สงสัยก็ comment ถามมาได้ค่ะ จะตอบให้

ดาวน์โหลดตัวอย่าง code ได้ที่นี่ค่ะ ก่อนอื่นควรไปอ่าน [PHP] Class สำหรับ convert DBF to MySQL ด้วยนะคะ เผื่อรันไม่ผ่าน

Posted in PHP | 1 Comment »

[Jasper] run Jasper Report from Java class

Posted by natty on October 3, 2008

จริงๆ ก่อนหน้าจะเขียนเืรื่องนี้ ควรเขียนการ run Jasper Report from JSP ก่อนนะเนี่ย แต่ไม่เป็นไร เอาอันนี้ก่อน

ได้วิธีการมาจาก ที่นี่ แต่ต่างกันที่ code การติดต่อ database นิดหน่อย และเราจะทำมันใน eclipse ไม่ได้รันแบบที่เค้าทำ

ก่อนอื่นสร้าง eclipse project เป็น java project แล้วเอา lib ต่อไปนี้ไปใส่ไว้ใน build path ของ eclipse

  • common-collection
  • common-logging
  • iReport
  • commons-javaflow
  • jasperreports
  • common-digester
  • common-beanutils
  • mysql-java-connector

และสร้าง ReportDriver.java ขึ้นมา ดังนี้

import java.sql.*;

import net.sf.jasperreports.view.JasperViewer;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.JasperReport;

import java.io.OutputStream;

public class ReportDriver {
    /**
     * Constructor for ReportDriver
     */
    public ReportDriver() {       
    }
    public static Connection connectDB(String databaseName, String userName, String password) {
        Connection jdbcConnection = null;
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String connectionURL = "jdbc:mysql://127.0.0.1:3306/test?user=root;password=root";
            jdbcConnection = DriverManager.getConnection(connectionURL,userName,password);
        }catch(Exception ex) {
            String connectMsg = "Could not connect to the database: " + ex.getMessage() + " " + ex.getLocalizedMessage();
            System.out.println(connectMsg);
        }
        return jdbcConnection;
    }

    /**
     * Takes 4 parameters: databaseName, userName, password, reportFileLocation
     * and connects to the database and prepares and views the report.
     * @param databaseName holds database name,
     * @param userName holds user name
     * @param password holds password to connect the database,
     * @param reportFile holds the location of the Jasper Report file (.jrxml)
     */
    public static void runReport(String databaseName, String userName, String password,String reportFile) {
        try{
            JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            Connection jdbcConnection = connectDB(databaseName, userName, password);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, jdbcConnection);
            JasperViewer.viewReport(jasperPrint);
        }catch(Exception ex) {
            String connectMsg = "Could not create the report " + ex.getMessage() + " " + ex.getLocalizedMessage();
            System.out.println(connectMsg);
        }
    }
    public static void main(String[] args) {
        //if (args.length == 4) {
            String databaseName = "test" ;
            String userName = "root";
            String password = "root";
            String reportFile = "Reports/test2.jrxml";
            runReport(databaseName, userName, password, reportFile);
        /*}/lse{
            System.out.println("Usage:");
            System.out.println("java ReportDriver databaseName userName password reportFileLocation");
        }
        return;*/

    }
}

ใน code จะไปเรียกไฟล์ test2.jrxml ที่สร้างโดย iReport ดังนั้นเราต้องไปสร้างไฟล์นี้ก่อน ให้ต่อไปยัง database ตัวเดียวกับที่เราใช้ใน code นี้ เมื่อรัน main method ขึ้นมา ก็จะได้ report ที่ถูก view โดย JRViewer

Posted in jasper, java | 2 Comments »

[Resin] JasperReport on resin

Posted by natty on September 25, 2008

หากเราไม่มี jasperserver บน resin แล้วเราต้องการให้มันสามารถใช้งานกับ .jrxml และ compile ได้ เราจำเป็นจะต้องมี lib ดังต่อไปนี้ หากต้องการใช้ในทุก project ก็เอาไปวางไว้ที่ resin/lib

common-collection
common-logging
poi
iReport
commons-javaflow
itext
jasperreports
common-digester
common-beanutils

java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
เกิดจากการไม่มี common-logging

javax.servlet.ServletException: net.sf.jasperreports.engine.JRException:java.lang.reflect.InvocationTargetException
สาเหตุหนึ่งอาจ เกิดจากการไม่มี common-collection

Posted in configuration, resin | 2 Comments »

[Resin] PHP in Java: Natty & Juacompe discussion

Posted by natty on September 24, 2008

เนื่องมาจาก natty ที่พอจะมีความรู้เรื่อง resin เท่าหางอึ่ง + PHP อยู่บ้าง บวกกับ juacompe ที่อัดแน่นไปด้วยความรู้เกี่ยวกับ server ที่ใช้ run java ทำให้วันนี้ก็เกิดความเข้าใจมากขึ้นกับ PHP in Java ใน resin

หากเราต้องการทำให้ java ของเราถูกเรียกโดย PHP ได้ ต้องทำให้ class นั้น extends AbstractQuercusModule และมี method ที่รับ parameter env ซึ่งเป็น quercus environment resources หรือไม่ก็ต้องสร้าง class ไว้แล้วให้มาสร้าง instant อยู่ใน class นี้

code ด้านล่างเป็นเป็น code ที่สร้าง method ขึ้นมาเพื่อให้ถูกเรียกจาก PHP ได้ ชื่อ method ว่า hello_test โดยจะเห็นว่ามันจะรับตัวแปร env ด้วย โดย class นี้อยู่ใน package example

 package example;

import com.caucho.quercus.env.Env;
import com.caucho.quercus.module.AbstractQuercusModule;

public class HelloModule extends AbstractQuercusModule {
   /*
   ** Notice the careful use of the naming
   ** convention hello_test.  This is done
   ** in order to prevent name collisions
   ** among different libraries.
   **
   ** @param env provides access to Quercus environment resources
   ** @param name
   */
   public String hello_test(Env env, String name)
   {
     env.println("inside HelloModule  hello_test()");
     return "Hello, " + name;
   }
} 

สิ่งที่สำคัญคือ ต้องสร้างไฟล์ชื่อ com.caucho.quercus.QuercusModule ไว้ที่ classes/META-INF/services โดยใส่ชื่อ package.class เอาไว้

 example.HelloModule 

ตอนหน้าค่อยต่ออีก….นี่เป็นเพียง study ยังไม่ได้ทดสอบเลยว่าทำต่างจากนี้แล้วจะพังป่าว

Posted in PHP, configuration, java | Leave a Comment »