Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

August 30, 2007

Are your Java projects misusing java.text.Format?

As a consultant with ThoughtWorks I get the opportunity to see many different code bases at a variety of client. A common mistake I see again and again is that developers do not realize that sub-classes of java.text.Format (e.g. NumberFormat, SimpleDateFormat) are not thread safe. If your project defines a static instance of a formatter, like so:
public static final SimpleDateFormat SIMPLE_DATE_FORMAT 
= new SimpleDateFormat("MM-dd-yyyy");

Then chances are you have a bug.

As is the case with many threading issues, this might not be easily identified because it probably doesn't happen consistently (we are at the mercy of the Thread Scheduler).

Recently on a project I created a test to prove that using such Formats can cause problems. We then fixed the issue and decided to keep the test in the code base to ensure that no developers 'accidentally' re-introduces it. NOTE: A Continuous Integration server, CruiseControl, ensures that our tests are run continuously.

Below is a simple, self-contained, test you can use to see the issue for yourself. Remember that the test might not fail every time because it is dependent on how the threads are scheduled. I have found that the test fails with more consistency when executed a single CPU (or single-core) machine.

import junit.framework.TestCase;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ThreadSafetyTest extends TestCase {
private static final SimpleDateFormat FORMAT
= new SimpleDateFormat();

public void testSimpleDateFormat() throws InterruptedException {
for (int i = 0; i < 50; i++) {
MyRunnable[] runnables = {
new MyRunnable(new Date(-1)),
new MyRunnable(new Date(1))
};

Thread one = new Thread(runnables[0]);
Thread two = new Thread(runnables[1]);

one.start();
two.start();

one.join();
two.join();

assertFalse("Values should not be equal",
runnables[0].value.equals(runnables[1].value));
}
}

private static class MyRunnable implements Runnable {
private Date date = null;
public String value = null;

MyRunnable(Date date) {
this.date = date;
}

public void run() {
value = FORMAT.format(date);
}
}
}

October 05, 2006

ParaNamer support in Waffle as an alternative to annotations

The latest release of Waffle (0.7) can now fully support ParaNamer as an alternative to annotations. ParaNamer provides "method parameter name access for versions of Java PRIOR TO Java 7.0". ParaNamer combined with Waffle will allow you to utilize your ActionMethod parameter names directly. In the example below Waffle will use "itemId" and "quantity" to resolve the arguments for the void removeFromCart(Long, int) action method.

public class ShoppingCartAction implements Serializable {  
...

// No annotation is necessary when utilizing ParaNamer
public void removeFromCart(Long itemId, int quantity) {
cart.remove(itemId, quantity);
}
}


For complete details on this please see the documentation on Waffle ActionMethods.

October 04, 2006

Describing Waffle with one picture

The Waffle team has been busy over the last few months adding features and simplifying the overall design. Much of the documentation has been updated to reflect these changes. Many times however, it is easier to describe something with a simple drawing or a picture. So have a look at the following to get a clear idea of what Waffle provides:

April 19, 2006

Waffle on TheServerSide.com

Waffle was recently posted on TheServerSide.com under the heading Another Rails-like web framework: Waffle. I was happy to see it posted and really appreciated the responses from those that are interested in Waffle.

I'm grateful for the comparison of Waffle to Ruby on Rails but Waffle is NOT a Java version of Rails. I think the only viable comparison I can make between Waffle and Ruby on Rails is:
  • convention based
  • simplicity
  • both take advantage of their respective languages
  • increased development productivity
Many, if not most, of the clients I consult at have made a significant investment in either Java or .NET (specifically C#) and for that reason alone Ruby on Rails is not a viable option. So next time you are about to begin work on a Java based web framework make sure you take a look at Waffle.

March 28, 2006

Tutorials for Waffle

I have been spending my free time updating Waffle with functionality and documentation. Two tutorials have been added to the Waffle web site which should provide a good overview of how to use Waffle and what makes it different.

The examples in the tutorials show how to use Waffle with JSP's. This was done based on feedback I have had when talking about Waffle with developers. At this point you should get latest Waffle code directly from subversion and build it with Maven 2.0.

I would appreciate any comments or feedback you could offer.

February 14, 2006

FreeMarker support in Waffle

I recently added FreeMarker support as another option for creating Views in Waffle. FreeMarker is very similar to Velocity but seems to be more powerful. One of the nice features of FreeMarker is that it will point out the errors in your scripts. Velocity basically ignores any problems you may have, while FreeMarker displays an error message directly in your html that pin-points (line and column number) where the problems is located. Additionally FreeMarker actually allows you to reference JSP tag libraries within your FreeMarker scripts. That should allows for some nice flexibility. Take a look at the FreeMarker Servlet Documentation for further details. And take look at Waffle to see how easy it is to create web frameworks when you don't have to learn a proprietary XML syntax or new UI language.

Next task is to document how to create Waffle application with JavaServer Pages (JSP).

January 12, 2006

Mmmm Waffles...

It's been a while since I posted a blog, but I have been busy working on a new open source project. A new web framework for use in java/j2ee web applications.

Yes, I know there are already a plethora of java web frameworks, but Waffle (which is the name of the web framework) is different. Honest.

I wanted to work with a web framework that supports POJO based Actions, Dependency Injection, and it has no dependency on XML configuration files. Applications built on Waffle will be easier to develop and test with than other java web frameworks. Plus, it's buzz word compliant already fitting nicely with AJAX. :-)

Please have a look at the documentation and send me feedback on any suggestion you may have for improving the documentation or the framework itself.