TOTD #61 showed how to manage/monitor a Rails/Merb application running using JRuby/GlassFish using standard Java tools. Both the application and the management tools are running on the same machine. But that’s never the case in a production environment. Instead remote management of an application is required when running in production mode. Fortunately, similar set of tools can be used for remote management as well.
However the underlying Java Virtual Machine needs to be configured to enable remote monitoring. This is achieved by setting JAVA_OPTS environment variables as shown below:
Set up the following envinronment variable before running the Rails/Merb application:
export JAVA_OPTS=”-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8686 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false” |
The first property enables the JMX remote agent (aka MBeans server) and local monitoring. The second property configures the port number for remote management using JMX/RMI connections. The third and fourth properties, for simplicity only, disable access control and SSL for remote monitoring. It is highly recommended to use them in a production environment. Using Password Authentication provide more details about configuring passwords for access control. Using SSL provide more details about configuring SSL.
Now for a Rails/Merb application running on GlassFish all the MBeans are also available on the JMX agent running on port 8686. This agent can be accessed using the standard URL as shown below:
service:jmx:rmi:///jndi/rmi://192.168.124.1:8686/jmxrmi |
“192.168.124.1″ is the host’s IP address on which the Rails/Merb application is running and “8686″ is the port configured in JAVA_OPTS. More details about the URL are available here.
First, lets use jconsole on a different machine to monitor this application. Start jconsole by typing the command in a shell as:
jconsole |
In the “New Connection” window, select “Remote Process” and enter the URL defined above as shown below:
And then the management data snapshot is captured as shown below:
And here is a snapshot from a remote Solaris machine:
Notice the URL, as opposed to “pid”, is shown on the jconsole window. This indicates that remote connection is being used instead of local process id. TOTD #61 further explains how jconsole can be used to manage/monitor your Rails/Merb applications.
As mentioned in TOTD #61, JRuby runtime exposes 4 different MBeans – ClassCache, Config, JITCompiler and ParserStats – all in “org.jruby” domain. Lets write a client application that query these MBeans to monitor the state of JRuby runtime and perform some management actions based upon the returned results.
Here is the Ruby client code using Java Management Extensions (JMX) APIs.
include Java
import java.io.IOException url = JMXServiceURL.new “service:jmx:rmi:///jndi/rmi://192.168.124.1:8686/jmxrmi” names = HashSet.new(mbsc.query_names(ObjectName.new(“org.jruby:*”), nil)) # clear the cache if it’s full |
Here are the main tasks achieved by this code:
- Do include/import for Java classes
- Connect with the JMX Agent where JRuby MBeans are registered and obtain a MBean Server Connection (explained here)
- Query for all the MBeans in “org.jruby” domain
- Create proxies for ClassCache and Config MBeans
- Flush the ClassCache if its full and print some debugging information
These MBeans are part of JRuby runtime so provide relevant management/montoring information corresponding to that. But as you can see it’s fairly trivial to write a client to query MBeans and achieve dynamism using standard Ruby code.
Each JRuby-exposed MBean has multiple attributes that can be utilized to monitor your application and take appropriate actions. For example, an application could detect low memory and stop accepting new connections.
And you can continue to use GlassFish for running your Rails and Merb applications (now Sinatra as well)!
Subsequent blogs in this series will discuss:
- How to remotely manage your Rails/Merb applications using JMX API ?
- How to publish your own metrics that can be managed using JMX API ?
- How to use jmx gem to manage/monitor ?
- How to use VisualVM to get more information about the underlying VM ?
- How to use NewRelic/FiveRuns to manage/monitor an application ?
Please leave suggestions on other TOTD (Tip Of The Day) that you’d like to see. A complete archive of all tips is available here.
Technorati: totd glassfish jruby rubyonrails merb jmx manage monitor
Related posts:- TOTD #63: jmx4r gem – How to manage/monitor your Rails/Merb applications on JRuby/GlassFish ?
- TOTD #61: How to locally manage/monitor your Rails/Merb applications on JRuby/GlassFish using JMX ?
- TOTD #13: Setup Mongrel for JRuby-on-Rails applications on Windows
- TOTD #11: Setup Mongrel cluster for JRuby-on-Rails applications on Unix
- TOTD #79: Getting Started with Sinatra applications on JRuby and GlassFish Gem
[Trackback] TOTD #61 and TOTD #62 shows how to use JMX APIs to locally/remotely manage/monitor your Rails/Merb applications. This Tip Of The Day extends TOTD #62 and shows how remote management can be done using jmx4r gem. This gem provides…
Comment by Arun Gupta's Blog — January 13, 2009 @ 10:54 am
[Trackback] Here is a blog entry that was sitting in my Drafts folder for a long time (just because I didn’t realize . Anyway, it shows how to install Open Solaris 2008/11 on Virtual Box. The original install was done using…
Comment by Arun Gupta's Blog — January 13, 2009 @ 10:56 am
hmm. thx u.
Comment by sinema izle — March 10, 2009 @ 1:45 pm