Struts2 – Performance Tuning
I was involved in migration of a very web application recently. The main reason for migration was cited as performance. Finally we have choosen Struts 2 (Struts 2.2.1.1 to be exact) for this migration. I have done some POC and found that performance was good.
Days passed by and we ended up with a application that contained thousands of JSP. When I tested the application for performance and I was shocked! The response was slow beyond the imagination. I started looking for performance tips on the net (google actually) and landed on this Official Struts 2 Performance Tuning Link. Applied the required changes based on the document but result was still shocking.
Further delved into XML files available in struts 2 jar’s and came up with following solutions and performance was much better than the imagination. And yes this was a sigh of relief for me. Try it out this will surely improve performance of any struts 2 based applications.
S.No | Changes Needs to be Done | Struts 2 File / JAR To be modified | Benefits |
1 | <constant name="struts.devMode" value="false" /> |
struts.xml | If devMode is set to true, it allows reloading of configuration and validation related files, but because they happen on each request ,this setting will totally kill your performance.So, Turn off the devMode to increase performance. |
2 | <constant name="struts.freemarker.templatesCache" value="true"/> |
struts.xml struts2-core-2.2.1.1.jar struts2-dojo-plugin-2.2.1.1.jar |
It enable the struts internal caching of Freemarker templates. Just adding this property will not change anything. You have to modify the struts2-core-2.2.1.1.jar and struts2-dojo-plugin-2.2.1.1.jar by moving out the template folder respectively and add this folder to root of the web application (parallel to WEB-INF). The built in Freemarker caching mechanism fails to cache templates when they are retreived from class path(or jars) and decrease the performance |
3 | <constant name="struts.xslt.nocache" value="false"/> |
struts.xml | This property configures the XSLTResult class to use stylesheet caching. Set this property to true for developers and false for productions. |
4 | <constant name="struts.ognl.logMissingProperties" value="=false"/> |
struts.xml | Logs as warning properties that are not found |
5 | <constant name="struts.el.throwExceptionOnFailure" value="false"/> |
struts.xml | This property instructs struts 2 runtime whether to throw a runtime exception when a property is not found in an expression or when the expression evaluates to fails |
6 | <constant name="struts.ognl.enableExpressionCache" value="true"/> |
struts.xml | Caches parsed OGNL expression. If your application generates a lot of OGNL expressions,this property saves a lot. |
7 | <constant name="struts.serve.static" value="false"/> |
struts.xml | FilterDispatcher uses this attribute. If true then Struts serves static content from inside the jars and if false struts serves static content from the /<WEBAPP_ROOT_DIR>/struts. To achieve this move the static folder content from struts2-dojo-plugin-2.2.1.1.jar(when using Ajax theme(DOJO) or Calender tag ) and core-2.2.1.1.jar to /<WEBAPP_ROOT_DIR>/struts. |
8 |
Add a freemarker.properties file in WEB-INF/classes folder and add the following:.
|
WEB-INF/classes/freemarker.properties | This value determine s how often Freemarker checks if it needs to reload the template from disk.In production this feature is not needed, thus set it to larger value for better performance |
9 | Use ${attribute} rather <s:property value=’#attribute’ /> |
JSP | |
10 |
Use "<s:property value="{getText(‘A_KEY’)}"/> rather than <s:i18n name="messageresource.package"> <s:text name="A_KEY"/> </s:i18n>" |
JSP |
Leave a Reply