Tuesday, August 5, 2008

Performance monitoring web applications with JavaScript

As end users, we often complain about slow web applications, and the same complaint is fired by the user towards us as web application developer. Most used monitoring solutions are based on some request filter implementation, where the server logs the timestamps of the incoming request and outgoing response. This works fine and provides good insight how much time we spend at the application server.

But how do we monitor the delay between the browser and the server, consisting out many multiple firewalls, proxies and many other slow network nodes? Especially when the browser machine is connected to internet directly, without some kind of corporate LAN where you could introduce a monitoring proxy?

After a bit of brainstorming with a smart colleague (thanks Marc), I found a very simple solution based on JavaScript. It catches browser navigation events using the load and unload events and stores the timestamps in a HTTP cookie variable. It does NOT work for asynchronous AJAX events, nor for separately loaded images, but it works well enough for us.


function enterPage() {
var endTime = new Date().getTime()
var timeIndex = document.cookie.indexOf("exitTime=")
var endPoint = document.cookie.indexOf(";", timeIndex)
var startTime = document.cookie.substring(timeIndex + 9, endPoint)
var duration = endTime - startTime
document.cookie = "duration=" + duration + ";"
}

function exitPage() {
var startTime = new Date().getTime()
document.cookie = "exitTime=" + startTime + ";"
}

<body onload="enterPage()" onunload="exitPage()">
my example page
</body>


The browser stores the load duration time in a cookie after the new page is rendered, and posts the cookie to the server in next HTTP request. Since we use J2EE at the server, we simply use the HttpServletRequest.getCookies() method to extract the value.

HTML events
JavaScript and Cookies

Thursday, July 10, 2008

Using dependency injection in EJB2

Many J2EE developers are still facing legacy projects, often containing horrible EJB2 session beans. Once I understood the improvements of EJB3 and the Spring alternatives for dependency injection, I cursed the ugly dependency look code in old EJB2 code.

Even if you cannot switch to an EJB3, you can still improve your existing code.
This is a sample of a typical EJB2 bean:


public class BookingBean implements SessionBean {
private CustomerDAO customerDAO;
private HotelDAO hotelDAO;
public void ejbCreate() {
customerDAO = Locator.lookup(CustomerDAO.class);
hotelDAO = Locator.lookup(HotelDAO.class)
}
}


If you have a lot of session beans, based on top of a large set of DAO instances, all this becomes a maintenance nightmare. It is very obvious that the code in the ejbCreate() method is really superfluous.

What I want to achieve in the new version:


public class BookingBean implements SessionBean {
@Autowired private CustomerDAO customerDAO;
@Autowired private HotelDAO hotelDAO;
public void ejbCreate() {
// use automatic injection of instances from global context
}
}


The "global context" can be something like a Spring application context, a classic JNDI naming context, or anything you want. Using reflection and a little bit of generic code, you can move the ejbCreate() to a base class.

Remaining work to do to wire a session bean to the dependencies:

  1. extend from the correct base class

  2. annotate your dependencies



Related links: