ผมอยากเรียน Grails & Groovy นะครับ มีใครพอจะเปิดคอร์สสอนในวันเสาร์และวันอาทิตย์ได้บ้างครับ (มีค่าเรียนอย่าแพงมากนะครับ ;p )
เผลอนิดเดียวก็ปล่อย Grails 2.0M1 ออกมาแล้ว
ไม่ได้เข้ามา update ข่าวสารของ Grails นานพอควร โดยครั้งนี้ทางทีมพัฒนาปล่อย version 2.o M1 ออกมาให้ใช้งานกัน ซึ่งมี feature ใหม่ๆ ที่น่าสนใจ และ ที่เข้าใจได้ง่ายๆ ดังนี้
1. Grails console ที่ใช้ง่ายขึ้น และมี output ที่เป็นมิตรและดูง่ายมากขึ้น ตัวอย่างดังรูป

และใน Grails console นั้นสามารถใช้ปุ่ม TAB สำหรับ Auto-completion อีกด้วย
2. มี Reload Agent ซึ่งช่วยลดปัญหาสำหรับการ auto-load classes, jar file ต่างๆ เมื่อมีการเปลี่ยนแปลง ซึ่งน่าจะช่วยคนพัฒนาได้มากเช่นกัน จะได้ไม่ต้องมานั่ง clean กันเอง และหวังว่าจะไม่กินหน่วยความจำมากไปจนเต็มนะ !!
3. มี Template ของ Document ใหม่ หน้าตาไฉไล ดังรูป

4. Test Report ใหม่ ดูไฮโซขึ้นเยอะ ดังรูป

