In this tutorial, we will be exploring how to run Ruby applications in a Java Enterprise Container – WIldFly application server. We will be using jRuby to run the Ruby programming language in the JVM.
Introduction to jRuby
jRuby, a version of the Ruby programming language that is implemented in Java. jRuby allows you to run Ruby code on the Java Virtual Machine (JVM), giving you access to the vast array of libraries and tools available in the Java ecosystem.
Whether you are a seasoned Ruby developer looking to expand your skillset or a Java developer interested in trying out a new language, jRuby is a powerful and versatile tool that is worth exploring. In this tutorial, we will cover the basics of jRuby syntax and programming concepts, as well as more advanced topics like interfacing with Java libraries and utilizing the JVM’s performance features.
So let’s get started on this journey of learning jRuby!
- First, make sure you have WildFly installed on your machine. You can download it from the WildFly website (https://wildfly.org/downloads/) or install it using your package manager if you are using a Linux distribution.
- Next, you will need to install JRuby on your machine. You can download JRuby from the JRuby website (https://www.jruby.org/download) or install it using your package manager if you are using a Linux distribution.
- Once JRuby is installed, create a new directory for your JRuby application and navigate to it in the terminal.
- Run the following command to create a new JRuby application:
jruby -S rails new myapp
This will create a new JRuby on Rails application in the myapp
directory.
- Next, you will need to add the
jruby-complete
library to your application. This library is required for running JRuby applications on WildFly. You can add it to your application by running the following command:
jruby -S gem install jruby-complete
- To deploy your JRuby application to WildFly, you will need to create a WAR file. You can do this by running the following command in the root directory of your application:
jruby -S warble
This will create a WAR file in the pkg
directory of your application.
- To deploy the WAR file to WildFly, copy the WAR file to the
deployments
directory of the WildFly server. Alternatively, you can use the WildFly CLI to deploy the WAR file. - Start the WildFly server by running the following command:
./bin/standalone.sh
On Windows systems, you will need to use the standalone.bat
file instead.
- Your JRuby application should now be deployed and running on the WildFly server. You can access it by visiting
http://localhost:8080/myapp
, replacingmyapp
with the name of your application.
Using TorqueBox to run application on JBoss AS
The TorqueBox project is a framework to develop Ruby on Rails application on WildFly
Please keep in mind that the TorqueBox project is no longer actively maintained. Do not use it for development or production usage unless you are willing and able to fix issues as they come up.
Install TorqueBox using the gem tool:
gem "torquebox", "${version}"
You can also pick and choose various TorqueBox components if you don’t want the entire suite of services. To do that, remove the torquebox gem from your Gemfile and add the appropriate individual gems.
An example of using only the web and scheduling services:
gem "torquebox-web", "${version}" gem "torquebox-scheduling", "${version}"
The list of official TorqueBox gems are:
torquebox (aggregate of all the other gems if you want the entire stack)
torquebox-core (dependency of others – no need to specify in your Gemfile)
torquebox-caching
torquebox-messaging
torquebox-scheduling
torquebox-web
Then, create a new Ruby on Rails application using the rails command. For example:
rails new myapp
Add the TorqueBox gem to your Gemfile:
gem 'torquebox-rails'
Run bundle install to install the gem and its dependencies.
bundle install
Run the torquebox war command to create a WAR file for your application. For example:
torquebox war myapp
Running the war file directly
Even though the war file is designed to deploy into WildFly, it is possible to run it directly outside of WildFly as well. This is mainly to give the ability to run rake or other scripts from inside the war file, but you can also use it to run the application inside your war file outside of WildFly. Examples:
$ java -jar myapp.war $ java -jar myapp.war -S rake db:migrateFound the article helpful? if so please follow us on Socials