Sunday, February 26, 2012

Book Review: EJB 3.1 Cookbook

I had the great opportunity to review the EJB 3.1 Cookbook by Richard M. Reese from Packt Publishing. The book provides a number of simple "recipes" for getting specific tasks accomplished in a compact and simple way. I thoroughly enjoyed reading the book. Admittedly it much easier to read a cookbook than some other technical content.

The content is divided into 12 chapters with numerous recipes per chapter. The topics range from an introduction to EJBs to web services. Each section generally has six to eight recipes. Each recipe is easily digestible with a minimum amount of EJB knowledge to start with. Keep in mind, this book will be easy to use if you have a minimum level of experience. Some recipes are sufficiently simple enough for a beginner.

The book will guide a novice to become a practitioner though expertly done examples. Advanced topics are covered in a way to make them seem simple. The practitioner will begin to become an expert.

As a general rule, I would not recommend anyone to read a cookbook from cover to cover. This applies to a "real" cookbook as well. However, if you are trying to get a deeper understanding of the EJB as a tool, then reading the book will at least add these recipes to your toolbox to draw upon when needed.

There are a number of important facts that a developer should keep in mind. These kernels of truth are what makes the difference in a book, and a reference. This manuscript is filled with a number.

In Chapter 2, there is a very succinct explanation of write locks.

A write lock does not permit concurrent access to a method. Once a client begins executing a method marked with a write lock, no other clients are permitted access to the method until the method invocation completes. Other clients are blocked. This is the default concurrent behavior of singletons.
This is a very important default behavior and should be kept in the developers mind when working with singletons. This default behavior means that you don't have to synchronize within singleton methods unless you change this default behavior. This is a great example of what you can find in this book.

Chapter 4 on EJB persistence includes a recipe on validating null and temporal fields. The @Null annotation is mentioned which validates that a field is null. I am not sure I am convinced of the usefulness of this particular annotation, as opposed to the @NotNull annotation which has more direct usefulness. The @Future and @Past annotations are really helpful. These simple annotations have removed a lot of boiler plate code, and made the check of a date much simpler to understand.

Chapter 6 on Transaction Processing is a really well thought out section, and explains the differences between Container Managed Transactions (CMT) (default), and Bean Managed Transactions (BMT). This is a must read for anyone doing anything beyond simple transactions. The explanation on the limitations of BMT are very important to consider. The chapter explains the transaction annotations to a level that I have not really seen in other books. The recipes (examples) are very clear, and make the concepts much easier to understand. For example:

There are a few restrictions on the use of transaction attributes. First, the REQUIRED attribute can not be used for methods of web service endpoints. This is because the caller of the endpoint has not started a transaction and the since the REQUIRED attribute requires one, it will fail.
This is very well written and explains the concept, and gives reasons why this will not work.

The chapter on EJB security mentioned a feature in GlassFish which I think is very cool, and only requires checking a check box to enable. The feature is Default Principal to Role Mapping. This allows you to avoid having to generate a XML deployment configuration files to map the roles from the Web/EJB application to roles defined in a realm on the server.

The chapter on Interceptors provides a number of good use case examples where interceptors really shine. I really liked this chapter and the explanation of annotations for use with interceptors like @ExcludeClassInterceptors and the differentiation between class and method interceptors. The explanation on interceptor chaining is one of the easiest explanations I have seen.

If I had a single complaint about the book, it would be chapter ten on Web Services. It has a couple of recipes, but not nearly enough. Web services play a major role in EE today; especially JAX-RS. The lack of a number clear examples is a missed opportunity.

Overall I found the book to be really well done, and I would gladly keep it as a reference for myself, and team. I would give it (4/5) stars. It is well written and worth purchasing.

OpenJDK 7 on Apple G5 PowerPC on Mac OS X 10.5.8

Apple G5 Power Mac

  • Are you an Apple G5 owner feeling a little left out by Apple?
  • Do you have a very expensive and capable piece of hardware which can still be a valuable and fashionable?
  • Do you develop applications in Java, or run applications based on Java and feel left behind?

I have a solution for you! Use OpenJDK BSD Port!

Using a build script sent to me by Kurt Miller, build recommendations from Kelly O'Hair, and the great work of the BSD Port team... I created a new build of OpenJDK 7 for my PPC based system using the Zero VM.

The results are fantastic. I can run GlassFish 3.1.1 along with all my enterprise applications.

GlassFish 3.1.1 on Zero VM
Well it wouldn't be me if I didn't share my good fortune. I updated the Darwin9Build page for OpenJDK to demonstrate how to build OpenJDK for yourself. There are also links to the binaries too.

Here is the link: Darwin9Build


Wednesday, February 08, 2012

Book Review: Murach's Java Programming 4th Ed.

I recently had the pleasure to read and examine Murach's Java Programming 4th Ed. Joel Murach does a good job of producing training and reference manuals for a variety of Java technologies. This is no exception.

The book focuses on Java 7 Standard Edition, but does include a number of other features which were added in Java 5 and Java 6 for completeness.

One of the things I really like is that it uses NetBeans as the IDE to teach Java to the next generation of programmers. A lot of books on Java focus on the language itself. This would have the appearance that an IDE plays no role in learning Java. Any professional programmer will tell you that an IDE is absolutely essential in making you more productive. Joel's choice of NetBeans as a tool to teach Java with demonstrates its ease of use with beginners. Chapter 1 is dedicated to getting the new programmer up to speed with NetBeans.

The book offers educators and trainers optional support materials for teaching. This thoughtful approach to education is vital for anyone teaching Java.

The layout and examples are designed to get the programmer up to speed on the topic being covered.  A typical example of the layout is to have textual material on the left-side page with examples on the right. This allows you to read the material, and refer to the actual code examples. The book layout makes it easy to teach and learn. You are not left reading a volume of material without any code examples to make the essential points. This is a real benefit to the reader.

In addition, the book is developed with exercises to get the beginner involved. The exercises are based on the material learned in the chapter. The example exercises follow a common theme throughout the book allowing you to build on top of the information learned in the previous chapters. This common thread keeps you engaged through the book to see how the different topics tie the applications together.

Overall I would highly recommend the book to anyone looking to learn Java 7, or update their skills. I would give it (4/5) stars.

Chapters

Chapter 3 has a great section on using BigDecimal and arbitrary precision arithmetic. I really liked the explanation and examples. Although BigDecimal was added in 1.4.2 it is not used as much as it should, and this gives it a little press.

Chapter 5 mentions a best practice which is often overlooked. The practice is to add any custom exceptions to the package in which classes are likely to throw them. Often I find that the exceptions are in a package.exception package. This may be a good design if there is a general hierarchy for using the exceptions in multiple sub-packages, but generally is not the case.

Chapter 7 describes a relationship between a class and its instance. The description is eloquent and worth repeating.
"Once an instance of a class is created, it has an identity (a unique address) and a state (the values that it holds). Although an object's state may change throughout a program, its identity never does."
This is a beautiful explanation of OO programming relationships between a class and instance.

Chapter 8 has a discussion on the use of final. The point to be made is that if you declare a class final, all of its methods automatically become final implicitly.

Chapter 10 on nested classes really does a good job of explaining the requirements and limitations of nested classes. There are a couple of exam type gems like an inner class can not contain any static methods, or variables, and a static class can only reside inside another class.

In chapter 11, there was a remark about using Arrays.sort(Object[]) to sort an array. It mentioned that the objects must implement the Comparable interface. I thought that it would "naturally order" without it. I was wrong. I validated that specific case. Even an old dog can learn new tricks.

Chapter 19 is the best tutorial on StAX I have seen. It is simple and easy to follow. After reading the chapter, I will go back and look at StAX when I need this kind of low level XML handling.

Chapter 20 covers Apache Derby (JavaDB). It does a good job of explaining the basics, and gives some implementation recommendations. My only gripe is that the author fell into the Oracle trap of claiming that it is not an enterprise capable database. This is simply not true as borne out from my own experience.

If there is a weakness in the book, it has to do with Chapter 22 on Threads. The chapter does not cover this important topic in enough details. It also does not mention the Java Concurrency Library which has been standard since JDK 6, and handles a lot of the issues with direct thread programming. This chapter should be updated.

Errata

Figure 2-5 Statements that mix int and double variables. The first example requires a cast to int.
int result9 = (int) invoiceTotal / invoiceCount;
Figure 9-1 Example 1 should reflect the current Java design principles. All methods of a public interface are public and abstract.
public interface Printable {
void print();
}
Figure 9-1 Description bullet #3 should read
A classs that implements an interface must provide an implementation for each method defined by the interface, or the class must be declared abstract.
Figure 10-8 Bullet #2 is incorrect. A nested class does not need to be enclosed in a public class of the same file name as the public class. For example X.java could contain the following code:
class P {

    Q q;

    P() {
        q = new Q();
    }

    public Q getQ() {
        return q;
    }

    class Q {

        void print() {
            System.out.println("I am inside P");
        }
    }
}

This will work without being in a public class.

Figure 12-2 Common Collection Classes.
The statement that a HashSet requires that classes to implement a hashcode is incorrect.

Chapter 12 (Page 384) 2nd Para. incorrectly states the two queue methods as push and pull. The pull method does not exist, and was likely supposed to be pop. There are actually three from the Queue<E> interface of note: offer(E e), peek(), and poll.

Popular Posts