Java


Timing JSF Requests Using a Phase Listener

One easy way of determining how long it takes to return a page from Seam, and/or JSF is to use a phase listener. This phase listener below logs the end of each phase of the JSF lifecycle and measures the time from the start of the RESTORE_VIEW phase (the first phase in the lifecycle and the start of the request) to the end of the RENDER_RESPONSE phase which is the last one. Logging the end of each stage of the cycle lets you see what else is going on during each phase of the cycle.

public class LogPhaseListener implements PhaseListener {

	public long startTime;

	private static final LogProvider log = Logging
			.getLogProvider(LogPhaseListener.class);

	public void afterPhase(PhaseEvent event) {
		if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {
			long endTime = System.nanoTime();
			long diffMs = (long) ((endTime - startTime) * 0.000001);
			if (log.isDebugEnabled()) {
				log.debug("Execution Time = " + diffMs + "ms");
			}
		}
		if (log.isDebugEnabled()) {
			log.debug("Executed Phase " + event.getPhaseId());
		}
	}

	public void beforePhase(PhaseEvent event) {

		if (event.getPhaseId() == PhaseId.RESTORE_VIEW) {
			startTime = System.nanoTime();
		}
	}

	public PhaseId getPhaseId() {
		return PhaseId.ANY_PHASE;
	}

}

To use this, simply add the class to your project and insert a new phase listener in the faces-config.xml file.

  <lifecycle>
    <phase-listener>package.name.LogPhaseListener</phase-listener>
  </lifecycle>

While it may not be totally accurate, it at least gives you an idea of the scale of the duration of a request (i.e. 68ms versus 394ms). I’ve used this fairly effectively in a few projects to cut out some bottlenecks as well as comparing and contrasting different JSF frameworks.

Creating A Spring Web Flow JSF Project From Scratch

(Updated – 9 August 2010 – This was written in my pre-Maven days and after a few requests for working source, I’ve built the same project using Maven which can be downloaded. Just unzip the maven project, go to the directory in the command line and type mvn jetty:run to start the server and deploy the project. Navigate to http://localhost:8080/swfproject/home.jsf or http://localhost:8080/swfproject/spring/testFlow to see the pages demonstrated in the tutorial.

I recently had to start another project using Spring Web Flow and found myself banging my head against a brick wall to get the web flow stuff set up and to request the page properly. As a result, I decided to write up my results as a quick how-to for other developers should they find themselves in the same situation and also as a reference for myself the next time I need to start a Spring Web Flow project using Spring Faces from scratch.This article is meant more of a “here’s-how” as opposed to a “how-to” or an “explain-why” so we’ll move at a quick pace with little explanation.
Read More »

Spring vs Seam Part 4 – Coming Soon (honest)

I know it has been a while since I pushed out my last piece on Spring Web Flow vs Seam, but I am still trying to get it ready. With Christmas, software releases at work leading to a hectic work schedule, and other stuff I have limited time. Furthermore, I am still re-writing parts of it, and I end up digressing into related info that is not central to the piece. Also, I am still having difficulty accurately writing up the final conclusion in a fair, and informed manner.
However, hopefully, I will get it completed in the next week here and I can get it released. I may also have some additional material for blog posts.

In defence of hidden fields with Seam

In this post about pagination with Seam entity queries, I outline a method to encapsulate the pagination mechanism in a facelet that internally uses hidden fields to hold the state. Alexander R?hl pointed out that Dan Allen in his book Seam In Action recommends avoiding hidden fields in favor of page parameters lest they become a tangled mess. It’s a fair point, and one that probably deserves a better answer than a brief post comment. Read More »

Busy Times

Things have been very busy recently as Fall/Autumn usually is. October is the month of Birthdays, Halloween, visiting relatives and plenty of leaf clearing. November was the same except it had Thanksgiving and a bad cold and a trip to the Emergency Room thrown in for good measure. Also, I’ve been trying to complete my piece comparing the Seam and Spring Web Flow (SWF) frameworks, as well as finalizing my JTexture procedural texture library, and writing a Depthcharge clone in Java, all while trying to read the books I got for my birthday in October. Hopefully, the fruits of my labor will materialize here over the next few weeks or so.

My Seam vs. Spring Web Flow article has been almost complete for a while pending one or two final readings. It’s currently at 70+ pages in the PDF version and I’m down to a final read through before I put them out there. I’m hoping to put them out over the next week or so as time permits. I’ve also been getting some experience with docBook which,
while it is a neat tool, it can difficult to wrangle with.

I’ve also been reading Effective Java (2nd Edition) by Joshua Bloch which is a cracking good read. Sometimes it’s easy to skip over the basics of a language, so it’s good to go back and really look at them with a professional eye and learn some really good tips that can have an effect throughout your code. For example, my procedural texture mapping framework will be undergoing some changes based on ideas I have read about in Effective Java, particularly Item 2, using a builder class instead of a complex constructor. Some of the texture classes have many parameters so I’ll switch over to something more manageable than 8 parameter constructors.

As Christmas is nearly upon us, I have shifted into my retro computing mode, where I start reminiscing about the days of computing long gone so I decided to write a quick game. I’m nearly finished writing a Depthcharge clone, and decided to make it a learning exercise. Rather than plan every detail out beforehand, I thought I would play it all by ear and see how easy it would be to refactor at a later date when I discovered my design mistakes. At some point I’ll document my findings.

Anyway, I’ll hopefully be getting a few posts up between now and the end of the year. I have some more projects I want to start looking at once I complete my current ones.

Java Standards Redux – Spring Licensing Fallout

In this post I talked about how the Java standards are important to ensuring java has a long and fruitful life. This post references Spring as an example but was written before the fallout from the Spring licensing issues. This fallout tends to back up the argument that standards are important, although since there were no standards at the time for Spring to follow, it is not a totally valid position.
Over the years, many people have been knocked for claiming that Spring is a proprietary framework. How can it be when it is open source would often be the argument. Proprietary in its literal sense means that it has one owner or controller which is true for Spring for the most part. A less formal definition when discussing programming, Java in particular, is that it does not follow any open standards. Spring broke a lot of new ground which in many cases, meant that there were no standards or only bad standards to follow. However, a number of developers also bought into using other Spring features like the Templates to aid them in their development.
Now that Spring has penetrated developers code to a large degree, vendor lock in was in place for many companies with large spring based projects and Rod and Co turned off the free beer once everyone was hooked. Again, it’s their business, their platform, and they can do with it as they like.
However, if the Java platform had a set of good standards for defining framework functions, then nobody would seriously bother with a non-standard framework, and nobody would be able to shut off the tap because they could be replaced in an instant by an alternative implementation of the standard. Again, this reinforces the need for good standards (like EJB3.1 appears to be) and also for timely updates to the standards.

Neal Gafter joins Microsoft

Neal Gafter has left Google to join Microsoft working on it’s dot net platform with Anders Hejlsberg. Oddly enough Anders was the father of Borland Delphi which I’ve used for over 13 years, who left Borland to go create C# and dot net. Now when I switch to java, one of the most well known Java pioneers moves to Microsoft to go work with him.