During performance testing execution on my application stack I came across following fault multiple times. My application stack uses Apache Tomcat 7 and JDK 1.7.0_55.
After investigation found that this is a bug in JDK JIT compiler which is fixed in later version started from update 60.
I had to get the release out and could not afford to upgrade the JDK version so used "-XX:-LoopUnswitching" switch to disable the loop unswitching.
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00002aaab0363f9e, pid=28321, tid=1102854464
#
# JRE version: 7.0_25-b15
# Java VM: Java HotSpot(TM) 64-Bit Server VM (23.25-b01 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# J org.apache.http.impl.cookie.BestMatchSpec.formatCookies(Ljava/util/List;)Ljava/util/List;
#
# An error report file with more information is saved as:
# /tmp/hs_err_pid28321.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
#
Tuesday, November 29, 2016
Friday, July 22, 2016
ERROR 1215 (HY000) at line 132: Cannot add foreign key constraint
If the data type of table column does not match to foreign key column then during schema creation MySQL throw error ERROR 1215 (HY000) at line 132: Cannot add foreign key constraint
Common mistake is that the foreign key column type is UNSIGNED integer but the column in other table is default integer.
Common mistake is that the foreign key column type is UNSIGNED integer but the column in other table is default integer.
Sunday, February 7, 2016
Gatling simple simulation example
This a simple Gatling simulation example. The POST request is already explained in my earlier blog Gatling post request with JSON body
The example will run the scenario using 5 users for 10 minutes duration with no pauses.
The example will run the scenario using 5 users for 10 minutes duration with no pauses.
import net.liftweb.json.DefaultFormats
import net.liftweb.json.Serialization._
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.core.scenario.Simulation
class MySimulation extends Simulation {
case class Person(name: String)
val createPerson = http("Create person")
.post("/person")
.body(StringBody(session => write(Person("Jack"))(DefaultFormats))).asJSON
val httpProtocol = http
.baseURL("https://localhost:8080/application")
.disableFollowRedirect
.disableAutoReferer
.disableCaching
.connectionHeader("keep-alive")
val myScenario = scenario("My scenario").during(10) {
exec(createPerson)
}.inject(rampUsers(5).over(1))
setUp(myScenario)
.pauses(disabledPauses)
.protocols(httpProtocol)
.assertions(global.failedRequests.count.is(0))
}
Gatling post request with JSON body
To make a POST request in Gatling with JSON request body use the following code -
The Person case class represents the JSON body structure you want to post.
The StringBody code line converts the case object into JSON representation for post.
Ensure to have following dependency in your classpath.
import net.liftweb.json.DefaultFormats
import net.liftweb.json.Serialization._
object PersonScript {
case class Person(name: String)
val createPerson = http("Create person")
.post("/person")
.body(StringBody(session => write(Person("Jack"))(DefaultFormats))).asJSON
}
The Person case class represents the JSON body structure you want to post.
The StringBody code line converts the case object into JSON representation for post.
Ensure to have following dependency in your classpath.
<dependency> <groupId>net.liftweb</groupId> <artifactId>lift-json_2.11</artifactId> <version>3.0-M7</version> </dependency>
Gatling read json response and store as list in session
Consider a REST end point /persons which returns JSON response of array of Person object. The Person object having property named id. You want to store all the returned id in personIds list in session to use it further.
The REST response is like this -
The above code will extract id values and store as list in personIds.
If REST response returns empty list then the above code will fail with error -
Using optional in the chain helps avert the error. Only if the findAll returns any thing the saveAs will execute and will work without failure.
[
{
"id": 1,
"name": "Jack",
},
{
"id": 2,
"name": "Jill"
}
]
val getAllPersons= http("Get all persons")
.get("/persons")
.check(status.is(200), jsonPath("$..id").findAll.optional.saveAs("personIds"))
The above code will extract id values and store as list in personIds.
If REST response returns empty list then the above code will fail with error -
jsonPath($..id).findAll.exists, found nothing
Using optional in the chain helps avert the error. Only if the findAll returns any thing the saveAs will execute and will work without failure.
Spring reloadable message source
Spring provides you to externalize your messages so that it can be changed without application restart. Add following snippet to your spring configuration.
The messages.file is pointing to property loaded by Spring PropertyConfigurer. For development purposes you can keep the messages file bundled with your project in classpath but for real deployment it will be outside.
The value should be like classpath:messages where the messages.properties file is kept in src/main/resources. The value can be messages.file=file:D:/config/messages if its kept out at this location.
The Spring messages are by default internalized so if you have done that setup it can pickup files like messages_en_GB.properties as per the locale.
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>${messages.file}</value>
</list>
</property>
<property name="cacheSeconds" value="1" />
</bean>
The messages.file is pointing to property loaded by Spring PropertyConfigurer. For development purposes you can keep the messages file bundled with your project in classpath but for real deployment it will be outside.
The value should be like classpath:messages where the messages.properties file is kept in src/main/resources. The value can be messages.file=file:D:/config/messages if its kept out at this location.
The Spring messages are by default internalized so if you have done that setup it can pickup files like messages_en_GB.properties as per the locale.
Spring externalize application configuration
To configure your application is different environment using different setup use the following in Spring framework -
Setup environment variable APPLICATION_CONFIG_HOME which points to directory where customer application.properties is available. If the file is available then it will override those properties which you have specified in that file.
For local development user need not required to define this as mostly developers will use the default.properties.
This setup provies an option to override if required in a particular environment e.g. QA, performance, staging, production, etc.
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:default.properties</value>
<value>file:${APPLICATION_CONFIG_HOME}/application.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="true" />
<property name="searchSystemEnvironment" value="true" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
</bean>
By default it will read the default.properties available in your application classpath. Typically it is available at src/main/resources/default.proerties.
Setup environment variable APPLICATION_CONFIG_HOME which points to directory where customer application.properties is available. If the file is available then it will override those properties which you have specified in that file.
For local development user need not required to define this as mostly developers will use the default.properties.
This setup provies an option to override if required in a particular environment e.g. QA, performance, staging, production, etc.
Spring data JPA Pessimistic lock
If you want to lock a database record to execute certain business logic and do not want any other thread to update the same then do the following -
import org.springframework.data.jpa.repository.Lock; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.query.Param; import javax.persistence.LockModeType; public interface PersonRepository extends CrudRepositoryIn this example Person is an entity and I want to lock a Person record based on id. The service layer method must be in transaction to use this method.{ @Lock(LockModeType.PESSIMISTIC_WRITE) @Query("select p from Person p where p.id = :id") Person findOneAndLock(@Param("id") int id); }
Simple Gatling setup with Maven
Just add the following maven pom snippet to your project pom.xml.
Create maven style project structure of src/test/scala. As per the example below your default simulation class is com.company.project.MySimulation. If you want to create different name/package pass -Dsimulation= to execute that simulation.
All of the properties mentioned in the pom can be overridden via command line using -D option.
Execute mvn test to run your gatling performance simulation.
Create maven style project structure of src/test/scala. As per the example below your default simulation class is com.company.project.MySimulation. If you want to create different name/package pass -Dsimulation=
All of the properties mentioned in the pom can be overridden via command line using -D option.
Execute mvn test to run your gatling performance simulation.
<project>
<properties>
<simulation>com.company.project.MySimulation</simulation>
<applicationUrl>http://localhost:8080/application</applicationUrl>
<noOfUsers>1</noOfUsers>
<durationInMinutes>1</durationInMinutes>
<rampUpInMinutes>1</rampUpInMinutes>
</properties>
<dependencies>
<dependency>
<groupId>io.gatling.highcharts</groupId>
<artifactId>gatling-charts-highcharts</artifactId>
<version>2.2.0-M3</version>
</dependency>
<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-app</artifactId>
<version>2.2.0-M3</version>
</dependency>
<dependency>
<groupId>net.liftweb</groupId>
<artifactId>lift-json_2.11</artifactId>
<version>3.0-M7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<groupId>io.gatling</groupId>
<artifactId>gatling-maven-plugin</artifactId>
<version>2.2.0-M2</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<configFolder>src/test/resources</configFolder>
<simulationsFolder>src/test/scala</simulationsFolder>
<simulationClass>${simulation}</simulationClass>
<noReports>false</noReports>
<jvmArgs>
<jvmArg>-DapplicationUrl=${applicationUrl}</jvmArg>
<jvmArg>-DnoOfUsers=${noOfUsers}</jvmArg>
<jvmArg>-DdurationInMinutes=${durationInMinutes}</jvmArg>
<jvmArg>-DrampUpInMinutes=${rampUpInMinutes}</jvmArg>
<jvmArg>-Xms2048M</jvmArg>
<jvmArg>-Xmx2048M</jvmArg>
</jvmArgs>
<propagateSystemProperties>true</propagateSystemProperties>
<failOnError>true</failOnError>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Friday, January 16, 2015
Accept encoding with Jersey REST API
Consider following Jersey resource definition -
@Path("/data")
@GET
@Produces(MediaType.TEXT_HTML)
public String getData();
If the client does not provide HTTP request header "Accept" with value "text/html" you will see following exception in your application. To fix ensure client provided Accept encoding matches with what is defined by the REST API.
javax.ws.rs.WebApplicationException: null
at com.sun.jersey.server.impl.uri.rules.TerminatingRule.accept(TerminatingRule.java:66) ~[jersey-server-1.17.jar:1.17]
at com.sun.jersey.server.impl.uri.rules.ResourceObjectRule.accept(ResourceObjectRule.java:100) ~[jersey-server-1.17.jar:1.17]
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) ~[jersey-server-1.17.jar:1.17]
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) ~[jersey-server-1.17.jar:1.17]
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1511) [jersey-server-1.17.jar:1.17]
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1442) [jersey-server-1.17.jar:1.17]
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391) [jersey-server-1.17.jar:1.17]
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381) [jersey-server-1.17.jar:1.17]
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416) [jersey-servlet-1.17.jar:1.17]
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538) [jersey-servlet-1.17.jar:1.17]
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716) [jersey-servlet-1.17.jar:1.17]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) [servlet-api-3.0.jar:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) [tomcat-catalina-7.0.42.jar:7.0.42]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [tomcat-catalina-7.0.42.jar:7.0.42]
Tuesday, June 11, 2013
Simple slf4j configuration with Maven
Just add the following dependencies to your project pom.xml. The slf4j logging will be up and running with default implementation of logback configuration. The default configuration shows logs of DEBUG and above level for all packages.
Each dependencies are explained below -
Each dependencies are explained below -
- slf4j-api - slf4j api which you will use in your code to log messages.
- commons-logging - note the version this is too exclude all the commons-logging dependencies coming from other dependencies of your project. This version is available at http://version99.qos.ch/commons-logging/commons-logging/99-empty/
- logback-* - logback implementation which will be used to configure/control the logging. You can choose to use log4j or other implementation also.
- jcl-over-slf4j - enables migration from commons logging to slf4j without any code changes
- log4j-over-slf4j - enables migration from log4j logging to slf4j without any code changes
- jul-over-slf4j - enables migration from java util logging to slf4j without any code changes
- osgi-over-slf4j - enables migration from osgi logging to slf4j without any code changes
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>99-empty</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.0.11</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.11</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>1.0.11</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.7.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>osgi-over-slf4j</artifactId>
<version>1.7.5</version>
<scope>runtime</scope>
</dependency>
Thursday, June 6, 2013
Spring MVC setup using Java Config
Add following config in the web.xml. The following config registers Spring listener. The listener knows that the configuration is based on Java Config by context param "contextClass". The context param "contextConfigLocation" tells the starting configuration class.
The servlet knows that the configuration is based on Java Config by init param "contextClass". The init param "contextConfigLocation" tells the starting configuration class.
<context-param> <param-name>contextClass</param-name> <param-value> org.springframework.web.context.support.AnnotationConfigWebApplicationContext </param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>com.project.config.ApplicationConfiguration</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>Sample configuration class. Note the "@Configuration" which tells that this class is a configuration class. The below configuration will scan package com.project and look for @Configuration, @Service, @Repository, @Component annotated class and create beans. Note that @Controller is excluded as I like to keep that separate in dispacther servlet context (explained later in the article). Also "WebConfiguration.class" is excluded as it will be used in the dispatcher servlet context.
@Configuration
@ComponentScan(basePackages = "com.project", excludeFilters = { @Filter(value = WebConfiguration.class, type = FilterType.ASSIGNABLE_TYPE),
@Filter(value = Controller.class) })
public class ApplicationConfiguration {
}
Add following config in the web.xml. The following config registers a dispatcher servlet. The dispatcher servlet routes urls to the respective controller. In this example urls ending with ".do" will be handled by Spring MVC.The servlet knows that the configuration is based on Java Config by init param "contextClass". The init param "contextConfigLocation" tells the starting configuration class.
<servlet>
<servlet-name>SpringDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.project.server.config.WebConfiguration</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringDispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Sample configuration class. Note the "@Configuration" which tells that this class is a configuration class. The below configuration will scan package "com.project" and look for @Configuration, @Component annotated class and create beans. Note that @Service, @Repository are excluded as I like to keep that separate in application context (see above). Also "ApplicationConfiguration.class" is excluded as it will be used in the application context.
@Configuration
@ComponentScan(basePackages = "com.project", excludeFilters = { @Filter(value = Service.class), @Filter(value = Repository.class),
@Filter(value = ApplicationConfiguration.class, type = FilterType.ASSIGNABLE_TYPE) })
@EnableWebMvc
public class WebConfiguration {
}
Wednesday, June 5, 2013
Spring MVC - HTTP Status 406
If you have encountered following errors with Spring MVC returning JSON response using @ResponseBody -
Then here is the explanation and solution -
Say for example you have this controller -
You will get the mentioned errors due to the above reason.
The POJO which is getting returned as @ResponseBody must have at least one public getter.
- On the broswer when accessing a resource -> HTTP Status 406 - The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
- In the logs -> org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
Then here is the explanation and solution -
Say for example you have this controller -
@Controller
public class TestController {
@RequestMapping(value = "/test")
public @ResponseBody Test test() {
return new Test();
}
}
The Test POJO -
public class Test {
private String name;
protected String getName() {
return name;
}
}
Notice that the POJO has only one getter and that too protected.You will get the mentioned errors due to the above reason.
The POJO which is getting returned as @ResponseBody must have at least one public getter.
Monday, December 28, 2009
Execute stored procedure in spring with return value and out parameters
Following code example shows how to execute stored procedure in spring with return value and out parameters. The comment along with the code explains the usage.
import java.math.BigDecimal;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
public class StoredProcedureExampleDAO extends SimpleJdbcCall {
public JdbcDequeueTicketDAO(final JdbcTemplate jdbcTemplate) {
super(jdbcTemplate);
// Here declare all the IN and OUT parameters defined for the stored
// procedure
declareParameters(new SqlParameter("CountryCode", Types.VARCHAR),
new SqlOutParameter("CountryName", Types.VARCHAR));
// Register the stored procedure name
withProcedureName("getCountryName");
// This ensures the stored procedure return value also gets populated
// in the returned Map with key "return".
withReturnValue();
}
public final String getCountryName(final String countryCode) {
// Map to send the IN params values.
MapinParams = new HashMap ();
inParams.put("CountryCode", countryCode);
// Map for the OUT params and return value.
MapoutParams = null;
try {
outParams = execute(inParams);
} catch (Throwable t) {
throw new DataAccessException("Failed to get the country name", t);
}
if (null != outParams) {
BigDecimal returnValue = (BigDecimal) outParams.get("return");
// The logic of getCountryName stored procedure is that its
// returns 1 if execution is successful otherwise returns 0.
if (null != returnValue && 1 == returnValue.intValue()) {
// On success read the CountryName value
String countryName = (String) outParams.get("CountryName");
return countryName;
}
}
throw new DataAccessException("Failed to get the country name");
}
}
Tuesday, December 15, 2009
Logging using Aspect Oriented Programming
Most of the time while debugging I like to see the values of the incoming parameters and what is the returned value. The only solution is to use Eclipse debugger but some times its always not possible to run eclipse debugger. For example, the application is running in some environment where you cannot connect or sometimes you want to check quickly by just switching on DEBUG level logging.
Today I used Aspect Oriented Programming (AOP) to log incoming parameters and return value of a method execution. There are multiple ways but I like the following approach -
Ensure that required log4j and aspect jars are in the classpath of your project. The log4j is properly configured and gets initialized. If don't want to use log4j then in the MethodLogger class replace the log4j related code with whatever you require.
First create the following annotation which will be used against those methods for which logging is required -
Now create following Aspect class in your source code. The comments explain the logic.
Today I used Aspect Oriented Programming (AOP) to log incoming parameters and return value of a method execution. There are multiple ways but I like the following approach -
Ensure that required log4j and aspect jars are in the classpath of your project. The log4j is properly configured and gets initialized. If don't want to use log4j then in the MethodLogger class replace the log4j related code with whatever you require.
First create the following annotation which will be used against those methods for which logging is required -
public @interface LogMethod {
}
Now create following Aspect class in your source code. The comments explain the logic.
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
// The Aspect annotation makes this class an Aspect
@Aspect
public class MethodLogger {
// The Before annotation ensures beforeMethod is called before the
// execution of a method annotated by LogMethod.
@Before(value = "@annotation(LogMethod)")
public void beforeMethod(JoinPoint joinPoint) throws Throwable {
// This ensures that the logger is of the actual class. If %c
// pattern is used in log4j then the class name of the actual
// method will print.
Logger logger = Logger.getLogger(joinPoint.getTarget().getClass());
if (logger.isDebugEnabled()) {
StringBuilder builder = new StringBuilder();
// Here we print the name of the method
builder.append(joinPoint.getSignature().getName());
builder.append("(");
// Here we print the values of the method arguments
appendArgumentValues(builder, joinPoint.getArgs());
builder.append(")");
logger.debug(builder.toString());
}
}
// The AfterReturning annotation ensures afterReturningMethod
// is after the execution of a method annotated by LogMethod.
@AfterReturning(value = "@annotation(LogMethod)", returning = "returnValue")
public void afterReturningMethod(JoinPoint joinPoint, Object returnValue)
throws Throwable {
Logger logger = Logger.getLogger(joinPoint.getTarget().getClass());
if (logger.isDebugEnabled()) {
StringBuilder builder = new StringBuilder();
builder.append(joinPoint.getSignature().getName());
builder.append("(");
// This prints the method argument class type
appendArguments(builder, joinPoint.getArgs());
builder.append("):");
// This prints the return value of the method. Incase of void
// it prints null
builder.append(returnValue);
logger.debug(builder.toString());
}
}
private void appendArguments(StringBuilder builder, Object[] objects) {
for (Object obj : objects) {
builder.append(obj.getClass().getName()).append(",");
}
if (objects.length > 0) {
builder.deleteCharAt(builder.length() - 1);
}
}
private void appendArgumentValues(StringBuilder builder, Object[] objects) {
for (Object obj : objects) {
builder.append(obj).append(",");
}
if (objects.length > 0) {
builder.deleteCharAt(builder.length() - 1);
}
}
}
Thursday, November 19, 2009
SpringSource Tool Suite
I have started using STS for the past 3-4 months. The tool is powered by the latest Eclipse version Galileo and comes bundled with useful plugins for Spring based development. The tool also comes with the maven plugin bundled in. I always use Maven and Spring for all my Java projects so I am enjoying this tool.
The suite also comes with Apache Tomcat server called TC Server. The server comes with a web application named "Insight". This web application tracks performance of the web applications deployed in this server. The application have a nice UI to drill down the statistics to pin point the code hampering the performance. No doubt the tool is very helpful to debug the code and find the performance bottlenecks. The beauty is no configurations are required at all.
The suite also comes with Apache Tomcat server called TC Server. The server comes with a web application named "Insight". This web application tracks performance of the web applications deployed in this server. The application have a nice UI to drill down the statistics to pin point the code hampering the performance. No doubt the tool is very helpful to debug the code and find the performance bottlenecks. The beauty is no configurations are required at all.
Friday, November 13, 2009
SAXParser usage code sample
How to Parse XML in Java is always a tricky question. There different types of parsers available mainly - DOMParser (JDOM, Dom4j, XOM, etc.), SAXParser (Xerces, Piccolo, etc) and StAXParser (XPP3, Woodstox, Aalto, etc). Depending upon the nature and requirement of the project you need to select the type of parser.
Normally StAX Parsers also known as Pull Parsers are the fastest in terms of processing any size XML. The implementations available today don't support XSD validations. Some implementations support DTD validation only. StAX parsers also provide API to create an XML.
SAX Parsers also know as Push Parsers are the next best in terms of performance. Most of the implementations support XSD as well as DTD validations.
DOM Parsers are also fast but have memory overhead as it builds the whole XML tree in memory. Most of the implementations support XSD as well as DTD validations. The benefit of DOM is when you need to play with small size XMLs, need to read back and forward, change the XML and output new XML, etc. DOM parser also comes with good API support to traverse the XML compared to the handler implementation one need to do in case of SAX and StAX Parsers.
I required to parse a huge XML file and also validate against the defined XSD. I choose SAX parser because DOM have memory overhead and StAX doesn't support XSD validation. I have used Sun JDK6 provided Xerces implementation of SAX parser.
Here is the code snippet which parse the XML as well as validate. The comments in the code explains the functionality.
Normally StAX Parsers also known as Pull Parsers are the fastest in terms of processing any size XML. The implementations available today don't support XSD validations. Some implementations support DTD validation only. StAX parsers also provide API to create an XML.
SAX Parsers also know as Push Parsers are the next best in terms of performance. Most of the implementations support XSD as well as DTD validations.
DOM Parsers are also fast but have memory overhead as it builds the whole XML tree in memory. Most of the implementations support XSD as well as DTD validations. The benefit of DOM is when you need to play with small size XMLs, need to read back and forward, change the XML and output new XML, etc. DOM parser also comes with good API support to traverse the XML compared to the handler implementation one need to do in case of SAX and StAX Parsers.
I required to parse a huge XML file and also validate against the defined XSD. I choose SAX parser because DOM have memory overhead and StAX doesn't support XSD validation. I have used Sun JDK6 provided Xerces implementation of SAX parser.
Here is the code snippet which parse the XML as well as validate. The comments in the code explains the functionality.
import java.io.File;
import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SAXXMLValidator {
private SAXParser saxParser;
public SAXXMLValidator(String schemaFilePath) throws SAXException,
ParserConfigurationException {
// Creates a schema factory object for the XSD validation
SchemaFactory schemaFactory = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// schemaFilePath is the abosulte path, you can use any other way of providing the file.
Schema schema = schemaFactory.newSchema(new File(schemaFilePath));
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
// Set validating as false as this only validates against the DTD mentioned in the XML document.
saxParserFactory.setValidating(false);
// If your XSD uses namespace then set this to true otherwise you will get error like this "cvc-elt.1: Cannot find the declaration of element 'project'"
saxParserFactory.setNamespaceAware(true);
// Provide the schema to the factory for the parser to validate the XML
saxParserFactory.setSchema(schema);
// Creates a SAXParser and its thread safe so best to initialize all this in Constructor to save creation cost at the time of call
saxParser = saxParserFactory.newSAXParser();
}
public void validate(String xmlFilePath, DefaultHandler defaultHandler)
throws SAXException, IOException {
// xmlFilePath is the abosulte path, you can use any other way of providing the file.
// Extend the DefaultHandler to create your own handler to parse the XML and collect the errors as DefaultHandler already implements ErrorHandler.
saxParser.parse(new File(xmlFilePath), defaultHandler);
}
public static void main(String[] args) {
try {
// I am using maven xsd and pom to test the code
SAXXMLValidator saxxmlValidator = new SAXXMLValidator(
"D:\\maven-v4_0_0.xsd");
saxxmlValidator.validate("D:\\pom.xml", new DefaultHandler());
} catch (SAXException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Friday, October 30, 2009
Configure Log4j for a web application on Apache Tomcat
Today I stumbled upon a problem of configuring log4j for a web application on Apache Tomcat. The problem is where to keep the log4j.xml and how initialize the logging system. One way is to keep the log4j.xml anywhere and change the Tomcat startup script to add -Dlog4j.configuration=<log4j.xml file path> but I do not want to depend on a change in Tomcat startup script.
I tried all the possible places but it did not work. I realized unless I define the log4j.configuration system parameter it will not work. I then looked at log4j api and found DOMConfigurator class which initializes the log4j system from log4j.xml.
Here is what I done to keep all configurations inside my application.
Create a listener class as shown below to initialize the log4j using the log4j.xml deployed with your application.
Add these entries to your web.xml. Here we are defining a parameter to configure the log4j.xml file path and registering the above created listener.
<pre>
<context-param>
<param-name>log4jXMLFilePath</param-name>
<param-value>WEB-INF/classes/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>Log4jInitializationListener</listener-class>
</listener>
</pre>
Start the application to confirm log4j is working. I have used the following log4j configuration so here it creates a server.log file.
I tried all the possible places but it did not work. I realized unless I define the log4j.configuration system parameter it will not work. I then looked at log4j api and found DOMConfigurator class which initializes the log4j system from log4j.xml.
Here is what I done to keep all configurations inside my application.
Create a listener class as shown below to initialize the log4j using the log4j.xml deployed with your application.
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
public class Log4jInitializationListener implements ServletContextListener {
public Log4jInitializationListener() {
}
public void contextInitialized(ServletContextEvent servletContextEvent) {
DOMConfigurator.configureAndWatch(servletContextEvent.getServletContext().getInitParameter("log4jXMLFilePath"));
Logger logger = Logger.getLogger(Log4jInitializationListener.class);
logger.debug("Log4j working");
}
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
Add these entries to your web.xml. Here we are defining a parameter to configure the log4j.xml file path and registering the above created listener.
<pre>
<context-param>
<param-name>log4jXMLFilePath</param-name>
<param-value>WEB-INF/classes/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>Log4jInitializationListener</listener-class>
</listener>
</pre>
Start the application to confirm log4j is working. I have used the following log4j configuration so here it creates a server.log file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="RollingFileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="server.log"/>
<param name="MaxFileSize" value="100MB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %l - %m%n" />
</layout>
</appender>
<root>
<priority value="debug"/>
<appender-ref ref="RollingFileAppender"/>
</root>
</log4j:configuration>
Friday, June 19, 2009
Project Euler Problem 5
Problem 5
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?
public class ProblemFive {
public static void main(String[] args) {
// Any number will be always divisible by 1
// To be divisible by 2 it has to be an even number
// Number will be greater than or equal to at least 20
int start = 20;
while (true) {
int i = 3;
for (; i <= 20; ++i) {
if (0 != (start % i)) {
break;
}
}
if (i >= 20) {
break;
}
start += 2;
}
System.out.println(start);
}
}
Wednesday, June 17, 2009
RIA frameworks compared
Web based application having desktop like GUI is popularly known as RIA (Rich Internet Application). In old days only few solutions were available namely Java applet, Flash, Dynamic html, etc. Nowadays there are loads of frameworks which provide desktop like experience. These frameworks are based on AJAX (Asynchronous JavaScript + XML) technology.
The choices of RIA frameworks are huge and it's difficult to choose the most suitable for your need. The best part of open source technology is for every problem there are numerous solutions, but the worst part is choosing the right technology for your need.
I found these available frameworks to build RIA - Adobe Flex, extJS, Jboss Richfaces, IceFaces, Oracle ADF, JavaFX, Silverlight, GWT, IT Mill Toolkit, ZK, OpenLaszlo, BackBase, Echo, Morfik, Haxe, YUI, pyjamas, DWR, Prototype, Curl, SproutCore, Cappuccino, jQuery, etc.
A selection criterion needs to be defined to select a suitable framework. I choose these criteria -
Popularity of the framework - The framework should be popular in developer community. Popularity brings good publications, community support, improvements, etc.
Support - A framework should have a good support in terms of books, training facilities, mailing lists, etc.
Out-of-the-box GUI components - A framework should provide ready to use components to save time in building basic blocks.
Underlying technology - Underlying technology is also important to check the learning curve, effort required, developer expertise, etc.
After applying the above criteria, I am left with extJS, Flex, GWT and Silverlight. These frameworks provide rich look and feel and have loads of ready to use components.
extJS
It's a cross browser Java Script library for building RIA.
Advantages
Huge set of rich widgets.
Good API documentation available.
Disadvantages
It's not free.
JavaScript knowledge required.
No good support for Web Service.
To do simple things, lot of JavaScript coding required.
CSS customization is not easy.
Firebug debugging is of no use as the generated html is bad.
Adobe Flex
Flex is a highly productive, free open source framework for building and maintaining expressive web applications that deploy consistently on all major browsers, desktops, and operating systems.
Advantages
Best look and feel.
Rich design and multimedia as its flash.
Easy to deploy.
Flex Builder IDE to develop UI using drag and drop.
Open source.
Web service integration is very easy.
Desktop application can be also developed.
Disadvantages
Flex Builder is not free.
Knowledge of MXML and ActionScript required.
Requires Plugin installation on the client side.
Browser history support not possible.
Rendering flash is still slow.
RPC calls are cumber some and need third party software like BlazeDS.
GWT (Google Web Toolkit)
Writing web apps today is a tedious and error-prone process. Developers can spend 90% of their time working around browser quirks. In addition, building, reusing, and maintaining large JavaScript code bases and AJAX components can be difficult and fragile. Google Web Toolkit (GWT), especially when combined with the Google Plugin for Eclipse, eases this burden by allowing developers to quickly build and maintain complex yet highly performant JavaScript front-end applications in the Java programming language.
Advantages
Developer need to know only Java. No JavaScript.
Huge choices of ready to use components.
Eclipse IDE support to develop.
Code can be debugged in Eclipse.
Eclipse hosted mode available for hot deployment.
Look and feel is good and can be easily customized using CSS.
Maven supported so builds are easy.
Junit test cases to test the code.
Easy Web Service configuration based on annotations.
Similar to Java Swing.
Loads of active mailing lists to seek solution.
Doesn't require Plugin installation on the client side.
GWT is free so no cost.
Browser history support works.
Disadvantages
Only for Java developers.
Compiling Java to JavaScript is very slow.
No drag and drop IDE available.
Desktop application cannot be developed.
Microsoft Silver light
Microsoft Silver light is a free runtime that powers rich application experiences and delivers high quality, interactive video across multiple platforms and browsers, using the .NET framework.
Advantages
.Net knowledge helps.
Visual Studio IDE support.
Desktop applications can be developed.
Disadvantages
Requires plugin installation on the client side.
Fairly new framework so wider support is not available.
Its not open source.
The choices of RIA frameworks are huge and it's difficult to choose the most suitable for your need. The best part of open source technology is for every problem there are numerous solutions, but the worst part is choosing the right technology for your need.
I found these available frameworks to build RIA - Adobe Flex, extJS, Jboss Richfaces, IceFaces, Oracle ADF, JavaFX, Silverlight, GWT, IT Mill Toolkit, ZK, OpenLaszlo, BackBase, Echo, Morfik, Haxe, YUI, pyjamas, DWR, Prototype, Curl, SproutCore, Cappuccino, jQuery, etc.
A selection criterion needs to be defined to select a suitable framework. I choose these criteria -
After applying the above criteria, I am left with extJS, Flex, GWT and Silverlight. These frameworks provide rich look and feel and have loads of ready to use components.
extJS
It's a cross browser Java Script library for building RIA.
Advantages
Disadvantages
Adobe Flex
Flex is a highly productive, free open source framework for building and maintaining expressive web applications that deploy consistently on all major browsers, desktops, and operating systems.
Advantages
Disadvantages
GWT (Google Web Toolkit)
Writing web apps today is a tedious and error-prone process. Developers can spend 90% of their time working around browser quirks. In addition, building, reusing, and maintaining large JavaScript code bases and AJAX components can be difficult and fragile. Google Web Toolkit (GWT), especially when combined with the Google Plugin for Eclipse, eases this burden by allowing developers to quickly build and maintain complex yet highly performant JavaScript front-end applications in the Java programming language.
Advantages
Disadvantages
Microsoft Silver light
Microsoft Silver light is a free runtime that powers rich application experiences and delivers high quality, interactive video across multiple platforms and browsers, using the .NET framework.
Advantages
Disadvantages
Labels:
extJS,
Flex,
framework,
gwt,
rich-internet-application,
Silverlight
Subscribe to:
Posts (Atom)