JRuby/Rails with JPA on Glassfish V3

JRuby/Rails by default uses ActiveRecords package for the purposes of Object Relational Mapping (ORM). JPA is a JavaEE ORM technology and the advantage of JPA is its pluggability. Modeling class and database migration in Rails are what Entity class and persistence provider do in JavaEE.

The TP2 of GlassFish V3 was released just before 2008 JavaONE and the default persistence provider of V3 is Eclipse Link. It'll be interesting to see how easy it is to use JPA in a classic blog rails application. Vivek wrote in his blog that he used JPA and Rails on GlassFish V2 with TopLink as persistence provider. Here I would modify the blog demo application in his blog to fit for GlassFish V3 and Eclipse Link.

Install JRuby and Rails

  1. Download the JRuby 1.0.3 zip file . I am setting up everything on Solaris 10 Sparc platform.
  2. Unzip the file and set the JRUBY_HOME to [Install_Dir]/jruby-1.0.3
  3. Include [JRUBY_HOME]/bin in PATH for convenience.
  4. Install Rails 1.2.6:
    gem install rails -v=1.2.6 --no-rdoc --no-ri -–include-dependencies
    
    The latest Rails release 2.0.2 has a lot of differences with Rails 1.2.6. In this blog, JRuby1.0.3/Rails1.2.6 will be used and I'll write a follow up blog for the combination of JRuby 1.1.1/Rails 2.0.2.
  5. Install warbler:
    gem install warbler
    Warbler is needed for creating war file from JRuby application.

Setup GlassFish V3 and MySQL

  1. Download TP2 of GlassFish V3 and unzip.
  2. Add JRUBY_HOME value to the GlassFish V3 config file. For example, in my case add
    JRUBY_HOME=/opt/jruby-1.0.3
    to the bottom of [Install_Dir]/glassfishv3-tp2/glassfish/config/asenv.conf
  3. Setup the MySQL community server by following the MySQL menu.
  4. Create a new DataBase instance named "test" in MySQL.
  5. Copy the MySQL connector j driver to [Install_Dir]/glassfishv3-tp2/glassfish/lib.
  6. Set JAVA_HOME and include [JAVA_HOME]/bin to path. JDK 1.6.0_06 was used in this blog.
  7. Start the GlassFish V3:
    [Install_Dir]/glassfishv3-tp2/glassfish/asadmin start-domain.

Create Java Entity Beans

Create Java Entity bean by following the steps described in Vivek’s blog. A short cut is to download the Entity code and import the code to Netbeans 6.1. The persistence provider in persistence.xml needs to be changed to Eclipse Link as shown below:


If the image appears small in your browser, please right click the image and choose "View Image" to see details. Build the project with “Shift + F11” and a new Post.jar will be created under Post/dist dir.

Create the Rails Application

  1. Download the blog demo Rails application. Unzip the file and change directory to [Install_Dir]/jpa_blog/WEB_INF. Create a directory called classes.
  2. Add the Java Entity beans by unjar the Post.jar.
  3. Use warbler to package the Rails app into a war file: cd [Install_Dir]/jpa_blog; jruby –S warble
The jpa_blog.war will be create under [Install_Dir]/jpa_blog.

Deploy the Blog Demo Rails Application

Deploy the repackaged Rails application onto GlassFish V3:

[Install_Dir]/glassfishv3-tp2/glassfish/asadmin deploy jpa_blog.war
In the GlassFish V3 server.log, the following message should display if the deployment is successful:
[#|2008-05-22T18:42:08.977-0700|INFO|GlassFish10.0|javax.enterprise.system.container.web|_ThreadID=12;_ThreadName=Thread-5;|PWC1412: WebModule[/jpa_blog] ServletContext.log():Info: using runtime pool timeout of 30 seconds|#]
[#|2008-05-22T18:42:08.979-0700|INFO|GlassFish10.0|javax.enterprise.system.container.web|_ThreadID=12;_ThreadName=Thread-5;|PWC1412: WebModule[/jpa_blog] ServletContext.log():Warning: no initial runtimes specified.|#]
[#|2008-05-22T18:42:08.981-0700|INFO|GlassFish10.0|javax.enterprise.system.container.web|_ThreadID=12;_ThreadName=Thread-5;|PWC1412: WebModule[/jpa_blog] ServletContext.log():Warning: no max runtimes specified.|#]
[#|2008-05-22T18:42:09.009-0700|INFO|GlassFish10.0|javax.enterprise.system.container.web|_ThreadID=12;_ThreadName=Thread-5;|Loading application jpa_blog at /jpa_blog|#]
[#|2008-05-22T18:42:09.034-0700|INFO|GlassFish10.0|javax.enterprise.system.tools.deployment|_ThreadID=12;_ThreadName=Thread-5;|Deployment of jpa_blog done is 15526 ms|#]

Then, access the blog application at: http://[HOST_NAME]:8080/jpa_blog/blog and the application should display:

Click “New post” link to create new blog entry:

Click “Create” button to submit the blog and the application will show the new blog created and a list of blogs created before:

Comments:

[Trackback] Innovation is a hard thing to accomplish without risk. Sun is trying to grow their customer base with JavaFX, but may have introduced too much risk in not tethering to the needs of existing customers.

Posted by Jacob Hookom's Blog on August 07, 2008 at 04:53 AM PDT #

[Trackback] Innovation is a hard thing to accomplish without risk. Sun is trying to grow their customer base with JavaFX, but may have introduced too much risk in not tethering to the needs of existing customers.

Posted by Jacob Hookom's Blog on August 08, 2008 at 01:50 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Ming Zhang

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today