Slashdot Mirror


Building Java Enterprise Applications, Volume I

David Kennedy writes: "This is a review of Brett McLaughlin's new O'Reilly title, Building Java Enterprise Applications. Volume 1: Architecture, subtitled Designing with EJBs, Databases, and Directory Servers." Read David's in-depth review, below. Building Java Enterprise Applications, Volume I: Architecture author Brett McLaughlin pages 300 publisher O'Reilly rating 9 reviewer David Kennedy ISBN 0596001231 summary Practical guide, with examples, for building a J2EE application from scratch.

Summary: Building Java Enterprise Applications is an excellent book, and ought to be on the bookshelf of every J2EE programmer working on the mid- and back-tier. If you are like me, then you then have a series of books on various parts of the J2EE alphabet soup -- a few on EJB/JNDIs, one on JMS, one on RMI, one on JDBC, a database/SQL primer, a J2EE patterns book (I recommend Depur et al. by the way), maybe even some hyped-up case studies from Sun's press etc -- but nothing on how to design an entire J2EE application from scratch. There is nothing scarier than a blank piece of paper at the beginning of a project -- this book provides a combination of a tutorial and worked example, along with an insight into the thought processes of the designer.

There are not enough books of this type for the J2EE platform; the emphasis on tying together disparate technologies to build a coherent system is exactly what I need at this stage of my career, and I found the author's constant revisions and tweaking of his design fascinating and reassuring. I'm going to pre-order Vols. II and III.

Check your sources.

You might recognise the author's name -- Brett McLaughlin is the author of another O'Reilly title, Java & XML*, and writes for flashline.com, IBM Developer Works, JavaWorld and others. You can either Google for these or visit the web-site newInstance. In my opinion he knows his onions, is aware of what other root vegetables are out there, and, most important, he can communicate well.

What's the book about? I'll give you a bit of the blurb first, as it's a fair description of the material:

"Java has many enterprise APIs: JNDI, EJB, JMS, JAXP, and the other XML APIs, JDBC and more. But how do you as a developer put the pieces together and build something that works? How do these components integrate with back-end servers (databases and directories) and with front-end platforms (web servers and web services)?"

"[This] is Volume I of that series; it covers the business logic and back-end of an enterprise system, including entity EJBs, JDBC, JNDI (...), and JMS. Volume II will discuss architectures for web applications; Volume III will venture into the still-uncharted territory of web services."

That's quite an ambitious series; and something of a departure in style for O'Reilly, who have built their enviable reputation by providing definitive titles on one technology at a time. This more a book on when to use a tool, and which tool to use, rather than how to use a tool. I think it's good to see O'Reilly branching out in this way, but it brings them into the preserve of other publishers. It might be interesting to see how this new type of title does.

So what is covered in detail? Let's have a detailed look at the contents:
  1. Introduction
  2. Blueprints
    This chapter outlines the case study that the author uses for the remainder of the book. This takes the form of a simple, but not trivial, financial-services tool. The blueprints are high-level sketches of the business need, the Data Layer, the Business Layer, and the Presentation Layer.
  3. Foundation
    This covers designing the data stores, databases and directory servers.
  4. Entity Basics
    Basic design patterns, coding and deploying beans.
  5. Advanced Entities
    IDs and CMP, data modeling and the nasty details.
  6. Managers
    Managers, in the facade sense, for entity beans and the LDAP directory.
  7. Completing the data layer
    Nasty details, populating the data store.
  8. Business Logic
    The facade pattern and stateful/stateless design.
  9. Messaging and packaging
    JMS on the client and server. Packaging.
  10. Beyond flexibility
    The wrap-up chapter, covers the major design points, discusses adapting the material to your own projects, and hints and what presentation layers may be added as a teaser for Vols. II and III.

As you can see there are no surprises in the contents. Once the high-level problem and solution is laid out, there's just a sensible progression through the layers. I particularly liked the practice of stopping and reviewing at regular checkpoints -- it helped tie the material together and emphasize the layering in the design.

There are some detailed appendices giving vendor specific instructions for databases, containers etc. This section also contains all the non-unique code for each layer, e.g., all the entity beans that weren't discussed in detail.

  • SQL Scripts
    Cloudscape, InstantDB, MySQL, Oracle, PostgreSQL.
  • SQL Deployment
    Ditto.
  • Directory server setup
    iPlanet, OpenLDAP.
  • Application server setup
    BEA Weblogic only.
  • Supplemental code listings
    All code also available in completed final form on the associated web-site.

Sounds wordy... It's not. This is a short book, only about 300 pages including appendices and index. (Compare that to something like Roman's classic EJB book ...) Chapter content is only 200 pages. Fully a third of the content of the book is code; this is definitely one for the programmer, those of you who delight in detailed breakdowns of requirements, user stories, schedules, etc will find little or nothing of interest here.

Equally, there is little in the way of explicit (non-coding) high-level design discussion -- all the code is evolved directly from the well-written text. This is not a bad thing at all -- the design seems sensible and straightforward, always a good sign, and mostly presents an admirable example to any young programmers watching.

All this doesn't mean you are reading a listing though. As on any project involving EJBs, there is a lot of more-of-the-same code between beans -- most of this code is concentrated in the appendices, and only the material under discussion is presented. New code is always presented in full, from package declaration to closing brace. This is refreshing and permits you to actually get something working as you read through the text, although you'll need to be prepared to set up app servers, databases etc to get maximum benefit.

Target audience? Experienced Java programmers who have started using the J2EE platform and are fairly comfortable with all the bean types, JMS, JNDI etc. This book states several times that it is not a primer on any one technology, and provides ample references to more detailed texts when appropriate.

This is very much a book for a wannabe J2EE developer who can't quite figure out how to fit the pieces together, or, like me, just has a gap in his/her skillset when it comes, to, say, LDAP.

What's good? Lots of it. Mainly, the best thing is the clear presentation of a LOT of code via a well partitioned example application (which will also be re-used in Vols II and III). The code is of good quality too, and presents several idioms that while obvious now, were unknown to me when I started EJB work... with the usual reworking-over-a-weekend later on. In particular, there are some commonsense pieces of code -- like a nested exception class for those of us still using pre-1.4 (and remember, you're tied to what your app server supports), some simple session and entity bean Adapter classes, simple Value Object classes etc. As I said, nothing earth-shatteringly novel, but it's nice to see a lot of these idioms used together to simplify the code.

Another admirable thing about the book is the handling of the detail. I've read several books which follow the practice of putting in Gotcha! box-outs, and to be honest, few of them are that useful unless you are a novice. I'm been programming for a few years now, and was amazed at the silly difficulties I've had with my first EJB project -- as a result I'm pleased to say that the box-outs indicating problem areas sound like the voice of bitter experience. For example, there is discussion on following the correct style for accessors/mutators under CMP (getId works, getID cheerfully fails), advice on the very fixed order in the deployment descriptor XML, problems with case-sensitive searches in JNDI, etc. Those of you who've worked with, particularly, EJB1.0/1.1, will undoubtedly have groaned as you realised the problem de jour was something simple-but-outside-your-code like those examples.

Admittedly it's not my area really, but I also found the whole treatment of directory servers very clear and useful. For the first time I understood (a) how they work (b) when they complement databases (c) how to use them easily from my code. Again, I admire the level of detail achieved without being confusing -- I don't see many introductory books include things like the default port number for directory servers using SSL (636 - well, I didn't know that!).

What's bad?

Not much. By nature of the book it doesn't go into huge detail on all technologies used -- there were a few areas where I wanted more. In particular I would have liked to have seen more on testing; now that XP is pretty much mainstream, no one can deny that unit testing is vital on production projects. (When I started using EJBs I had to kludge together a nasty version of JUnit which fitted into the sub-optimal build and client-server framework we were using. I've since found that there are better ways to test EJB layers, but I can still only think of one book, by Richard Hightower, which walks you through examples.) Although the build files in the example use Ant, which makes JUnit and other tools very easy to integrate, there is no mention of unit testing. This is a pity.

The only other things that caught my eye were minor -- coding style in particular. The coding style in the book is very straightforward and Sun-standard, but I have to admit that I'd have liked more JavaDoc'ed code. The code on the website is much more fully commented. I understand that printing this means more paper, and thus a thicker and more expensive book, but on some of the custom methods it would have clarified things for me.

In particular, and I'm being picky here, I didn't find that the authors practice on handling nulls and errors fitted with my own -- admittedly I don't so much practice "defensive coding" as "paranoid coding." Most methods were not null safe, and that can be a nightmare to debug in an n-tier system. Also, he took the line of returning null to indicate failure or error. I understand it's a valid design decision -- my experience says to go with more explicit errors in a larger project, and I would have liked a page or two on the choices here.

Another area where I feel there is room for improvement in the presented style is in the use of hard-coded Strings for lookups - for example, in the AccountManager object there are several lookups of the AccountHome, e.g.:

    AccountHome accountHome = (AccountHome)context.lookup(

        "java:conp/env/ejb/AccountHome";  // Whoops, finding this can be tough!
From experience code reviewing EJB based projects, it's going to save a lot of pain looking for typos if this repeated hard-coded String is (a) extracted as a constant so it can only be mistyped in one place and (b) refactored into a lookup method. It's a fairly minor point, but useful to do right from the start on an EJB project and worth pointing out to someone starting their first one. (Mis-typed meta-data like this is a bit of a weakness in the J2EE framework in my opinion - I always feel that I'd save a lot of time if the compiler or some J2EE aware verifier could check over those Strings to see if they match anything else in the build... I've used vendor tools which claimed to do so, but as they didn't even check that methods/lookup names were in the bean source I wasn't sure what was being verified!)

One last thing: I know it's minor, but why the insistence on importing explicitly? I feel it makes maintenance more difficult -- change one LinkedList to an ArrayList and you're off fiddly with minor imports again. I also didn't find this:

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
as appropriate for a printed book as this:
import javax.jms.*;
It would have been nice to trade those 10 lines wasted for some custom JavaDoc. However, all told there is remarkably little to grumble about in this book -- I couldn't even spot the obligatory editorial mistake. (That really annoyed me.)

Alternate titles?

Can't think of a good one. (Either a sign that this book is one you might want to look at or else so completely specialised as to be of use to only one person in the world, and that person is probably the author. Luckily, I think it's the former in this case.)

It is however worth a trip to the bookstore for companion, as opposed to alternate, titles before reading this - it assumes detailed knowledge of several J2EE areas, but provides suggested (O'Reilly) titles for reference.

Sounds good -- but what do you know anyway?

Time for the disclaimers. Some material in the book I found useful because I lack experience -- in particular, some database and LDAP stuff.

However, 5 years of getting paid to play^H^H^H^Hcode, and a personal reference library of some 120+ books has made it easier to spot the rare decent title! Most of my J2EE books are from my experience of EJBs for the last year or two, so I know what mistakes are easy to make, as I've made 'em. [I'm actually catching up on my reading, and hence reviewing, due to the Great Telecomms Downturn finally affecting me - anyone want a J2EE developer? :-) ] Finally, I paid for this book (which isn't the case for some of my other reviews).

You can purchase Building Java Enterprise Applications, Volume I from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

(* Bonus mini-review: a useful book, but not easy reading, I found it hard to slog through, but managed my first XLST work in about 10 minutes using it.)

7 of 154 comments (clear)

  1. I prefer that style of import statement by infinii · · Score: 2, Insightful

    For the person who is learning, it's beneficial to see exactly which package a particular class belongs to. Obviously in this code example it didn't matter but if you were dealing with 4 different packages all added using a *, then one might be hard pressed to find a class.

    Considering it's a book on Java, being specific suits it's audience. The author isn't worried about code maintenance issues here.

  2. Book Code != Professional Code by ChimChim · · Score: 5, Insightful
    When I was growing up, and wanted to learn how to program, I did it mainly through books I could buy each month at the local bookstore. So it took me awhile to figure out why real code didn't look like it did in books. Here's my answer to soem fo the author's complaints:

    Another area where I feel there is room for improvement in the presented style is in the use of hard-coded Strings for lookups - for example, in the AccountManager object there are several lookups of the AccountHome, e.g.: AccountHome accountHome = (AccountHome)context.lookup( "java:conp/env/ejb/AccountHome"; // Whoops, finding this can be tough!
    Yeah, but reading it is easier, especially if you're skimming through or haven't read a particular chapter where they introduce all their constants. Obviously, good practice differs.

    One last thing: I know it's minor, but why the insistence on importing explicitly?
    So that readers can see the exact dependencies of the class you're writing. In fact, I often write imports explicitly in production code as a form of documentation, but usually not for packages like java.io or packages where nearly every class is needed.

    I didn't find that the authors practice on handling nulls and errors fitted with my own
    Handling nulls is crucial, but it also clutters the example and makes it longer (somewhat). I would look at it (if they'd included it) and known they were being defensive, but I could see another reader possibly mistaking it for a special case? But i assume page length was [probably the main reason.

    Books can strive to give you as much code as possible, but really, you shouldn't use it (and many books have such disclaimers). You're not buying code, you're buying the ideas behind it, so code should be explanatory and descriptive rather than production ready. Some books come with utility libraries or include code from popular, well-used libraries, but even then never cut and paste book code into a production project!
  3. Re:Wha? by ashultz · · Score: 2, Insightful


    I like two tier applications too... it seems that often people put in a middle tier just to have 3. You can often combine the logic and the webserver on the same box (running Apache + a JSP engine, for example). Unless you've got weird load issues, forcing another level of boxen isn't useful, and slows things down.

    But then it's not "3 tier" and therefore must not be scalable. Or something.

  4. Re:Wha? by FatherOfONe · · Score: 2, Insightful

    Wow what you said sounds great! No more SQL. CMP Entity beans handles all my SQL issues...

    Oh wait. I have to learn ANOTHER language. It is like SQL but yet not the same. Oh and I CAN'T have but simple relationships between tables that map to ONE bean! I want 10 or so tables to map to a "customer" bean.

    Oh well I guess that I will just manage the persistance myself... OH MY GOD! trying to manage all the lifecycle stuff is a pain and weird stuff happens if you don't design it correct, and you try and scale it....

    Give most of us SQL any day of the week. It is a pain, but at least you know it will work, and your data guys can help tune it. Also you don't have to worry about a lazy coder using instance variables and not cleaning them up with a bean managed persistant bean.

    I don't want to claim to be some EJB hater or EJB God, but I do know that for 99% of the people out there a good JSP/Servlet engine is all they need.

    I will say that if you want to get over 100 transactions a second (normal transactions 30 inserts/updates and 70 selects) EJB's appear to be the only way to get Java to scale.

    They are by no means a silver bullet. The learning curve is steep, and they are difficult to test well.

    --
    The more I learn about science, the more my faith in God increases.
  5. Re:Fundamental flaw of most J2EE apps by smagoun · · Score: 3, Insightful
    I prefer to store UI field info in tables (AKA "data dictionary" or "field dictionary".) These tables then generate the UI's as needed.

    IMO, code generators are evil. You are going from a compact representation of attributes to a less compact representation (code). IOW, bad information factoring. It is like entering and management spreadsheet data via XML instead of a grid.

    Um. So is it good or bad to generate stuff? You can't have it both ways. Your compiler is a code generator, like it or not. You go from a compact representation of information (C, Java, whatever) to a less-compact version (asm/0s and 1s) that the computer can understand.

    Furthermore, the compact representation of the information that's provided as input to the generator is the goal! It sounds like we agree - compact good, big piles of code bad. The code generator lets you make one tidy little file - your data definition - that the generator then blows out into the various APIs that other components need. You still have the piles of code, but you don't have to think/worry about them, because they're always there and they always "just work.".

    Sure, it would be nice if we didn't need to generate any code and we could just operate on the data itself, but there are real-world considerations that make that difficult. RDBMSes talk SQL, browsers talk HTML, and there's a bunch of stuff in between. You still have to get from the UI to the DB, and you can either write that code yourself, or have the computer do it for you. By defining the data and then transforming the data definition (via code generation or other method), you can have your cake and eat it.

    (And yes, I hate getters/setters too, but they're only about 1% of the stuff you can actually generate)

  6. Re:Fundamental flaw of most J2EE apps by Fnkmaster · · Score: 3, Insightful
    You make some good points - smagoun's methodology is a solution to a problem that doesn't have to exist, but in practice, it does exist in most companies and enterprise software being built today.


    Also, your solution isn't quite well fleshed out enough for me to buy into whole hog. A pure meta-data model (sorry, I know that's not precise terminology, but I think I am using it in the same way you are using the term data dictionary) is well suited to data that you only need to take from a form field, stick into a particular DB table and field, and retrieve and display later.


    If your application needs any particular logic to be based off of some fields, metadata models fail - you need semantic knowledge of field's meaning and associations in the application code.


    Unfortunately, many enterprise applications have a lot of data they just need to shuffle around and pass along to somebody else farther downstream, and a lot of data they actually need for application manipulation. In my experience, if you try to handle complex data types with a data dictionary-style mechanism, your dictionary ends up with so many damned parameters it becomes unmanageable. If the data is handled in code, you will just have more code for complex data, and less code for passthrough or simple data.


    Also, I should point out that in general, code generators keep data in MORE COMPACT form, not less compact - I believe that smagoun is referring to a build system that uses generated source code files as intermediates in the build process, not as verbose files that are to be edited manually. The advantage this provides is that you can get compiled code performance out of data manipulations that would otherwise require semi-interpreted performance if they actions are all determined at runtime (example - autogenerated externalization methods in Java vs. default object serialization - huge performance difference in performance critical apps).

  7. Re:Somewhat OT, ignorant question by Tablizer · · Score: 2, Insightful

    Java does well on the server simply because it is the best tool for that job. End of story. It is also much easier to maintain and upgrade than garbled PERL or old C hacks.

    Some choice we have here: Java OR C OR Perl. Whatta diverse world. That it?

    You know, personally I also think that Perl sucks. But Perl programmers seem to do wonderful things with it and can often even read and maintain other perler's code.

    As long as they are productive, I have no place to bash Perl just because it ain't my bag. (Oh, I'll still complain though :-)

    I think programming and paradigms is probably subjective. If you want productivity, then give people tools that best fit their head. Surveys by Ed Yourden tend to confirm this (although it is highly preliminary).

    If you can show that Java and/or OOP are *objectively* better, then I would just loooove to see the actual evidence. Until then, please don't dictate to me what works best for me. Give me tools that fit my head well, and I can crank sh_t out faster than a vast majority of Java fans (and have it be more maintainable and simpler).

    Probably the same with Perl fans. Paul Graham beat a lot of projects using LISP, a language created in the 1950's, and is now wealthy because of it, if you want an actual example of tool-fit-based productivity.