Adam Bien's Weblog   

Friday Feb 22, 2008

State Of The "Patterns For Java EE 5 Project"

The p4j5 project is doing well. There are already fifty registered observers, 2 developers and a sample application "Training Database", which was built regarding to some more "esotheric" patterns and ideas (like Persistent Domain Object, Fluent Interface with "Extended" Persistence Context). I use the application by myself to track my progress in sports (running + biking). The architecture of the app is discussed in the current, and upcoming, issues of the JavaSpektrum magazine.

This app can be checked out from SVN (from p4j5) or directly used from http://www.adam-bien.com/tdb/. There are already about one hundred registered users. However the connection to my Glassfish v2 instance is very slow from outside - so be patient.

Beyond that, in the repository are already about sixty Netbeans 6.0 sample projects, which are directly deployable (and tested) to Glassfish v2.

I will discss some of the patterns during the workshop in frankfurt next week. The theory behind the sample code is explained in detail in my (German) book "Java EE 5 Architectures" ...but who cares about German books? :-).


[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]

 Subscribe in a reader

Tuesday Feb 19, 2008

Speed Hacking with Java EE 5,Best Practices and Patterns

Java EE 5 can be really efficient - you just have to get rid of 50 superfluos layers, and abstractions :-). During the "Entwicklertage" workshop in Frankfurt, I will introduce some lean patterns and best practices and we will build together with the students a fully functional Java EE 5 application - in very green filed fashion. We will even install the whole infrastructure (JDK, IDE, server) during the workshop - so it will be nearly "real world".  ...best critics, questions and heretical statements about Java EE 5 will be rewarded with a t-shirt :-).

Beyond that I will disect the Run And Bike application to explain some presentation patterns as well.


[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]

 Subscribe in a reader

Monday Feb 18, 2008

Superb compilation of JVM parameters - from 1.3.1 to 1.6

From JDK 1.3.1 to 1.6. All major (about one hundred) JVM settings with explanation can be found here. Thanks to Michael Bien for the hint.


[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]

 Subscribe in a reader

Sunday Feb 17, 2008

The X-Files (actually -Xmx files), reloaded.

I got interesting feedback for my last post about the -Xmx and windows issues, with many links to external resources. First of all,  32bit applications on windows are limited to 2GB RAM. This limit can be raised to 3GB, however some settings at OS-level are required. This is just a fact, and has nothing to do with Java. This article covers the issues and settings in great detail.

Some JVMs, like JRockit, allow more heap, than 1.6GB - 1.85GB, or 2.85GB in particular, however the memory is partitioned (fragmented) in this case. So the performance of the allocation, as well as garbage collection, could degrade. The "trick" with /3GB switch is also mentioned for the IBM JVM.

The -Xmx switch is somehow dangerous as well. Too high setting causes the JVM to "crash" (actually not to start), with the following message:

"Invalid maximum heap size: -Xmx4g
The specified size exceeds the maximum representable size.
Could not create the Java virtual machine
."

So the proper -Xmx configuration is a crucial for all Rich Internet Applications, which are installed or deployed vie WebStart etc. In this case the lowest common denominator is crucial... On most machines there should be no problem with configuration below -Xmx1.4g. Fine tuning could be dangerous, especially if you are responsible for user support :-). On the server the issue is less a problem - the "try and error" strategy works well. However only in view companies I know the servers run on windows...:-)


[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]

 Subscribe in a reader

Thursday Feb 14, 2008

The Mystery of the Maximal -Xmx Settings, Ergonomics and Tuning

From time to time, especially developers working with windows, run into problems with JVMs maximum heap. Although in theory the maximum heap on 32bit OS should be 4 GB, it is actually on windows less than a half of the theoretical setting. The actual number is around 1.4GB, it is hard to reach 1.5GB. I found several resources, which point to the causes of this behavior. This older entry, explains the reasons (windows systems reserve some memory for "private use", which is no more available for applications). It contains some links to MSDN as well. This bug report, explains the behavior, as a problem in the JDK's dll. It mentions also, that other JVMs (like JRockit) are able to handle slightly more maximum heap. However on my Vista 32bit machine (3GB RAM), the highest heap I was able to set up was 1626m (it isn't able to start with 1628m...sometimes with 1629m it varies,) with Sun's JVM. I tested it with the SwingSet, the Swing demo application: java -Xmx1620m -jar SwingSet2.jar

If you are interested in GC/JVM tuning, this whitepaper is a good resource. It explains the default settings, tuning ideas and ergonomics in great detail.


[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]

 Subscribe in a reader

Sunday Feb 10, 2008

Is it worth to use POJOs instead of EJB 3 in terms of performance? (with results, source and load scripts)

I hear from time to time funny statements like "EJB 3 are too heavyweight", or "POJOs are lightweight". If I have the chance I ask directly how the term "lightweight" is defined - and get really funny (mostly inconsistent) answers. This remembers me somehow the answers I get for questions like: "What you actually meant by serviceorientation?" or even better "What do you mean by Web 2.0?".

