Torquebox + CDI demo
trqbox-demo-backend - contains the "business logic" - the CDI beans and other java classes. The archive must
beans.xml file. Here there's only one bean,
trqbox-demo-frontend - contains the frontend in Ruby On Rails. In the
rails directory there's an application
generated using Rails, with an additional controller (
welcome_controller), which lookups a CDI bean from the backend
and displays the result of a method.
The lookup is done in the following way:
result = inject(MyJavaBean).some_method(param1, param2)
Where the inject method is simply:
include_class "pl.softwaremill.common.util.dependency.D" def inject(what) D.inject(what.java_class) end
trqbox-demo-ror-cdi-int - the integration layer. The packaging is defined as
war, but the war is never deployed.
It is created only in order to have all the jars with their dependencies in one directory. These jars are then copied
trqbox-demo-frontend/rails/trqbox-demo-frontend/lib (think of this as
WEB-INF/lib), and become part of the
deployed Rails application (
.knob). The knob also contains a
web.xml file, in the
config directory, specifying
the listener, which setups
Deploying the example
- Clone this repository
- Download latest Torquebox
- Unzip it next to the repository
- Create a
conf_envfile basing on
conf_env_template. There you need to fill in the
TORQUEBOX_HOMEproperty, which should be the directory that got created by unpacking Torquebox.
- Install Bundler: first
source conf_env, then
jruby -S gem install bundler. This is needed because Torquebox comes with an "empty" JRuby installation.
- Install dependencies: go to
jruby -S bundle install. This should install Rails.
- Build the project:
mvn clean install
- Now start up the AS: go to
- Deploy the backend and frontend: run the
deploy.shscript (it copies the jars into the lib directory of the Rails app, and invokes a Torquebox Rake task which copies the
.knobinto JBoss's deploy directory).
- Test that it works: go to
http://localhost:8080/welcome/index. You should see welcome messages, coming both from Rails and from the CDI bean.
Creating this application from scratch
Setting up the project
- Download & unzip latest (Torquebox)[http://torquebox.org/torquebox-dev.zip]
- Create a maven project with three modules (backend, frontend, integration). The backend needs JEE6 APIs, the integration module should depend on the backend (so that the jar is included) an
- Create a
conf_envand source it (a convenience script - so that the proper path to Torquebox and JRuby is set)
Creating an empty rails project
- Install rails:
jruby -S gem install rails
- Create a
trqbox-demo-frontend/railsdirectory, change to it
- Create an empty Rails project:
jruby -S rails new trqbox-demo-frontend -m $TORQUEBOX_HOME/share/rails/template.rbThe project will have the name
trqbox-demo-frontendand uses the Torquebox template, which adds some additional gems for jobs, scheduling, etc. (JBoss AS <-> JRuby/Rails integration).
- Install any dependencies of the new project:
cd trqbox-demo-frontend; jruby -S bundle install
- Deploy the application: basically run
jruby -S rake torquebox:deployin the
trqbox-demo-frontend/rails/trqbox-demo-frontenddirectory. This will generate a
jboss/server/default/deploy. Look inside it - nothing complicated there :). To redeploy the application, you just need to touch that file.
- Open up
http://localhost:8080- you should see the welcome screen.
- Generate a controller (using the Rails generator), try adding some methods. No need to redeploy, the changes are visible instantly! :)
Adding CDI integration
- Create a
ServletContextListenerin the integration module which setups
- Add a
web.xmlwith that listener to
web.xmlwill become a part of the deployed knob.
- Create a CDI bean in the
mvn clean install. This will cause the integration module to produce an unpacked war with all the dependencies.
- Now we need to copy those dependencies to
trqbox-demo-frontend/rails/trqbox-demo-frontend/lib- create a script.
- Finally we can lookup the dependency in Rails using
- Redeploy and test