5. ปรับปรุง error report ต่างๆ ให้ง่ายและเข้าใจได้ง่ายขึ้น ทำให้หาจุดที่ผิดพลาดได้ง่ายขึ้นอีก happy กันไปอีกขั้น
ุ6. ใน version นี้มี default database คือ H2 ซึ่งก็แน่นอนว่ามี Console งามๆ มาให้ใช้งานกันง่ายๆ
7. Feature หลักๆ ใน version นี้ ประกอบไปด้วย ฺBinary plugin, Groovy 1.8 และ Spring 3.1 profile support
8. action ใน Controller สามารถใช้ method ได้แล้ว ซึ่งแต่ก่อนจะสร้างด้วย closure เท่านั้น เช่น
def index() {
}
9. สนับสนุน Servlet Asynchronous ของ Servlet 3.0 แล้ว
10. Scaffolding นั้นสร้างด้วยมาตรฐาน HTML5
11. มี jQuery มาให้ด้วยเลย
Reference Website
Multi-Tenant-Core
http://multi-tenant.github.com/grails-multi-tenant-core/guide/index.html
ลักษณะงานคือ ต้องการสร้าง server ที่ใช้ได้จากหลายบริษัท
คิดว่าน่าจะทำเป็น Database per บริษัท
โดยใช้ชื่อ URL ต่างกันก้อด้าย เช่น
“company1.blue-eye.com”
“company2.blue-eye.com”
หรือจะทำแบบ check login user ก้อด้าย
หรือทำทั้ง 2 แบบ
======================
หัวข้อที่ 2. Multi-Tenant Modes
หนูเลือกเป็น “singleTenant”
แล้วมันบอกว่า มีวิธี Resolving the DataSource 2 วิธี
1) Mapping DataSources through Config.groovy
2) Mapping DataSources through the database
หนูไม่เข้าใจว่ามันต่างกันยังไง แล้วแบบไหนเหมาะกว่า?
ถ้าใช้วิธีที่ 1
t1 = “jdbc:mysql://localhost/ets_dev1″
t2 = “jdbc:mysql://localhost/ets_dev2″
t3 = “jdbc:mysql://localhost/ets_dev3″
แล้ว URL ที่ใช้เข้าเว็็บจาก “http://localhost:8080/nuch” จะเปลี่ยนเป็นอย่างอื่นรึป่าวคะ?
พอเข้าไปแล้วมันฟ้อง
org.codehaus.groovy.grails.web.mapping.exceptions.UrlMappingException: Error mapping onto view [/index]: Could not decode mapped tenant id from request server name localhost
หรือเพราะโค้ดหนูยังไม่สมบูรณ์?
แล้ว เราต้อง CREATE DATABASE ets_dev1 2 3 เองใช่มั้ยคะ?
แล้ว
//JNDI Example
t1 = “java:comp/env/myDataSource”
ไม่มีได้มั้ยคะ?
==================
หัวข้อที่ 3. Configure a Tenant Resolver
3.1 DNS Resolver Set Up
มี 2 แบบ คือ
1) Mapping subdomains using Config.groovy
2) Mapping subdomains using the database
มันต่างกันยังไง แล้วมีผลต่อ Database กับ URL อย่างไร?
3.2 Acegi Resolver Set Up
เราควรจะลง multi-tenant-acegi ใช่มั้ยคะ?
พอลงแล้ว เราต้องสร้าง class User เองเหรอคะ? เห็นลงแล้วไม่มีให้
แล้ว “userTenantId” จะเป็น 1 or 2 or 3 … ใครเป็นคนกำหนด หรือว่ามันอัตโนมัติ?
แล้วมีโอกาสที่เค้าจะโกหกเพื่อเอาข้อมูลบริษัทอื่นมั้ยคะ?
รู้สึกว่าหนูจะไม่รู้เยอะมาก ต้องขอรบกวนพี่ๆหน่อยนะคะ ><
ขอบคุณค่ะ
Grails 1.4.0 M1 ออกมาแล้ว
โดยใน version นี้มีหลายๆ อย่างที่เปลี่ยนแปลง และ ปรับปรุงให้ดีขึ้นมาก ส่วนที่น่าสนใจมีดังนี้
1. มาพร้อม Libraries ต่างๆ ดังนี้
- Spring Framework 3.1 M1
- Groovy 1.8
- Hibernate 3.6
- Servlet 3.0
- Apache Tomcat 7.0
2. ฺBinary plugins
3. Reload agent
4. Plugin usage tracking
5. Improve scaffolding UI ให้สนับสนุน HTML5
6. มี Unit Testing ที่ดีขึ้น
7. Abstract Inheritance Support in GORM
8. GORM API now available to Java
9. Include H2 Database Engine เข้ามาเป็น default database แทนที่ HSQL
โดยในแต่ละ features นั้นจะนำมาอธิบายๆ อย่างละเอียดในบทความต่อๆ ไปครับ
Reference Website
[Slide] Groovy 1.8 update จากงาน GR8Conf Europe 2011
ไปเจอ slide ของ Guillaume Laforge ที่ไป present ในงาน GR8Conf Europe 2011 เลยนำมาบอกต่อครับ น่าสนใจมากๆ เกี่ยวกับ Groovy ครับ
transaction ใน service
ขอคำชี้แนะหน่อยค่ะ เรื่อง transaction ใน service ค่ะ
AnyDomain.withTransaction { … }
มันมีความสามารถในการ rollback แค่ไหนคะ
ทำข้ามหลายๆ service methods กับหลายๆ domain ได้รึป่าว?
อย่างเช่น เรามี 3 methods คือ
newBill( )
newTxaction( )
deleteBill( )
ถ้าเราสั่ง deleteBill( ) แล้วต้องการให้การ save( ) ทั้งหมดใน
newBill( ), newTxaction( ) ไม่ถูก save ในฐานข้อมูล จะทำได้มั้ยคะ?
ขอบคุณค่ะ
how to make runable jar file from groovy or grail project krub?
กำลังหัดใช้ groovy และ grails ครับ
แต่ตอนนี้มีปัญหาว่า พอสร้าง Groovy Project ชื่อว่า hello_world เสร็จแล้ว ผมจะไปสร้าง jar file เพื่อมาทดลอง รัน ได้อย่างไรครับ
ช่วยแนะนำด้วยครับ หรืออธิบายให้ด้วยครับ
ขอบคุณมากครับ
siamkom
ปล. ตอนนี้ใช้ eclipse, netbeans และ STS อยู่ครับ
Error: installing mail plugin
ปัญหา :
ใช้ SpringSource Tool Suite (Version: 2.5.2.RELEASE)
ไม่สามารถใช้คำสั่ง grails > install-plugin mail ได้
Error:
Resolving plugin JAR dependencies …
:: problems summary ::
:::: WARNINGS
::::::::::::::::::::::::::::::::::::::::::::::
:: UNRESOLVED DEPENDENCIES ::
::::::::::::::::::::::::::::::::::::::::::::::
:: org.springframework#org.springframework.test;3.0.5.RELEASE:
configuration not found in
org.springframework#org.springframework.test;3.0.5.RELEASE: ‘master’.
It was required from org.grails.internal#surfsesh;0.29 test
::::::::::::::::::::::::::::::::::::::::::::::
Failed to install plugin [mail-1.0-SNAPSHOT]. Plugin has missing JAR
dependencies.
[delete] Deleting directory
/var/lib/hudson/.grails/1.3.6/projects/workspace/plugins/mail-1.0-SNAPSHOT
Solution:
1. เข้าไปที่ file BuildConfig.groovy และ เอา comment ออกที่บรรทัด mavenCentral()
2. ลอง install-plugin mail ใหม่ อีกครั้ง
ผล…
“Download complete.
Plugin mail-1.0-SNAPSHOT installed”
Reference:
http://grails.1312388.n4.nabble.com/spring-security-ui-depending-on-mail-plugin-gt-dependency-resolution-fails-td3238433.html
Grails 1.3.7 ออกมาแล้ว
โดยสิ่งที่ปรับปรุงใน version นี้ประกอบไปด้วย
- Groovy 1.7.8
- สามารถกำหนด parameters ที่จะไม่เก็บใน log ได้ โดยใช้
grails.exceptionresolver.params.exclude = ['password', 'creditCard']
หรือถ้าไม่ต้องการใช้ development mode ก็สามารถปิดการเก็บ log ได้โดยใช้
rails.exceptionresolver.logRequestParameters = false
ส่วน mode อื่นๆ เช่น testing, production จะปิดอยู่แล้ว
Changelog: See JIRA
Download: http://grails.org/Download
Documentation: http://grails.org/doc/1.3.x
การใช้งาน Redis ใน grails application
Redis คืออะไร
- OpenSource
- Memory storage
- NoSQL => Key-value
โดยใน Grails นั้นมี Redis plugin ให้ใช้งาน โดยใช้ Redis library ชื่อว่า JRedis ซึ่งจะเป็นการเปลี่ยน storage ใน GORM จาก Database มาเป็น Memory ซึ่งมี features เหมือนกับ GORM for Hibernate ดังนี้
- Simple persistence methods
- Dynamic finders
- Criteria queries
- Named queries
- Inheritance
ส่วน features ที่ยังไม่สนับสนุนคือ
- HQL queries
- Dirty checking methods
- Composite primary keys
- Many-to-many associations (these can be modelled with a mapping class)
- Any direct interaction with the Hibernate API
- Sorting on strings
- Custom Hibernate user types
ดังนั้นเราลองมาสร้าง project ที่ใช้ Redis กันดีกว่า ซึ่งมีขั้นตอนดังนี้
1. ทำการติดตั้ง Redis Server ที่เครื่องก่อน ซึ่งแบ่งการติดตั้งตาม OS ดังนี้
1.1 สำหรับ Windows
- ทำการ download จากที่นี่
- ทำการ extract zip file ให้ทำการ click file redis-server.exe เพื่อ start redis server ซึ่งจะแสดงผลดังรูป