Lastly I was asked "...It would be interesting to see if the same web app would be faster if you only developed a web-project using JSF, Derby and TomCat as Webserver..." (instead of Glassfish with EJB 3, JSF and Derby). This request made me really curious - and I built two independent projects. An EAR "EJBLoadTest" which consists of an Servlet and to cascaded EJB 3.

public class NumberGenerator extends HttpServlet {
    @EJB
    private NumberGeneratorFacadeLocal numberGeneratorFacadeBean;
  
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println(numberGeneratorFacadeBean.getNumber());
        } finally {
            out.close();
        }
    }
 }

 With two local Session Beans. The first one fetches the millis, calls the second one and computes the result. The business logic is dumb, but it avoids GC optimizations.

@Local
public interface NumberGeneratorFacadeLocal {

    public long getNumber();
   
}

@Stateless
public class NumberGeneratorFacadeBean implements NumberGeneratorFacadeLocal {
  @EJB
  private MillisProviderLocal millisProviderBean;
   
  public long getNumber(){
      return System.nanoTime() - millisProviderBean.getMillis();
  }
 
}

The second bean is simple as well.  

package com.abien.loadtest.ejb;

import javax.ejb.Stateless;

@Local
public interface MillisProviderLocal {

    public long getMillis();
   
}

@Stateless
public class MillisProviderBean implements MillisProviderLocal {
   
    public long getMillis(){
        return System.currentTimeMillis();
    }
}
 

I introduced the two beans, to maximize the overhead between the "POJOs" and EJB3. In simple applications there would be only one layer - and so less overhead.

The POJO application is identical. Instead of EJBs I used POJOs, and instead of an EAR, a WAR.

The POJO servlet comes with an additional "init" method. Because dependency injection doesn't work for POJOs (=classes without the @EJB annotation), the POJO creation was factored out in a init method:

public class NumberGenerator extends HttpServlet {
   private NumberGeneratorFacadeLocal facade;
  
    public void init(){
        this.facade = new NumberGeneratorFacadeBean();
    }

The same story in the facade -  the DI was replaced with an constructor call:

public class NumberGeneratorFacadeBean implements NumberGeneratorFacadeLocal {
  private MillisProviderLocal millisProviderBean;
 
  public NumberGeneratorFacadeBean(){
      this.millisProviderBean = new MillisProviderBean();
  }
   
  public long getNumber(){
      return System.nanoTime() - millisProviderBean.getMillis();
  }
 }

 

For the load tests I used JMeter. I repeated the tests several times. The load generator and the Glassfish v2 were on the same machine. This differs a little bit from real world - but is perfectly suitable to show the EJB 3 overhead.

The results: 

  • EJB3: The throughput of the EJB 3 solution was 2391 transactions/second. The slowest method call took 7 milliseconds. The everage wasn't measurable. Please keep in mind that in every request two session beans were involved - so the overhead is doubled.
  • POJO: The throughput of the POJO solution was 2562 requests/second (request - there are no transactions here). The slowest method call took 10 ms.

The difference is 171 requests / seconds, or 6.6% (therefore 3.3% for a single session bean).

Conclusion:

The dfference is less than expected (I expected an overhead over 10%)... However the POJO sample only works for idempotent / transient use cases. It wouldn't work for highly concurrent, database applications -  I only created one instance for servlet - not request.... In that case, you will have to synchronize the access to the shared state e.g. with transactions or "instances per request". In that case a plain web container wouldn't be the simplest solution.

However the EJB 3 solution would work even with database and JMS without modification... The funny story here - the POJO solution required some more lines of code. :-).

I checked the projects (POJOLoadTest, EJB3LoadTest), the load script, as well as the results (as screenshots) into http://p4j5.dev.java.net. I used plain Netbeans 6.0 for this purpose - feel free to reexecute the load test again - and share the results :-).

 


[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]

 Subscribe in a reader

Saturday Feb 09, 2008

JSF Matrix - most popular JSF/AJAX frameworks condensed

This matrix is really useful. It compares many (18) different JSF frameworks. Most of them come with a link to demos, documentation and license information. [Thanks to aquarium for this hint]


[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]

 Subscribe in a reader

Friday Feb 08, 2008

Nice, but hidden, CRUD wizard in Netbeans 6.0

I explored a nice CRUD wizard, which is able to generate a Session Bean from existing JPA entities. I'm not sure, whether this functionality was already existent before Netbeans 6.0.  The wizard is available in the context of an EJB project. You can access the wizard from the menu "New File" (Strg+N), then navigate to the folder "persistence", and choose "Session Beans For Entity Classes". At least a single JPA entity should exist in your project...

The generated code is almost perfect. E.g. I generated a CRUD Session Bean from the following Entity "Bike":

@Entity
@Table(name = "BIKE")
public class Bike{
    @Id
    private String name;
    private String description;
    private int expectedkilometers;

//...some code omitted.

 The generated Session Bean looks like: 

@Stateless
public class BikeFacade implements BikeFacadeLocal {
    @PersistenceContext
    private EntityManager em;

    public void create(Bike bike) {
        em.persist(bike);
    }

    public void edit(Bike bike) {
        em.merge(bike);
    }

    public void remove(Bike bike) {
        em.remove(em.merge(bike));
    }

    public Bike find(Object id) {
        return em.find(com.abien.wizardsample.Bike.class, id);
    }

    public List<Bike> findAll() {
        return em.createQuery("select object(o) from Bike as o").getResultList();
    }
}

The coresponding Local interface is clean and simple as well:

@Local
public interface BikeFacadeLocal {

    void create(Bike bike);

    void edit(Bike bike);

    void remove(Bike bike);

    Bike find(Object id);

    List<Bike> findAll();

}

As mentioned, the code is o.k, but there is still room some for improvement:

  1. Inside the findAll method a simple query and not a NamedQuery was used. This hits not only the performance, but is harder to maintain. The declaration, or reuse of an query at the JPA entity would be much better (but much harder to implement :-)).
  2. The naming of the method "edit" is strange a - "update" or even "merge" would be better.
  3. The @Local annotation could be moved from the interface, to the Session Bean - then the interface would be clean and independent from the EJB 3 API.
  4. Actually a Unit Test could be generated as well :-).
I wondered in a Java EE 5 training, why a student can accomplish his exercise so fast - he used this wizard :-).


[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]

 Subscribe in a reader

Wednesday Feb 06, 2008

EJB 3, JPA, JSF and Patterns - sample application online - already 75 registered users

The  (Training Data Base, or "Run And Bike") is an application which manages your training data (sports, not Java trainings :-)). The essential use case is the corelation between the average running/biking speed and your average pulse. I started with sports and was just curious about my progress. Because of lack of time I used Netbeans 6.0 with visual JSF and Glassfish v2 with Java DB. The first version was fully functional, but I wasn't really satisfied with the maintainability and quality of code. Especially the separation between the generated and implemented code was not good enough. I rewrote the whole application during a weekend using the Domain Driven approach (or just objectorientation) and some presentation view patterns like ("Passive View"/"Supervising Controller").

The API to the JPA entities and Session Beans looks like (an excerpt from a Unit Test):

            training.login(userName,password).
            running().
            today().
            averagePulse(135).
            comment("ok").
            duration("1:05:01").
            maxPulse(165).
            distance(12).
            wasRace().
            weather(SUN).
            add();
 

Only in few days (actually in three) about 75 new users registered. I got even one contributor/committer. The Training Data Base is a part of the "Patterns For Java EE 5" java.net project (built with Netbeans 5.5/6 and tested with Glassfish v2). The whole sourcecode is maintained in the Subversion repository, I install the bits from time to time on my Glassfish v2 server (in general once a week - it's always beta :-)). E.g. this week I implemented RSS feeds with most eager runners. The goal of the project should is fun + an opportunity to try out interesting things. I will provide a charting component in the next time, and excel (or openoffice of course :-)) export as well.

The whole architecture will be explained in detail in the upcoming JavaSpektrum issues (German Java Magazine). The first part will concentrate on the presentation, the second more on the business tier... I will explain the whole application in great detail during the "Entwicklertage" workshop as well.


[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]

 Subscribe in a reader

Tuesday Feb 05, 2008

Eclipse's Resource Perspective and the Netbeans 6 answer

I always missed  Eclipse's resource perspective in Netbeans. In fact it was the only remaining functionality, which forced me to use Eclipse from time to time. Especially the management of CVS, or Subversion repositories (like e.g. updates of the p4j5.dev.java.net or greenfire.dev.java.net sites) can be easily established with Eclipse. Eclipse, in the contrary to Netbeans, is able to treat every folder as a project, which allows the check-in/out of any arbitrary content - which is perfect for the management of HTML content. I used this functionality to manage the documentation (doc, pdf etc.) in my projects as well. I complained about this fact at the last JUGM/NUGM meeting. An attendee responded: "Do you know Favorites?"

Indeed the Favorites window (Strg+3) is not only able to manage any arbitrary content, but use the Subversion/CVS functionality as well. Beyond that, you can even include any folders / projects to your Favorites. It is decoupled from the e.g. project view - what I really like. Eclipse's resource perspective is "just" another, raw view for the existing projects. Netbeans Favorites is more: it allows you to browser in the filesystem, open projects (which will appear in the Project window then) and synchronize your folders with a remote repository.

 Even the description from Netbeans Help matches exactly my needs:

"The Favorites window enables you to access any file on your computer or network without having to create a project for the file. You can open the Favorites window by choosing Window > Favorites (Ctrl-3).
When you first open the Favorites window, it only contains your computer's home directory. You can add any folder to the Favorites window by right-clicking in the window and choosing Add to Favorites. If the folder is registered as a VCS working directory in the Versioning Manager, VCS commands are also available for the folder.
Because the Favorites window does not know anything about what projects a given file belongs to, none of the regular project commands like Run File and Compile File are available in the Favorites window. You can run an Ant script from the Favorites window as you would anywhere else in the IDE." 


[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]

 Subscribe in a reader

About,RSS / Atom

About me: www.adam-bien.com
JavaONE 2008 Interview
My Recent Books
Adverts
Search
Links
greenfire.dev.java.net
...the last 150 posts
my.netbeans.org
Visitors
License