« Back

Performance bottleneck in JSON web services in Liferay 6.0.x

General Blogs April 23, 2013 By Kan Zhang

This issue is addressed in Liferay 6.1.x. It only affacts Liferay 6.0.x.

Issue:

There is one performance bottleneck in JSON web services in Liferay 6.0.x: JSONServiceAction class is loading service class per request.

When doing a load test on the Liferay JSON web services (Example below), the performance is bad.

http://<IP_ADDRESS>:8080/tunnel-web/secure/json?serviceClassName=com.liferay.portal.service.CountryServiceUtil&serviceMethodName=getCountries

Cause:

In JSONServiceAction.getJSON() method:

On each of the request, the above code will load the service class based on the serviceClassName request parameter.

But the classloader.loadClass is a synchronized method, it becomes a performance bottleneck.

Solution:

The project I was working on about two years ago only requires to access a certain set of JSON services. My strategy was to preload the service classes into a cache to avoid runtime class loading.

In Liferay ext plugin:

1. Create ServiceCacheUtil class:

2. create CustomJSONServlet. This allows me to configure the list of service classes to preload in tunnel-web's web.xml and to use the CustomJSONServiceAction (see step 4) class.

3. Customize web.xml in tunnel-web to use the CustomJSONServlet:

4. create a CustomJSONServiceAction which extends JSONServiceAction:

5. You will need to do the same steps above in your custom portlet if you have a Service Builder generated custom JSON service in your portlet.

Note: The issue already got resolved in Liferay 6.1.x using @JSONWebService anotation. The @JSONWebService anotated services will be registered in the JSONWebServiceActionsManager to avoid runtime classloading.

Warning: The above code is not tested. I have implemented it two years ago but unfortunately I do not have that code any more. The above code I wrote is based on my memory - Use at your own risk.

 

Threaded Replies Author Date
Any one know how to use Gist in blog? I have... Kan Zhang April 23, 2013 3:33 PM
Kan - go to gist.github.com, cut/paste your... James Falkner April 24, 2013 7:42 AM
Thanks James! Kan Zhang April 24, 2013 8:40 AM
James - Looks like all the indents in Gist are... Kan Zhang April 24, 2013 8:55 AM

Any one know how to use Gist in blog? I have read James Falkner's blog about how to use Gist:
---------------
You can include gists (code snippets) using the 'Embed' option, and cutting/pasting the necessary javascript. This is the only kind of js allowed in your blog.
---------------
But I didn't find 'Embed' option in the editor....
Posted on 4/23/13 3:33 PM.
Kan - go to gist.github.com, cut/paste your code, click 'Create Public Gist', then you will see a 'Embed this Gist' option on the left, simply cut and paste the code (it starts with a <script> tag) into your Liferay blog, by first switching to the "Source" view of your blog (click the "Source" button), cut and paste, then switch back and beautify emoticon
Posted on 4/24/13 7:42 AM in reply to Kan Zhang.
Posted on 4/24/13 8:40 AM in reply to James Falkner.
James - Looks like all the indents in Gist are not displaying properly because of the following CSS on liferay site:
pre {
white-space: pre-line;
}
-------
Fixed by inserting the following CSS in "Source" view:
<style type="text/css">
pre {
white-space: pre !important;
}
</style>
Posted on 4/24/13 8:55 AM in reply to James Falkner.