GlassFish v3 作為模組化重寫版本,核心目標在於支援非 Java EE 容器與模組運行。本文示範如何在 GlassFish v3 上,零 Warbler/Gem、零 Goldspike 插件,直接部署 JRuby on Rails 應用並與 Java Servlet 共存互呼。
# 使用 JRuby 1.1 建立 Rails 專案
~/testbed/jruby-1.1/samples/rails > ../../bin/jruby -S rails railsee3
WEB-INF/web.xml 定義 Servlet(範例:HelloServlet 對應 /hello 路徑)WEB-INF/sun-web.xml 設定 Context Root 為 /servlet、啟用 Class Loader 委派模式WEB-INF/lib 目錄,放入編譯好的 Servlet JAR(如 HelloServlet.jar)# 編輯 config/asenv.conf 設定 JRUBY_HOME
JRUBY_HOME="/Users/arungup/..."
# 部署 Rails 應用
~/testbed/glassfish/v3/p2b9/glassfish/bin/asadmin deploy --force=true railsee3
# 訪問 Servlet
https://localhost:8080/servlet/hello

帶參數呼叫:

jruby script/generate controller home indexapp/controllers/home_controller.rb,使用 JRuby Java 整合 API 呼叫 Servlet:class HomeController < ApplicationController
include Java
def index
url = Java::Net::URL.new("https://localhost:8080/servlet/hello")
conn = url.open_connection
reader = java.io.BufferedReader.new(java.io.InputStreamReader.new(conn.get_input_stream))
@servlet_output = ""
while (line = reader.read_line) != nil
@servlet_output << line
end
reader.close
end
end
app/views/home/index.html.erb 顯示結果:<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>
<%= @servlet_output %>
https://localhost:8080/railsee3/home/index
GlassFish v3 的模組化架構打破語言邊界,讓 Ruby on Rails 與 Java EE 元件在同一容器原生共存、互相呼叫,無需額外打包工具。這為漸進式遷移、多語言微服務架構提供了強有力的平台支撐。