What is XRebel? What you can use it for? How to use it with Liferay? Let’s find out!

What is XRebel

First a few words of introduction on what is XRebel and what can we use it for.

XRebel, as it’s tagline says, is a “lightweight java profiler”.

What can it do for you? It can plug into your Java application and will give you insights on how and what your application is doing in the following areas:

  • database queries – especially handy for Hibernate users, as it gives you the ability to get the select statement AND the actual parameters that were bound
  • calling remote services – during integration with third party systems via some sort of web services (be it SOAP or REST) you need to provide specific requests and responses your system made during an erroneous call so that the other system provider can verify the correctness of it. XRebel gives easy access to the communication chain (with some limitations)
  • web requests – XRebel will show you what requests are being sent to your app and the responses the system produced
  • web session – see what you store in the session and check if the session object is not exploding in memory due to too much data

XRebel + Liferay – connecting the dots

Some initial assumptions regarding your setup:

  • XRebel 2.0.0 (Implementation-Version: 201504071124) installed at %XREBEL_PATH%
  • Java: jdk1.6.0_37 + jre1.6.0_37
  • Liferay 6.0.6 with bundled Tomcat 6.0.29 (Liferay Portal Community Edition 6.0.6 CE (Bunyan / Build 6006 / February 17, 2011) download here)  installed at %LIFERAY_HOME% with default settings (HSQL Database, port 8080)

To start working with XRebel we need to add one line to JAVA_OPTS variable (%LIFERAY_HOME%tomcat-6.0.29binsetenv.bat/%LIFERAY_HOME%tomcat-6.0.29binsetenv.sh):

“-javaagent:”%XREBEL_PATH%xrebel.jar”

After  starting Liferay we have a message in tomcat console log from XRebel:

2015-05-02 14:22:03 XRebel: Started XRebel for application: http://localhost:8080/

2015-05-02 14:22:03 XRebel: XRebel UI is available at http://localhost:8080/xrebel

If we run our application on http://localhost:8080/ it will now include XRebel GUI.  It may not work for all requests, especially with RESTful applications. To make it work we need to open it in a new browser tab http://localhost:8080/xrebel, where you will find a standalone UI for XRebel. There we can see all the requests targeted at application deployed in the ROOT context.

After we login into Liferay (user: test@liferay.com) we can see all HTTP requests with timings and percentage statistics for methods called the on server side.



In my opinion the most valuable thing in XRebel is the Query tab. In this example we can see SQL query with filled parameters and this query we can just copy and paste to our favourite SQL IDE and execute it or check execution plan.


query_view_after_login

We can see all objects stored in session just like on the screen:

session_view_after_login

Real-life example – integration of Liferay with National Bank of Poland currency service

To start testing we create xrebel-test portlet to see how XRebel can help us in developing with Liferay.

We create one servlet which returns a JSON-formatted currencies list. We are using our library to download currencies from NBP (National Bank of Poland). We want to download currencies and log some event data to the database.

Our servlet:

Our servlet “/xrebel-test/currenciesDownload” returns:

In XRebel UI(in context of our portlet) we can see:

screen3

As we can see XRebel detected our call to NBP and displayed Request/Response. However there is a little problem because we couldn’t see the whole response body from NBP service. I’ve tried to set the property „xrebel.injection.log_response” to true with hopes the it will help – to no avail. I think it would be great if it was possible to see the whole response body, but as of now it’s not possible – there’s a hard limit of 2048 characters. The reason for this is that the content might be too big to render all at once. In future releases there is supposed to be some solution for this problem.

The most interesting feature  for me is SQL query preview. We can see our select query for Counter and CurrencyDownloadLog entities, but we can’t see full data update and insert queries – the statements are there but the parameters bound to specific call are missing.

screen4

If we set the property „xrebel.injection.log_response”  to true we can see the insert and update queries in xrebel.log file, but again, without bound parameters:

We’ve sent above issue to XRebel team and they added a fix. I think it will be soon published in new release.

In Windows 7 xrebel.log file is located at %USERPROFILE%.xrebel and it contains logs from all our applications and overrides in next start of application. If we need, we can change this location just adding a VM argument:

-Dxrebel.log.file=/new/path/to/xrebel.log.

What is more, we can add next argument to set debug mode:

-Dxrebel.log=trace

Summary

To sum it up, the setup of XRebel with Liferay was pretty easy and in short amount of time I was able to get insights on the stuff my app was doing. XRebel provides a clear and useful UI to get the data needed for application profiling. As of writing of this post there are a few features that I would love to have to in XRebel to make the tool more complete but as of now it’s already very handy and can help with lots of common profiling tasks.

Share