Home     RSSRSS

Posts Tagged ‘Java’

Concurrency, Thread Safety and Local Variables

August 12, 2016 by kiranbadi1991 | Comments Off on Concurrency, Thread Safety and Local Variables | Filed in Development, Performance Center, Performance Engineering, Performance Test Tools

One of the most of the common reason for concurrency issues I often see in web application is due to concurrent access of data stored in variables. Generally in servlets , data in variables are often stored as Local variables, Instance Variables, Class Variables , request attributes, session attributes and context attributes.

Below example simplest I can think of for storing data as local variable and accessing it in a thread safe manner

public class MyServlet extends httpServlet {

  // mylocalage is localvariable here for this servlet.

    public void printAge(){
              int mylocalage = 0;
      mylocalageage = mylocalage - 10;
      System.out.println("My age 10 years earlier was: " + mylocalage);
      

    }
}

Its considered that by design that data stored in local variable is thread safe.

Every thread accessing the above servlet will have their own values and they will not interface with each other.

image

Local variables are stored in stack in Java. So data stored in these variables are thread safe.

Tags: , ,

Oops Error and Java Error/Exception Handling

April 4, 2014 by kiranbadi1991 | Comments Off on Oops Error and Java Error/Exception Handling | Filed in Development, Environment, Performance Engineering, Project Management, Web Server

Very Often during the testing for java based application, I come across the generic Oops error message which looks something like below screenshot,

image

Lot many developers who are building the java based application  or are new to development use these techniques to display error or exception message. It’s a good technique and even I have this technique in my application which I have coded.The code to display Oops error which I have in my application is something like below,

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page isErrorPage="true" %>
<html>
    <head>
        <title>Show Error Page</title>
    </head>
    <body>
        <h1>Oops...</h1>
        <table width="100%" border="1">
            <tr valign="top">
                <td width="40%"><b>Error:</b></td>
                <td>${pageContext.exception}</td>
            </tr>
            <tr valign="top">
                <td><b>URI:</b></td>
                <td>${pageContext.errorData.requestURI}</td>
            </tr>
            <tr valign="top">
                <td><b>Status code:</b></td>
                <td>${pageContext.errorData.statusCode}</td>
            </tr>
            <tr valign="top">
                <td><b>Stack trace:</b></td>
                <td>
                    <c:forEach var="trace" 
                               items="${pageContext.exception.stackTrace}">
                        <p>${trace}</p>
                    </c:forEach>
                </td>
            </tr>
        </table>
    </body>
</html>

The code above uses the JSTL tag and EL to display the error page.It works perfectly fine and it does display most of the exception which is thrown by the application.

However drawback of this approach  is that it shows error or exception directly to the end user and it means that we are leaking information to the outside world about our code base. Its not good. Some of the reasons as why its not good can be found here.

Most of the sites display catchy error screens when something goes wrong with site. They look so good and in fact  appeal the users. Great number of examples  can be found here.

Now changing from that Generic Oops page to some thing catchy and stylish is also very easy. It involves creating one static page with funny image in it and steps for the user to go to other section of the site or report the error message to site administrator or something which you want your users do when they see error page. However to implement this change some knowledge as how java handles error/exception is required. Below links are probably worth the read  to understand the science of java errors or exception,

http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html

http://www.artima.com/intv/solidP.html

http://www.onjava.com/pub/a/onjava/2003/11/19/exceptions.html?page=2

http://docs.oracle.com/javase/6/docs/technotes/guides/logging/overview.html

Now changing from Generic Oops page to some catchy page is also quite easy. Build the static JSP or HTML page and edit the web xml error page section. It looks something like below in Netbean editor,sorry I use netbean for development so I have screenshot based on it. Web xml can also edited in notepad or any other IDE.Editing via IDE is recommended since its heart of the application.

image

We can also write the exception handler servlet and build the page dynamically using the throwable class. The important thing to be considered here is that we are not leaking information and at the same time we are also informing users that something went wrong and they can follow some other path to their work. Once you have implemented the change, we can immediately test this change by triggering the error condition to see that in fact we are redirecting to error page correctly.

If you are QA  and if you see Oops Page, then probably you want to log the defect for this irrespective of the condition/data as what triggered the error.However you also need to make sure that exception stack trace is correctly logged in the logs and it has all the information that is required to debug the condition. If you are using any logging framework like Log4j ,it should not take more time to redirect the error to appender and verify the completeness of the message.

Tags: , ,

Log the Servlet Processing Time

January 12, 2014 by kiranbadi1991 | Comments Off on Log the Servlet Processing Time | Filed in Development, Environment, Performance Engineering

Recently one of my friend who had a Java based web application which was was recently developed and deployed in the production environment was facing some performance issues contacted me. For some reasons in that application couple of functionality was taking more time to show up than few other functionalities, after doing some investigation, we realized that couple of servlets were taking more time than others for processing on the server side. First we identified servlets, then identified DAO associated with those servlets and then finally queries associated with that DAO.Once queries were identified we did some load testing for those queries using different data sets.

We used the below program to gather the required information about the servlet processing time,

import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;
 
@WebListener
public class logprocessingtime implements ServletRequestListener {
    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        ServletRequest servletRequest = sre.getServletRequest();
        servletRequest.setAttribute("start", System.nanoTime());
    }
 
    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        ServletRequest servletRequest = sre.getServletRequest();
        Long start = (Long) servletRequest.getAttribute("start");
        Long end = System.nanoTime();
        HttpServletRequest httpServletRequest =
                (HttpServletRequest) servletRequest;
        String uri = httpServletRequest.getRequestURI();
        System.out.println("time taken to execute servlet " + uri + ":" + ((end - start) / 1000) + "microseconds");
    }
}

The above java program is basically a web listener which we registered it to the servlet container and it listens for every request that web application receives. For every request the application receives we log the response time taken for it to process the request.

This program meets the servlet 3.0 Specs and in case if you want it to port to 2.4 , then probably you need to register the listener with web.xml.

More information about Servlet Request listener can be found here. Its powerful interface and it helps to get lot of troubleshooting information.

Tags: , , ,