1.2 สำหรับ Linux ให้ทำดังนี้
- ทำการ download จากที่นี่ http://redis.io/download ซึ่งแนะนำให้ใช้ version 2.0.4
- ทำการ extract file ที่ download และ start redis server มาด้วยคำสั่ง
>tar xzf redis-2.0.4.tar.gz
>cd redis-2.0.4
>make
>./redis-server
2. สร้าง project ชื่อว่า test_redis
>grails create-app test_redis
3. ทำการติดตั้ง Redis plugin และ config ดังนี้
>grails install-plugin redis
ทำการ config ใน file /grails-app/conf/Config.groovy ดังนี้
grails.redis.host="localhost" grails.redis.port=6379 // Port ตามรูปด้านบน //grails.redis.password="secret" //ตัวอย่างนี้ไม่ต้องการใช้ password grails.redis.pooled=true grails.redis.resources=15 grails.redis.timeout=5000
4. สามารถใช้งาน Redis อย่างเดียว หรือ ใช้งานร่วมกับ Hibernate ได้ดังนี้
4.1 ใช้งาน Redis อย่างเดียว ให้ทำดังนี้
- ทำการ uninstall hibernate plugin ด้วยคำสั่ง
>grails uninstall-plugin hibernate
หลังจากที่ทำการ uninstall แล้ว Domain class จะใช้งาน Redis โดย default ทันที
4.2 ใช้งาน Redis ร่วมกับ Hibernate
- หลังจากที่ทำการติดตั้ง Redis plugin แล้ว Domain class จะใช้งาน Hibernate โดย default แต่ถ้าต้องการใช้ Redis จะต้องเพิ่ม property ชื่อว่า mapWith ใน Domain class ดังนี้
static mapWith = "redis"
ตัวอย่างเช่น
package com.grails66
class User {
static mapWith = "redis"
String username
String password
static constraints = {
username()
password()
}
}
ตัวอย่างการใช้งานผ่าน Controller
package com.grails66
class UserController {
def scaffold = true
}
ทดลองใช้งานผ่าน url http://localhost:8080/test_redis/user/list จะทำงานตามปกติไม่เปลี่ยนแปลง ดังรูป

แต่ถ้าต้องการตรวจสอบว่าทำงานกับ Redis หรือไม่ให้ลองทำการปิด redis server ไปแล้วดูผลการทำงาน
นี่ก็เป็นตัวอย่างง่ายๆ สำหรับการใช้งาน Redis ใน Grails application
Reference Website
Redis Server
JRedis :: Redis client library
Grails :: Redis plugin
Announcing GORM for Redis
Redis server for Windows
