Saturday, August 30, 2014

Big Java News in Late Summer 2014

As is typical when JavaOne is imminent, there has been much big news in the Java community recently. This post briefly references three of these items (Java SE 8 updates, Java SE 9, and Java EE 8) and a "bonus" reference to a post I found to be one of the clearer ones I have seen on classpath/classloader issues.

String Deduplication in Oracle Java 8 JVM

In String Deduplication – A new feature in Java 8 Update 20, Fabian Lange introduces String Deduplication for the G1 Garbage Collector using the JVM option -XX:+UseStringDeduplication that was introduced with JDK 8 Update 20. The tools page for the Java launcher has been updated to mention the JVM options -XX:+UseStringDeduplication, -XX:+PrintStringDeduplicationStatistics, and -XX:StringDeduplicationAgeThreshold. More details on JDK 8 Update 20 are available in the blog post Release: Oracle Java Development Kit 8, Update 20. The Lange post has also sparked discussion on this and related JVM options on the Java subreddit.

Java 9 Features

Java 9 has been the hot topic of discussion in the Java community since the OpenJDK JDK 9 Project was announced. Long-awaited Java modularity (Project Jigsaw, which was booted from JDK 8) is probably the largest new feature anticipated for Java 9. Paul Krill writes in Why developers should get excited about Java 9 that "Jigsaw isn't the only new addition slated for Java 9. Support for the popular JSON (JavaScript Object Notation) data interchange format is key feature as well, along with process API, code cache, and locking improvements. The six JEPs currently proposed on that OpenJDK JDK 9 page are 102 (Process API Updates), 143 (Improve Contended Locking), 197 (Segmented Code Cache), 198 (Light-Weight JSON API), 199 (Smart Java Compilation, Phase 2), and 201 (Modular Source Code).

In the blog post Java 9 is coming with money api, otaviojava introduces JSR 354 ("JSR 354: Money and Currency API"), describes why it is needed, covers how it might be implemented, and concludes, "this API is expected to [be in] Java 9."

Java EE 8

Reza Rahman's post Java EE 8 Takes Off! talks about JSR 366 (Java EE 8 Specification) being kicked off. This post lists some of the anticipated high-level content for Java EE along with links to related JSRs.

Demystifying the Java Classpath

Java classpath issues are definitely one of the more difficult challenges that Java developers can face. The post Jar Hell made Easy - Demystifying the classpath with jHades provides a nice overview of some of the most common issues related to classpath and classloaders with concise and simple explanations of why these occur. I have not used jHades, but the quality of this post has definitely sparked my interest in that tool.

Conclusion

"Java" (SE, EE, JVM, etc.) keeps advancing and bringing us new language features, libraries, and tools. This post has referenced posts that highlight recent developments in JDK 8, JDK 9, and Java EE 8.

Friday, August 22, 2014

Java Numeric Formatting

I can think of numerous times when I have seen others write unnecessary Java code and I have written unnecessary Java code because of lack of awareness of a JDK class that already provides the desired functionality. One example of this is the writing of time-related constants using hard-coded values such as 60, 24, 1440, and 86400 when TimeUnit provides a better, standardized approach. In this post, I look at another example of a class that provides the functionality I have seen developers often implement on their one: NumberFormat.

The NumberFormat class is part of the java.text package, which also includes the frequently used DateFormat and SimpleDateFormat classes. NumberFormat is an abstract class (no public constructor) and instances of its descendants are obtained via overloaded static methods with names such as getInstance(), getCurrencyInstance(), and getPercentInstance().

Currency

The next code listing demonstrates calling NumberFormat.getCurrencyInstance(Locale) to get an instance of NumberFormat that presents numbers in a currency-friendly format.

Demonstrating NumberFormat's Currency Support
/**
 * Demonstrate use of a Currency Instance of NumberFormat.
 */
public void demonstrateCurrency()
{
   writeHeaderToStandardOutput("Currency NumberFormat Examples");
   final NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.US);
   out.println("15.5      -> " + currencyFormat.format(15.5));
   out.println("15.54     -> " + currencyFormat.format(15.54));
   out.println("15.345    -> " + currencyFormat.format(15.345));  // rounds to two decimal places
   printCurrencyDetails(currencyFormat.getCurrency());
}

/**
 * Print out details of provided instance of Currency.
 *
 * @param currency Instance of Currency from which details
 *    will be written to standard output.
 */
public void printCurrencyDetails(final Currency currency)
{
   out.println("Concurrency: " + currency);
   out.println("\tISO 4217 Currency Code:           " + currency.getCurrencyCode());
   out.println("\tISO 4217 Numeric Code:            " + currency.getNumericCode());
   out.println("\tCurrency Display Name:            " + currency.getDisplayName(Locale.US));
   out.println("\tCurrency Symbol:                  " + currency.getSymbol(Locale.US));
   out.println("\tCurrency Default Fraction Digits: " + currency.getDefaultFractionDigits());
}

When the above code is executed, the results are as shown next:

==================================================================================
= Currency NumberFormat Examples
==================================================================================
15.5      -> $15.50
15.54     -> $15.54
15.345    -> $15.35
Concurrency: USD
 ISO 4217 Currency Code:           USD
 ISO 4217 Numeric Code:            840
 Currency Display Name:            US Dollar
 Currency Symbol:                  $
 Currency Default Fraction Digits: 2

The above code and associated output demonstrate that the NumberFormat instance used for currency (actually a DecimalFormat), automatically applies the appropriate number of digits and appropriate currency symbol based on the locale.

Percentages

The next code listings and associated output demonstrate use of NumberFormat to present numbers in percentage-friendly format.

Demonstrating NumberFormat's Percent Format
/**
 * Demonstrate use of a Percent Instance of NumberFormat.
 */
public void demonstratePercentage()
{
   writeHeaderToStandardOutput("Percentage NumberFormat Examples");
   final NumberFormat percentageFormat = NumberFormat.getPercentInstance(Locale.US);
   out.println("Instance of: " + percentageFormat.getClass().getCanonicalName());
   out.println("1        -> " + percentageFormat.format(1));
   // will be 0 because truncated to Integer by Integer division
   out.println("75/100   -> " + percentageFormat.format(75/100));
   out.println(".75      -> " + percentageFormat.format(.75));
   out.println("75.0/100 -> " + percentageFormat.format(75.0/100));
   // will be 0 because truncated to Integer by Integer division
   out.println("83/93    -> " + percentageFormat.format((83/93)));
   out.println("93/83    -> " + percentageFormat.format(93/83));
   out.println(".5       -> " + percentageFormat.format(.5));
   out.println(".912     -> " + percentageFormat.format(.912));
   out.println("---- Setting Minimum Fraction Digits to 1:");
   percentageFormat.setMinimumFractionDigits(1);
   out.println("1        -> " + percentageFormat.format(1));
   out.println(".75      -> " + percentageFormat.format(.75));
   out.println("75.0/100 -> " + percentageFormat.format(75.0/100));
   out.println(".912     -> " + percentageFormat.format(.912));
}
==================================================================================
= Percentage NumberFormat Examples
==================================================================================
1        -> 100%
75/100   -> 0%
.75      -> 75%
75.0/100 -> 75%
83/93    -> 0%
93/83    -> 100%
.5       -> 50%
.912     -> 91%
---- Setting Minimum Fraction Digits to 1:
1        -> 100.0%
.75      -> 75.0%
75.0/100 -> 75.0%
.912     -> 91.2%

The code and output of the percent NumberFormat usage demonstrate that by default the instance of NumberFormat (actually a DecimalFormat in this case) returned by NumberFormat.getPercentInstance(Locale) method has no fractional digits, multiplies the provided number by 100 (assumes that it is the decimal equivalent of a percentage when provided), and adds a percentage sign (%).

Integers

The small amount of code shown next and its associated output demonstrate use of NumberFormat to present numbers in integral format.

Demonstrating NumberFormat's Integer Format
/**
 * Demonstrate use of an Integer Instance of NumberFormat.
 */
public void demonstrateInteger()
{
   writeHeaderToStandardOutput("Integer NumberFormat Examples");
   final NumberFormat integerFormat = NumberFormat.getIntegerInstance(Locale.US);
   out.println("7.65   -> " + integerFormat.format(7.65));
   out.println("7.5    -> " + integerFormat.format(7.5));
   out.println("7.49   -> " + integerFormat.format(7.49));
   out.println("-23.23 -> " + integerFormat.format(-23.23));
}
==================================================================================
= Integer NumberFormat Examples
==================================================================================
7.65   -> 8
7.5    -> 8
7.49   -> 7
-23.23 -> -23

As demonstrated in the above code and associated output, the NumberFormat method getIntegerInstance(Locale) returns an instance that presents provided numerals as integers.

Fixed Digits

The next code listing and associated output demonstrate using NumberFormat to print fixed-point representation of floating-point numbers. In other words, this use of NumberFormat allows one to represent a number with an exactly prescribed number of digits to the left of the decimal point ("integer" digits) and to the right of the decimal point ("fraction" digits).

Demonstrating NumberFormat for Fixed-Point Numbers
/**
 * Demonstrate generic NumberFormat instance with rounding mode,
 * maximum fraction digits, and minimum integer digits specified.
 */
public void demonstrateNumberFormat()
{
   writeHeaderToStandardOutput("NumberFormat Fixed-Point Examples");
   final NumberFormat numberFormat = NumberFormat.getNumberInstance();
   numberFormat.setRoundingMode(RoundingMode.HALF_UP);
   numberFormat.setMaximumFractionDigits(2);
   numberFormat.setMinimumIntegerDigits(1);
   out.println("234.234567 --> " + numberFormat.format(234.234567));
   out.println("1          --> " + numberFormat.format(1));
   out.println(".234567    --> " + numberFormat.format(.234567));
   out.println(".349       --> " + numberFormat.format(.349));
   out.println(".3499      --> " + numberFormat.format(.3499));
   out.println("0.9999     --> " + numberFormat.format(0.9999));
}
==================================================================================
= NumberFormat Fixed-Point Examples
==================================================================================
234.234567 --> 234.23
1          --> 1
.234567    --> 0.23
.349       --> 0.34
.3499      --> 0.35
0.9999     --> 1

The above code and associated output demonstrate the fine-grain control of the minimum number of "integer" digits to represent to the left of the decimal place (at least one, so zero shows up when applicable) and the maximum number of "fraction" digits to the right of the decimal point. Although not shown, the maximum number of integer digits and minimum number of fraction digits can also be specified.

Conclusion

I have used this post to look at how NumberFormat can be used to present numbers in different ways (currency, percentage, integer, fixed number of decimal points, etc.) and often means no or reduced code need be written to massage numbers into these formats. When I first began writing this post, I envisioned including examples and discussion on the direct descendants of NumberFormat (DecimalFormat and ChoiceFormat), but have decided this post is already sufficiently lengthy. I may write about these descendants of NumberFormat in future blog posts.

Tuesday, August 19, 2014

jinfo: Command-line Peeking at JVM Runtime Configuration

In several recent blogs (in my reviews of the books Java EE 7 Performance Tuning and Optimization and WildFly Performance Tuning in particular), I have referenced my own past blog posts on certain Oracle JDK command-line tools. I was aghast to discover that I had never exclusively addressed the nifty jinfo tool and this post sets to rectify that troubling situation. I suspect that the reasons I chose not to write about jinfo previously include limitations related to jinfo discussed in my post VisualVM: jinfo and So Much More.

In the Java SE 8 version of jinfo running on my machine, the primary limitation of jinfo on Windows that I discussed in the post Acquiring JVM Runtime Information has been addressed. In particular, I noted in that post that the -flags option was not supported on Windows version of jinfo at that time. As the next screen snapshot proves, that is no longer the case (note the use of jps to acquire the Java process ID to instruct jinfo to query).

As the above screen snapshot demonstrates, the jinfo -flags command and option show the flags the explicitly specified JVM options of the Java process being monitored.

If I want to find out about other JVM flags that are in effect implicitly (automatically), I can run java -XX:+PrintFlagsFinal to see all default JVM options. I can then query for any one of these against a running JVM process to find out what that particular JVM is using (same default or overridden different value). The next screen snapshot demonstrates how a small portion of the output provided from running java -XX:+PrintFlagsFinal.

Let's suppose I notice a flag called PrintHeapAtGC in the above output and want to know if it's set in my particular Java application (-XX:+PrintHeapAtGC means it's set and -XX:-PrintHeapAtGC means it's not set). I can have jinfo tell me what its setting is (note my choice to use jcmd instead of jps in this case to determine the Java process ID):

Because of the subtraction sign (-) instead of an addition sign (+) after the colon and before "PrintHeapAtGC", we know this is turned off for the Java process with the specified ID. It turns out that jinfo does more than let us look; it also let's us touch. The next screen snapshot shows changing this option using jinfo.

As the previous screen snapshot indicates, I can turn off and on the boolean-style JVM options by simply using the same command to view the flag's setting but preceding the flag's name with the addition sign (+) to turn it on or with the substraction sign (-) to turn it off. In the example just shown, I turned off the PrintGCDateStamps, turned it back on again, and monitored its setting between those changes. Not all JVM options are boolean conditions. In those cases, their new values are assigned to them by concatenating the equals sign (=) and new value after the flag name. It's also important to note that the target JVM (the one you're trying to peek at and touch with jinfo will not allow you to change all its JVM option settings). In such cases, you'll likely see a stack trace with message "Command failed in target VM."

In addition to displaying a currently running JVM's options and allowing the changing of some of these, jinfo also allows one to see system properties used by that JVM as name/value pairs. This is demonstrated in the next screen snapshot with a small fraction of the output shown.

Perhaps the easiest way to run jinfo is to simply provide no arguments other than the PID of the Java process in question and have both JVM options (non-default and command-line) and system properties displayed. Running jinfo -help provides brief usage details. Other important details are found in the Oracle documentation on the jinfo tool. These details includes the common (when it comes to these tools) reminder that this tool is "experimental and unsupported" and "might not be available in future releases of the JDK." We are also warned that jinfo on Windows requires availability of dbgeng.dll or installed Debugging Tools For Windows.

Although I have referenced the handy jinfo command line tool previously in posts VisualVM: jinfo and So Much More and Acquiring JVM Runtime Information, it is a handy enough tool to justify a post of its very own. As a command-line tool, it enjoys benefits commonly associated with command-line tools such as being relatively lightweight, working well with scripts, and working in headless environments.

Friday, August 15, 2014

Autoboxing, Unboxing, and NoSuchMethodError

J2SE 5 introduced numerous features to the Java programming language. One of these features is autoboxing and unboxing, a feature that I use almost daily without even thinking about it. It is often convenient (especially when used with collections), but every once in a while it leads to some nasty surprises, "weirdness," and "madness." In this blog post, I look at a rare (but interesting to me) case of NoSuchMethodError resulting from mixing classes compiled with Java versions before autoboxing/unboxing with classes compiled with Java versions that include autoboxing/unboxing.

The next code listing shows a simple Sum class that could have been written before J2SE 5. It has overloaded "add" methods that accept different primitive numeric data types and each instance of Sum> simply adds all types of numbers provided to it via any of its overloaded "add" methods.

Sum.java (pre-J2SE 5 Version)
import java.util.ArrayList;

public class Sum
{
   private double sum = 0;

   public void add(short newShort)
   {
      sum += newShort;
   }

   public void add(int newInteger)
   {
      sum += newInteger;
   }

   public void add(long newLong)
   {
      sum += newLong;
   }

   public void add(float newFloat)
   {
      sum += newFloat;
   }

   public void add(double newDouble)
   {
      sum += newDouble;
   }

   public String toString()
   {
      return String.valueOf(sum);
   }
}

Before unboxing was available, any clients of the above Sum class would need to provide primitives to these "add" methods or, if they had reference equivalents of the primitives, would need to convert the references to their primitive counterparts before calling one of the "add" methods. The onus was on the client code to do this conversion from reference type to corresponding primitive type before calling these methods. Examples of how this might be accomplished are shown in the next code listing.

No Unboxing: Client Converting References to Primitives
private static String sumReferences(
   final Long longValue, final Integer intValue, final Short shortValue)
{
   final Sum sum = new Sum();
   if (longValue != null)
   {
      sum.add(longValue.longValue());
   }
   if (intValue != null)
   {
      sum.add(intValue.intValue());
   }
   if (shortValue != null)
   {
      sum.add(shortValue.shortValue());
   }
   return sum.toString();
}

J2SE 5's autoboxing and unboxing feature was intended to address this extraneous effort required in a case like this. With unboxing, client code could call the above "add" methods with references types corresponding to the expected primitive types and the references would be automatically "unboxed" to the primitive form so that the appropriate "add" methods could be invoked. Section 5.1.8 ("Unboxing Conversion") of The Java Language Specification explains which primitives the supplied numeric reference types are converted to in unboxing and Section 5.1.7 ("Boxing Conversion") of that same specification lists the references types that are autoboxed from each primitive in autoboxing.

In this example, unboxing reduced effort on the client's part in terms of converting reference types to their corresponding primitive counterparts before calling Sum's "add" methods, but it did not completely free the client from needing to process the number values before providing them. Because reference types can be null, it is possible for a client to provide a null reference to one of Sum's "add" methods and, when Java attempts to automatically unbox that null to its corresponding primitive, a NullPointerException is thrown. The next code listing adapts that from above to indicate how the conversion of reference to primitive is no longer necessary on the client side but checking for null is still necessary to avoid the NullPointerException.

Unboxing Automatically Coverts Reference to Primitive: Still Must Check for Null
private static String sumReferences(
   final Long longValue, final Integer intValue, final Short shortValue)
{
   final Sum sum = new Sum();
   if (longValue != null)
   {
      sum.add(longValue);
   }
   if (intValue != null)
   {
      sum.add(intValue);
   }
   if (shortValue != null)
   {
      sum.add(shortValue);
   }
   return sum.toString();
}

Requiring client code to check their references for null before calling the "add" methods on Sum may be something we want to avoid when designing our API. One way to remove that need is to change the "add" methods to explicitly accept the reference types rather than the primitive types. Then, the Sum class could check for null before explicitly or implicitly (unboxing) dereferencing it. The revised Sum class with this changed and more client-friendly API is shown next.

Sum Class with "add" Methods Expecting References Rather than Primitives
import java.util.ArrayList;

public class Sum
{
   private double sum = 0;

   public void add(Short newShort)
   {
      if (newShort != null)
      {
         sum += newShort;
      }
   }

   public void add(Integer newInteger)
   {
      if (newInteger != null)
      {
         sum += newInteger;
      }
   }

   public void add(Long newLong)
   {
      if (newLong != null)
      {
         sum += newLong;
      }
   }

   public void add(Float newFloat)
   {
      if (newFloat != null)
      {
         sum += newFloat;
      }
   }

   public void add(Double newDouble)
   {
      if (newDouble != null)
      {
         sum += newDouble;
      }
   }

   public String toString()
   {
      return String.valueOf(sum);
   }
}

The revised Sum class is more client-friendly because it allows the client to pass a reference to any of its "add" methods without concern for whether the passed-in reference is null or not. However, the change of the Sum class's API like this can lead to NoSuchMethodErrors if either class involved (the client class or one of the versions of the Sum class) is compiled with different versions of Java. In particular, if the client code uses primitives and is compiled with JDK 1.4 or earlier and the Sum class is the latest version shown (expecting references instead of primitives) and is compiled with J2SE 5 or later, a NoSuchMethodError like the following will be encountered (the "S" indicates it was the "add" method expecting a primitive short and the "V" indicates that method returned void).

Exception in thread "main" java.lang.NoSuchMethodError: Sum.add(S)V
 at Main.main(Main.java:9)

On the other hand, if the client is compiled with J2SE 5 or later and with primitive values being supplied to Sum as in the first example (pre-unboxing) and the Sum class is compiled in JDK 1.4 or earlier with "add" methods expecting primitives, a different version of the NoSuchMethodError is encountered. Note that the Short reference is cited here.

Exception in thread "main" java.lang.NoSuchMethodError: Sum.add(Ljava/lang/Short;)V
 at Main.main(Main.java:9)

There are several observations and reminders to Java developers that come from this.

  • Classpaths are important:
    • Java .class files compiled with the same version of Java (same -source and -target) would have avoided the particular problem in this post.
    • Classpaths should be as lean as possible to reduce/avoid possibility of getting stray "old" class definitions.
    • Build "clean" targets and other build operations should be sure to clean past artifacts thoroughly and builds should rebuild all necessary application classes.
  • Autoboxing and Unboxing are well-intentioned and often highly convenient, but can lead to surprising issues if not kept in mind to some degree. In this post, the need to still check for null (or know that the object is non-null) is necessary remains in situations when implicit dereferencing will take place as a result of unboxing.
  • It's a matter of API style taste whether to allow clients to pass nulls and have the serving class check for null on their behalf. In an industrial application, I would have stated whether null was allowed or not for each "add" method parameter with @param in each method's Javadoc comment. In other situations, one might want to leave it the responsibility of the caller to ensure any passed-in reference is non-null and would be content throwing a NullPointerException if the caller did not obey that contract (which should also be specified in the method's Javadoc).
  • Although we typically see NoSuchMethodError when a method is completely removed or when we access an old class before that method was available or when a method's API has changed in terms of types or number of types. In a day when Java autoboxing and unboxing are largely taken for granted, it can be easy to think that changing a method from taking a primitive to taking the corresponding reference type won't affect anything, but even that change can lead to an exception if not all classes involved are built on a version of Java supporting autoboxing and unboxing.
  • One way to determine the version of Java against which a particular .class file was compiled is to use javap -verbose and to look in the javap output for the "major version:". In the classes I used in my examples in this post (compiled against JDK 1.4 and Java SE 8), the "major version" entries were 48 and 52 respectively (the General Layout section of the Wikipedia entry on Java class file lists the major versions).

Fortunately, the issue demonstrated with examples and text in this post is not that common thanks to builds typically cleaning all artifacts and rebuilding code on a relatively continuous basis. However, there are cases where this could occur and one of the most likely such situations is when using an old JAR file accidentally because it lies in wait on the runtime classpath.

Saturday, August 9, 2014

Book Review: Java EE 7 Performance Tuning and Optimization

Packt Publishing recently published 's Osama Oransa's Java EE 7 Performance Tuning and Optimization. This post is my review of this book of twelve chapters and approximately 430 pages of substantive content.

Preface

The Preface of Java EE 7 Performance Tuning and Optimization provides brief summaries of all 12 chapters. The Preface also lists the main tools used throughout the book: JDK 7 Update 45, NetBeans 7.4, GlassFish 4.0, MySQL 5.5, Eclipse with Eclipse Test   Performance Tools Platform Project, Apache JMeter 2.10, and JProfiler 8. The Preface states that Java EE 7 Performance Tuning and Optimization is a "strong entry point for the persons without any performance tuning experience" and that the book is for "experienced Java developers, architects, team leaders, consultants, support engineers, and all people working in the performance tuning in the Java applications, and particularly in Java Enterprise applications."

Chapter 1: Getting Started with Performance Tuning

The first chapter of Java EE 7 Performance Tuning and Optimization is a nearly 30 pages long high-level discussion on "the art of performance tuning." The chapter discusses "Java EE performance tuning art" before looking at identifying performance issues and classifying these performance issues by waterfall development lifecycle discovery phase (when the issue was identified) and by root phase (when the issue was introduced).

Chapter 1 spends several pages discussing high-level "soft skills" one needs as a leader of a performance tuning team and the very-high-level approach used to narrow down causes of performance issues. There are several other high-level discussions covered in this chapter such as descriptions of the different common architectures and the different application layers to be performance tuned.

Java EE 7 Performance Tuning and Optimization's initial chapter has a few things for experienced Java developers, but is really better suited for those entirely new to Java performance tuning considerations. It might also be better suited to the readers who are team leaders and consultants than those readers who are developers. The most beginning Java developers might benefit from the brief, very high level descriptions of application layers, but most of this information should be something most developers already know.

Chapter 2: Understanding Java Fundamentals

Chapter 2 of Java EE 7 Performance Tuning and Optimization provides lower-level details than the first chapter. However, much of this chapter is identical to online resources upon which it is based. One of the first sentences in Chapter 2 states, "Most of the content of this chapter adheres to Oracle Java documentations as the source of Java specifications and features." I didn't know what this meant when I first read it, but I believe it is the author's way of saying that much of the material in the chapter comes directly from Oracle's Java-related documentation.

The second chapter begins with a section on new Java EE 7 features. Each new Java EE 7 feature is briefly described, illustrated with pseudo code, and has its underlying specification referenced with a link. I agree with the author's recommendation that anyone with awareness of the new Java EE 7 features should skip this section.

Chapter 2's section "Understanding memory structure in the JVM" is good, but much of it is word-for-word the same as the text provided in Chapter 2 ("The Structure of the Java Virtual Machine") of the Java Virtual Machine specification and in Memory Management in the HotSpot JVM. In fact, many of the diagrams are the same or nearly the same except that the original sources have colors included in the diagrams, which are grayscale in the PDF version of Java EE 7 Performance Tuning and Optimization that I reviewed.

I also liked the "Understanding concurrency in Java" section of Java EE 7 Performance Tuning and Optimization, but it seemed to be much the same content (including several passages that are word-for-word the same) as the "Concurrency Basics" section of the Java EE 7 Tutorial and Chapter 17 ("Threads and Locks") of the Java Virtual Machine specification.

Chapter 2 also includes brief discussion of JSP/servlet application scopes and more focus on Java EE 7 concurrency features. There is some good information in this chapter, but the downsides are that some of its content seems a little out of order and much of the content relies heavily, even directly, on Oracle's documentation.

Chapter 3: Getting Familiar with Performance Testing

The third chapter of Java EE 7 Performance Tuning and Optimization opens with a definition of performance testing and contrasts performance with the more common types of testing done to test functionality. The chapter then lists and briefly discusses each "important aspect of the performance testing process." This section contains useful definitions and terminology for those new to performance testing and performance optimization and is largely at a higher or more general level than Java EE. Someone wanting to know the different between load testing and stress testing, for example, would find this section very useful.

The third chapter provides a list of some tools that can used in performance testing, but its focus is on JMeter, which it devotes numerous pages to. The section on JMeter could have likely been a chapter of its own and provides quite a bit of detail about setting up and applying JMeter in performance testing of a web service, of a web application, and of JDBC code.

Chapter 4: Monitoring Java Applications

Chapter 4 of Java EE 7 Performance Tuning and Optimization provides a survey of some of the monitoring tools available for monitoring an application at different layers. The section on monitoring operating systems discusses and provides illustrations of Windows-based monitoring tools such as Windows 7 Task Manager, Resource Monitor, netstat, and Typeperf. The portion covering Linux monitoring tools is shorter (about half of a page), but lists tools such as ps, pgrep, pstree, top, vmstat, iostat, sar, and tcpdump, and refers the reader to these respective tools' man pages for additional details. The section on operating system monitoring concludes with an example of applying Windows and Linux tools to diagnose heavily contended CPUs.

Chapter 4's coverage of "The Java monitoring tools" talks about "the JDK monitoring tools" that are specifically delivered with the Oracle/Sun JDK such as JConsole, VisualVM, and Java Mission Control. The section on "Java monitoring tools" also mentions IDEs (and monitoring plug-ins for the IDEs) and application server-specific monitoring tools.

The JVM Tool Interface (JVM TI) is introduced in Chapter 4 as a replacement for the deprecated JVMPI/JVMDI. This section is essentially a condensed version of the Introduction portion of the JVM Tool Interface 1.2 specification.

The Chapter 4 section "Understanding the different JDK tools" discusses tools provided with the Oracle/Sun JDK for monitoring (jps, jstat, jstatd), profiling (JConsole, VisualVM, Java Mission Control), and troubleshooting (jhat, jstack, jinfo, jmap, jsadebugd). The overviews of each of these tools and their respective syntax seem to be borrowed directly from the Oracle tools pages (online and similar to man pages), but the section does include some specifics about using these tools in the book's examples at the end of each tool's discussion. Even the categorization of the tools is the same as the main Oracle tools page.

Chapter 4 barely scratches the surface of using JConsole, but provides more detailed and nicely illustrated examples of using VisualVM, Oracle Java Mission Control, and the NetBeans Profiler. The chapter introduces Eclipse plug-ins and provides some focus on JVM Monitor and Test and Performance Tools Platform (Eclipse Memory Analyzer Tool is covered in a later chapter). Chapter 4 concludes by introducing and significantly covering JProfiler.

Chapter 5: Recognizing Common Performance Issues

Java EE 7 Performance Tuning and Optimization's fifth chapter outlines categories of performance issues, briefly describes each category, and then looks in more detail at a subset of the individual categorized performance issues. The sections on "Performance Symptoms" of each performance issue are probably the most interesting to me. For developers new to performance tuning, it could be helpful to have "performance symptoms" collected and associated with particular likely causes of the symptoms. The discussion of performance issues also hits on some commonly accepted practices to adopt and some commonly accepted practices to avoid.

The second major part of Chapter 5 deals with "client-side performance issues." In this case, "client-side" appears to be synonymous with (assumes) a web application and web browser. This portion of Chapter 5 introduces and illustrates with screen snapshots several browser-based tools such as Chrome Developer Tools, Chrome plugin Speed Tracer, Internet Explorer Developer Tools, and FireFox Developer Tools.

Chapter 6: CPU Time Profiling

Chapter 6 of Java EE 7 Performance Tuning and Optimization is the first of three chapters to focus on use of Java profilers. This chapter focuses on profiling CPU usage and the subsequent two chapters focus on profiling threading and memory usage. This sixth chapter introduces three Java profiling tools that feature CPU profiling capabilities: NetBeans Profiler, Java Mission Control (JMC), and JProfiler. These sections on using these three tools provide sharp screen snapshots (full color in electronic version) to demonstrate all three graphically-oriented tools in action.

After introducing the three profiling tools in action profiling CPU usage and method execution times, Chapter 6 moves onto using the results provided by those tools to identify and categorize potential causes of the most significant time-consuming operations. This includes high level discussion of how to resolve many of these types of issues and a little more detailed coverage of addressing issues related to inappropriate algorithms.

Chapter 7: Thread Profiling

Chapter 7 demonstrates using the same three Java profiling tools demonstrated in Chapter 6 (NetBeans Profiler, Java Mission Control, JProfiler), but this time used for profiling threads. The demonstration of these three profiling tools used for profiling thread usage is followed by some discussion on what the results might indicate.

The seventh chapter of Java EE 7 Performance Tuning and Optimization also looks at "options from the operating system to produce a thread dump for the executing Java process" (CTRL+Break and sending SIGQUIT signal [kill -3]). There is also a section on using the Oracle JDK tools jstack, JVisualVM, and Java Mission Control to dump heaps. The section on "Understanding the thread dump structure" provides a highly useful overview of the data included in a thread dump and how to use it. The chapter also introduces the Thread Dump Analyzer.

Chapter 7 includes very high level discussion of potential threading performance issues. It also points out that -XX:+PrintFlagsFinal and jinfo can be used with the Oracle JDK to determine which JVM flags are currently set. High level discussion on "detecting the root cause of a hung application" is also in Chapter 7. There is a very brief overview of dealing with thread-related issues at the end of the chapter.

Chapter 8: Memory Profiling

The eighth chapter of Java EE 7 Performance Tuning and Optimization is the concluding chapter of the three-chapter series on applying Java profiling tools and this chapter specifically covers memory profiling. The chapter illustrates using NetBeans Profiler and JProfiler to perform memory profiling. This chapter also describes what is contained in a garbage collection log before briefly demonstrating use of JVisualVM's plugin called Visual GC (the plugin version of the Oracle visualgc command-line tool that is downloaded separately from the JDK command-line tools) to see memory and garbage collection activities in the different heap memory pools.

Chapter 8 provides an overview of heap dumps and their content. It explains approaches that can be used to obtain a heap dump such as use of JVM options (-XX:+HeapDumpOnOutOfMemoryError and -XX:HeapDumpPath), Oracle JDK tools (jmap, JVisualVM, and Java Mission Control), and with Java profiler tools (NetBeans, Eclipse Memory Analyzer Tool, and JProfiler). This chapter also describes Object Query Language (OQL) and provides examples of OQL similar to those provided by the OQL Help that can be seen at http://localhost:7000/oqlhelp/ when running jhat against a heap dump on the local machine.

Chapter 8 wraps up with a nice high-level overview of the types of things that commonly lead to memory issues and OutOfMemoryError.

Chapter 9: Tuning an Application's Environment

Chapter 9 of Java EE 7 Performance Tuning and Optimization opens with a discussion of JVM tuning using the standard and non-standard Oracle JVM options. This includes high-level discussion of using JVM options related to garbage collection and different garbage collectors. There is a similar section on JRockit VM tuning options.

After covering JVM tuning, Chapter 9 moves onto application server tuning with general application server tuning discussion illustrated with examples specific to GlassFish and WebLogic application servers. The section "Tuning the Oracle GlassFish application server" states that it goes "through the configuration as specified in the GlassFish 4 performance tuning guide." In fact, this section is very close to a subset of what is in the GlassFish 4 Performance Tuning Guide but does offer a small number of screen snapshots not available in the guide. The chapter also includes brief, high-level overviews of "tuning the Oracle Weblogic application server" and "tuning web servers (HTTP servers)" (including Apache HTTP Server and Oracle Web Server).

"Tuning the operating system and hardware" rounds out Chapter 9 and is a very brief discussion of issues to consider and strategies for tuning operating system and hardware.

Chapter 10: Designing High-performance Enterprise Applications

The tenth chapter of Java EE 7 Performance Tuning and Optimization begins with the assertion, "Design is our key to a good performing enterprise application" and then mentions the objective of the chapter to "cover some of the design areas that we should consider while designing our enterprise applications." The first section in this chapter provides high level discussion on things to think about when designing and how there are trade-offs between different design objectives. This high level discussion includes comparing "potential performance impacts" of various considerations (such as security and framework choices) on performance.

Chapter 10 devotes a couple pages to high-level discussion of "performance anti-patterns." This is followed by sections on Service-Oriented Architecture (SOA) performance aspects and Resource-Oriented Architecture (ROA) performance aspects. The chapter concludes with coverage of data caching performance aspects.

Chapter 11: Performance Tuning Tips

Java EE 7 Performance Tuning and Optimization's Chapter 11 begins with a discussion on performance considerations during Agile software development. This discussion addresses when in the agile development process performance considerations should be made. It also introduces Test-Driven Development (TDD) and code reviews (manual and automated) as software approaches to improve performance tuning ability. The section on automated code reviews lists several code analysis tools as standalone tools and as NetBeans tools.plugins (such as PMD).

The "Java EE Performance Tuning Tips" section of Chapter 11 provides very brief, high-level descriptions of Java EE performance tuning tips categorized by Java EE component (web services, Enterprise JavaBeans, servlets/JavaServer Pages, JavaServer Faces, and Java Persistence API). The "Java Performance Tuning Tips" section provides a high-level overview of some common issues in Java SE code that lead to performance issues and mentions tips (coding and JVM options) to reduce or avoid those issues. There is also discussion of logging and exception handling best practices that can help performance.

Chapter 11's section "Using the javap tool to understand micro-optimizations" provides an overview of one of my favorite Oracle JDK command-line tools, javap, to determine how changes are impacting the byte code. The "Database performance tuning tips" section of Chapter 11 discusses a few database performance tips such as use of SQL optimizer hints and batch processing. Chapter 11 ends with a section on "Client-side optimization" that addresses "presentation layer performance." The author introduces and briefly illustrates PageSpeed Insights as part of this client-size optimization discussion.

Chapter 12: Tuning a Sample Application

The objective of the final chapter of Java EE 7 Performance Tuning and Optimization is "to demonstrate the performance tuning activities of a small application so that we can practice some of the activities that we have learned in this book." The example used here is called ExcellentSurvey and numerous pages are devoted to identifying and fixing performance-related issues in this ExcellentSurvey sample application. The chapter's summary also summarizes the book as a whole.

General Observations
  • Java EE 7 Performance Tuning and Optimization covers a lot of ground. It is mostly at an introductory level. Much of the content in this book is available online, but the book brings all of this content together in a single source. Readers likely to benefit most from reading this book are those with minimal Java performance tuning experience. The book provides a wide overview of available tools, tips, techniques, and best practices related to Java performance tuning. This book is not as deep as a book like Java Performance, but is instead targeted at higher level performance tuning concepts and approaches.
  • Although Java EE 7 Performance Tuning and Optimization does cover performance concepts related to Java EE application servers and Java EE, much of what it covers can be applied even more generally to all types of Java-based/JVM-hosted applications.
  • Screen snapshots are generally sharp and readable. They tend to be full color in the electronic version (PDF) of the book that I reviewed.
  • Code listings are in black font on white background (no color syntax) and there are no line numbers included in the code listings.
  • A flowchart for troubleshooting Java performance issues is built as the book progresses and this is a good starting point for someone wanting a systematic approach to resolving performance issues.
  • Java EE 7 Performance Tuning and Optimization is lengthier than it needs to be.
    • Content reproduced from Oracle's sites and other sites could have been summarized and referenced rather than being reproduced. The section on Linux monitoring tools used the better approach of listing the tools and referencing where to get the details on those tools. I think it would have been better if other parts of the book had followed that same tactic instead of merely reproducing content from those other sources.
    • Some of the overview and high-level concepts chapters seemed overly verbose to me, but that could be more a matter of taste. I prefer books to dive right into technical detail with only a little description and background; others prefer more background and discussion.
  • Some portions of Java EE 7 Performance Tuning and Optimization were difficult to understand due to awkward sentences or imprecise wording.
    • For example, the fourth chapter and sixth chapters discuss use of profiling tools to "catch HotSpot areas." At first, I was trying to figure out how the paragraph in Chapter 4 was specific to the HotSpot virtual machine because it was referenced with the same trademarked-style "HotSpot." Only after its second use in the same section, where "HotSpot areas" were described as "underperforming areas," did I confirm my suspicion that these references were intended to be to "hot spots" rather than to "HotSpots. Once I realized this, I was able to use context to distinguish when the author was talking about the HotSpot JVM versus talking about "Hot Spots" spots where code execution times are longer or more memory is being consumed.
    • A second example is this phrase: "Missed cache hits are very costive..."
    • An otherwise interesting brief discussion on potential memory issues due to inconsistencies between equals(Object) and hashCode() is a bit marred by a typo (uses "mush" rather than "must") "In Java, if any two objects are equal, then their hash code values mush be equal as well."
    • There are numerous more examples like these and I feel that this book could have used a round or two more of text editing. More editing would have probably helped tighten the book up a bit as well.
  • Java EE 7 Performance Tuning and Optimization makes heavy use (sometimes word for word identical text) of some significant online resources on JVM and application server performance tuning. These excellent online resources include:
  • Although Java EE 7 Performance Tuning and Optimization generally provides broad coverage of performance tuning Java EE 7 applications, it is important to note in a book review the things it doesn't cover or only covers very briefly. These include:
    • Specific Java EE implementations (specific application servers) - this is understandable as entire books and guides can be written to each particular Java EE application server implementation.
    • Java Management Extensions (JMX) - JMX is briefly referenced in a few locations, but its direct use is relatively ignored (it is indirectly discussed in terms of much of the coverage of VisualVM and other JMX-powered tools).
    • Virtual Machine Environments - Although operating system level performance tuning is covered multiple times in this book, running Java applications in virtual machines is not discussed in any level of significant detail.
  • Readers of Java EE 7 Performance Tuning and Optimization will likely want to download the associated code samples. In particular, the code examples that intentionally have problems that can be diagnosed with the described tools can provide practice on applying those tools.
  • Because different reviewers have different tastes, different background, different experiences, and different preferences, I think it's important to read book reviews from multiple sources. A different perspective on Java EE 7 Performance Tuning and Optimization is available in Abhishek's post Book Review: Java EE 7 Performance Tuning and Optimization.
Conclusion

Java EE 7 Performance Tuning and Optimization is a lengthy book that at times felt lengthier to me than it needed to be. However, developers new to Java performance (both EE and SE) may appreciate this extra background. Significant portions of some of the earlier chapters are nearly word-for-word the same as online Oracle Java documentation, but some readers new to these tools may find this more convenient than reading the original sources. Experienced Java EE developers who have had to do basic tuning of their applications in the past are likely to find a few new ideas or concepts in this book, but the people most likely to benefit are those with relatively little or no Java performance experience. For them, this book provides an overall view of the landscape of tools and concepts related to Java EE (and really general JVM) performance tuning.

The book's strengths include clear screen snapshots that are especially useful in demonstrating graphical tools and discussion and demonstration of applying tools to code examples with performance issues. Its weaknesses include numerous grammar issues and typos that make the text less readable than it otherwise would have been and stating the same things too many times and being lengthier than it needs to be. On one hand, the repetition seems monotonous to someone familiar with the concepts, but, on the other hand, this repetition might benefit those new to the concepts.

Monday, August 4, 2014

Books That Have Most Influenced My Software Development Career

I have read numerous books on software development and have learned valuable things from most of them, but a small subset of these books has significantly influenced me and how I develop software.

Effective C++ and More Effective C++

Although I had written a lot of lines of code in BASIC, Pascal, C, and C++ before starting my career, it was C++ that was my first professional programming language. As such, it was the first programming language in which I started to get past the basic syntax and and learn about more advanced issues and nuances of the programming languages. For the first time, I started considering performance issues, maintainability, readability, and issues other than just whether the code worked or not. Effective C++ and More Effective C++ helped me to see that there was more to programming than basic syntax and getting functionality to work. These books helped me to realize that there are general programming concepts that are wise to follow but that there are also language-specific concepts and tips one should learn and apply in the programming language of choice. General software development principles apply across many languages, but sometimes it is necessary to know language-specific features as well.

Effective Java, Editions 1 and 2

After such good experiences with Effective C++ and More Effective C++, I was excited to learn about the book called Effective Java as I was transitioning from C++ to Java, a transition several years in the making. Effective Java did not disappoint. I started reading Effective Java sooner in my Java experience than I had started reading the Scott Meyer's C++ books in my C++ experience and it helped me tremendously. It helped me to see from the beginning that there were more considerations when developing with Java than just writing syntax and making things work.

Java Cookbook

Although I have purchased and read seemingly countless Java-themed books over the years, I've never had a book focused on the Java language basics. Instead, I've always had specialized books focusing on aspects of Java or Java EE. The way that I learned the basics of the Java programming language was via the Java Cookbook. It helped me realize that I like this recipe-based approach to reading about, learning, and applying a new programming language. I've liked recipe-oriented books ever since, especially for quickly learning and apply a new programming language or framework.

Groovy Recipes: Greasing the Wheels of Java

Groovy Recipes: Greasing the Wheels of Java was my introduction to Groovy. This excellent book provides a nice introduction to Groovy that not only quickly introduces its syntax and features but also introduces practical ways that Groovy can be applied in regular everyday situations. I was able to quickly see the value of learning Groovy and to realize that Groovy was more than just a trendy language fad. This book reaffirmed my realization that I learn programming languages best from recipe-oriented books that demonstrate the language used in practical situations. Groovy Recipes: Greasing the Wheels of Java and the Groovy language itself have dramatically influenced my overall approach to software development by giving me the tools and confidence to do significantly more scripting than I used to do. Being able to script and develop software in similar languages (Groovy and Java) and use the same Java libraries and frameworks has made scripting easier. Easier scripting has meant that I've done more of it and spent less time doing it.

Holub on Patterns: Learning Design Patterns by Looking at Code

I purchased Holub on Patterns after seeing Allen Holub speak at SD West 2005 (I wasn't the only one). Holub, the author of Why Extends is Evil and Why Getters and Setters Are Evil, challenged my way of thinking (traditional and commonly accepted way of thinking at the time) about object-oriented programming. I definitely look at objects now more from a behavioral perspective than data perspective. Also, although willing to write get/set methods when necessary, my reluctance to write them until needed has been of great benefit as the era of multicores has helped us realize the dangers of mutable state.

Although Holub on Patterns does provide in-depth coverage of some common design patterns implemented in Java, the far bigger influence it had on me was in how I think about object-oriented design.

Expert One-on-One J2EE Design and Development

At a time when developers of enterprise Java applications were being told to use heavyweight EJBs and like it, many of us were wondering what we were missing when it seemed overly complex to us. Rod Johnson's J2EE Design and Development stuck a chord with me; it was relieving to see that I wasn't the only one who thought EJB 1.x was too difficult and cumbersome. More importantly, Johnson's book provided examples and discussion text that helped to see what parts of J2EE were worth using and which parts needed improvement. The Spring Framework was born out of the examples covered in this book and Spring's success inspired changes in Java EE that make it much more productive and useful. J2EE Design and Development influenced me personally and influenced an entire community. It also helped me to realize that one can be correct in thinking something isn't all it's said to be even if the so-called experts insist it is. This has helped me weather numerous fads (and their blindly adherent evangelists) in software development since then. The book also articulated better than I could my own thoughts and feelings on enterprise Java development.

Agile Web Development with Rails

During my brief infatuation with Ruby on Rails, I found Agile Web Development with Rails to be a great introduction to Ruby on Rails. Although I enjoyed my foray into Rails, I have never worked with it to the same degree as I have with Java. Agile Web Development with Rails introduced me to Ruby on Rails, but more importantly it influenced my career, which has mostly not been in Rails, by demonstrating in a practical way how agile development can be done if a client is readily available to provide feedback. Agile Web Development with Rails and the Ruby on Rails framework itself helped change my way of thinking to see the value of convention over configuration. Shortly thereafter, the Java-sphere began to see changes inspired by Ruby on Rails's success. These included Java Persistence API (JPA) adopting "configuration by exception" approach and the Grails framework. Ruby on Rails influenced much of the web development community and Agile Web Development with Rails was the tool that allowed me to most easily experience this influence personally.

The Practice of Programming

The Practice of Programming does not waste time with extraneous pages. The 250 page book covers the most fundamental issues in programming such as debugging, testing, performance, and portability. I read this early in my software development career and find myself reading portions of it again from time to time. Many great books have come along since that deal with these subjects, but The Practice of Programming was the first to cover most of these for me.

Software Craftsmanship: The New Imperative

Software Craftsmanship: The New Imperative was published at almost the perfect time for me in my career. The book helped me articulate my own observations about what made a "software engineer" more complete. In addition, this relatively short and to-the-point book helped me discover other approaches that I would later use in efforts to make my code more than just functional.

201 Principles of Software Development

201 Principles of Software Development features 201 short (typically one or two paragraphs) descriptions of 201 software development principles with a reference to a generally well-recognized expert source with further details on that principle. The text is comprehensive enough to understand the principle and the references to proven sources give the reader the ability to learn in more detail how to apply the covered principle. This book not only helped me to solidify these principles in my own mind, but helped me to understand the breadth of materials available to the software developer.

During my software development career, I've repeatedly seen trends come and go. Sometimes I'll hear a relatively new developer talking about some "new" thing that is really just a reincarnation of the same idea in different form that its previous incarnations. 201 Principles of Software Development helped me realize at a relatively early stage in my career that many concepts and principles that were "new" to me, were really not new to the greater software development industry. Most "innovative" concepts in our industry typically share common themes or are even grounded in one or more of the principles covered in this book.

Conclusion

The books covered in this post have had significant influence on my software development career and how I think about software development today. Numerous other books have had lesser degree of influence. In some cases, these other books might have had more influence had I encountered them at a different (often earlier) point in my career. In fact, that might be the point of this post: different books will have different impact on different developers depending on where those developers are in their careers. The books in this post have had particularly great influence on me, but I know that other developers have been significantly influenced by books such as Code Complete, Clean Code, and numerous other books. I was surprised at how many language-specific books were on this list as I compiled it, but each was really more about that language. These books are about how to apply the language to achieve more general software development ideals and that's where they have been most influential to me.

Saturday, August 2, 2014

Book Review: Arduino Home Automation Projects

I have never been as interested in playing with Arduino as many of my colleagues and friends such as Jon are, but I did readily accept Packt Publishing's offer to review the recently released Arduino Home Automation Projects by Marco Schwartz. The book's subtitle is "Automate your home using the powerful Arduino platform" and it is composed of seven chapters and just under 120 substantive pages.

Preface

The Preface of Arduino Home Automation Projects describes Arduino as "the perfect platform to build home automation systems." The Preface includes short descriptions of what each of the seven chapters covers. This section makes it clear that each of the seven chapters covers a separate Arduino project with the seventh chapter culminating in covering how to build one's "own home automation system based on Arduino."

Arduino Home Automation Projects's Preface also describes the general hardware and software needed for the projects covered in the book (unique specific requirements for each project are covered in the relevant chapter). These include Arduino Uno board, Arduino IDE, and integrated web server and database (Schwartz recommends EasyPHP for Windows, Serveur Web - LAMP for Linux, and MAMP for OS X).

The Preface also discusses "who this book is for" and states that readers "don't need to know anything about the Arduino platform beforehand," but do need "basic knowledge of electronics and programming."

Chapter 1: Building Wireless XBee Motion Detectors

Chapter 1 is on "building wireless XBee motion detectors" and emphasizes using XBee ("low-power radio modules" described on Wikipedia as "form factor compatible radio modules") and the ZigBee (IEEE 802.15.4) protocol for the communication portion.

Marco Schwartz, author of Arduino Home Automation Projects, introduces his aREST library in this initial chapter of his book (though aREST is used in later chapters and associated projects as well). The main aREST project page describes aREST as "a simple library that implements a REST API for Arduino" that "is designed to be universal and currently supports REST calls via HTTP, via the Serial Port, and Bluetooth Low Energy" (portions of the specifics on each of these are removed in my quotation of the description). The aREST library consists of a single C++ header file (aREST.h) and appears to be available for use under a GPL license (described in the comments in the aREST.h source).

The motion detectors project described in this first chapter also includes construction of a graphical interface using "HTML, CSS, JavaScript, and PHP." The author describes how these are to be demonstrated and used for this project: "We are going to use HTML and CSS to design the interface itself, JavaScript to handle the different elements of the interface and make the link with PHP, and finally use PHP to communicate directly with the XBee modules." The chapter finishes with detailed coverage of using these languages to implement this functionality.

Chapter 2: Control Lights from Your Phone or Tablet

The second chapter of Arduino Home Automation Projects demonstrates use of Wifi to "control lights directly from your computer, phone, or tablet." This project includes application of relays and a CC 3300 Wifi Chip. This chapter's project constructs a software user interface with the same languages used in Chapter 1's project and demonstrates use of Arduino code to test the hardware configuration before the user interface is available.

Chapter 3: Measuring the Temperature Using Bluetooth

Arduino Home Automation Projects's third chapter brings Bluetooth and Python into the discussion for use in the temperature measuring project that is the subject of that chapter. The author uses the Bluefruit EZ-Link (a Bluetooth serial link module), a DHT (Digital Humidity and Temperature) sensor (along with C-based DHT-sensor-library), and other components with the Arduino board to measure and report the temperature. Software needed for this project includes the just-mentioned DHT library as well as Python 2.7 and pySerial (a Python Serial library to access the serial port via Python).

Chapter 3 describes in detail the code needed to create an Arduino "sketch" to "test our sensor via Bluetooth." The chapter first demonstrates compiling and running the code from the Arduino IDE and then moves onto use of TkInter (described on its Wiki page as "a thin object-oriented layer on top of Tcl/Tk that is "Python's de-facto standard GUI package") to build an independent standalone executable user interface. This part of the chapter demonstrates how building of this Python-based interface code is similar to building the same interface testing code in Arduino. By the way, in case you're wondering, an Arduino "sketch" is defined as, "name that Arduino uses for a program" and "the unit of code that is uploaded to and run on an Arduino board" (Arduino Getting Started Guide).

Chapter 4: Weather Station in the Cloud with Xively

The fourth chapter of Arduino Home Automation Projects transitions from local monitor projects to a project that sends measurements from the Arduino board to the cloud. In this case, Xively is the destination in the clouds and the author describes Xively as "an online platform where you can send data from various devices such as Arduino." Schwartz points out that this is the first project in the book "linking home automation to the 'Internet of Things'" and describes "Internet of Things" as the "idea of having every device connected on the Web" that is "a major trend at the moment."

Chapter 4 uses hardware and software used in projects associated with the first three chapters and also introduces use of a photo cell. The chapter's introduction to Xively includes text description and screen snapshots demonstrating how to set up an Xively account.

Chapter 4 includes detailed coverage of the Arduino sketch for this weather station project and demonstrates testing of the project's connectivity to Xively. The chapter then illustrates with text and screen snapshots the rendering of the device's measurements on the device's dashboard presented on the Xively cloud via web page.

Chapter 5: Monitor Your Energy Consumption in the Cloud

Chapter 5 of Arduino Home Automation Projects builds directly on the preceding chapter to create a project that "sends energy consumption data" for a connected appliance "to the cloud." This chapter's project applies some of the hardware and software from previous chapters' projects and adds new hardware (AC712 current sensor).

Like Chapter 4's project, the Chapter 5 project uses Xively. Although this chapter references that chapter for Xively configuration instructions, most of the necessary instructions and screen snapshots are repeated again in Chapter 4. The chapter demonstrates writing and applying the Arduino sketch and seeing the results in the web as accessed from the Xively cloud.

Chapter 6: Hack a Commercial Home Automation Device

Arduino Home Automation Projects's sixth chapter represents another major transition. The prior five chapters focused on building home automation projects from scratch with Arduino while Chapter 6 instead focuses on "hacking" an existing automation device with Arduino. Early in this chapter, the author calls out a warning about this project being for informational purposes only and that the described hack should "not be used as such in your home for safety reasons."

Schwartz describes the characteristics of the device to be hacked that the readers should look for. The author also explains that he uses a TinyCircuits Arduino board in this project so that it is small enough to fit in the device chosen for the hacking and that Arduino Micro would have been another satisfactory option. The chapter details how to write the Arduino sketch to test this from the serial port and then how to develop a web-based interface with HTML and JavaScript to control it from a web browser.

Chapter 7: Build Your Own Home Automation System

The final chapter of Arduino Home Automation Projects starts with its stated goal: "to give you a step-by-step method to build your own home automation systems." The author points out that the nature and specifics of the example project associated with this Chapter 7 are less important than the general steps and principles covered.

Because Chapter 7 builds its example project from even more basic components than the first six chapters, the list of required hardware is longer than in most of those chapters. Specific resistors and capacitors are specified along with an "ATmega328 microcontroller with Arduino bootloader," a crystal clock, and a few other things. Software required for this chapter's example project includes JeeLib for Arduino IDE ("to enable low-power operation of your system"), EAGLE (to design the printed circuit board), and OpenSCAD (to design a 3D printed case).

With the needed software and hardware introduced, the final chapter looks at designing an Arduino system from scratch, designing a printed circuit board and ordering fabrication of that printed circuit board, and designing and order a 3D printed case. The "Summary" for Chapter 7 summarizes that chapter as previous chapters' summaries did for their respective chapters, but his final chapter's "Summary" also includes several paragraphs summarizing the entire book.

General Observations
  • The photographs in Arduino Home Automation Projects are sharp and clear, making it easier to visualize what is described in the book.
  • Hardware and software needed for each chapter's project is typically called out early in the respective chapter, but is sometimes not called out until a few pages into the chapter.
    • The author tends to describe the hardware and software in higher level and more general terms first and then to list out the specifics of what he used for each chapter's project
    • The author provides links to online sources of the same components or software he used when building each chapter's associated project.
    • Readers not comfortable with electronics or programming should probably get exactly the hardware Schwartz used for each project to give them the best chance of the hardware and provided code working together without any need to debug or change code or hardware configuration. Readers who are more comfortable with electronics and programming or are willing to take some time to debug potential conflicts between different hardware and the provided code might choose to use some of the alternate hardware Schwartz mentions in several of the book's projects.
  • The chapters in Arduino Home Automation Projects represent relatively independent projects, though there are some dependencies between chapters. For example, aREST is described in Chapter 1 but is used in several chapters and their associated projects. Commonly used hardware and software is often sufficiently covered in each chapter in which it is use with reference to its first introduction if even more detail is desired.
    • Several different libraries are installed in Arduino in the course of the seven chapters. In each case, the author explains in essentially the same sentence how to install libraries in Arduino: "To install an Arduino library, simply place the library folder in the /libraries folder of the main Arduino folder." This is an example of how the chapters are largely independent of one another because necessary background details are often mentioned again when needed.
  • The projects covered in Arduino Home Automation Projects are electronics projects and, in some cases, do carry risks of personal injury if the reader is not cautious. The author does a nice job of calling out and highlighting several of these situations.
  • The code examples are provided for download and the author details in text what the code's doing for the most important aspects. The reader of Arduino Home Automation Projects is reminded that this type of low-level programming is often at least a bit device-specific. The author carefully describes the exact devices he uses that the code examples are written for and tested against.
  • Anyone interested in the overall style of Arduino Home Automation Projects and whether they would like Schwartz's approach to writing can get a good taste of that style in Schwartz's online post Wireless Security Camera with the Arduino Yun that has a similar style and similar content to the chapters in this book.
  • The contents of Arduino Home Automation Projects made me feel that author Marco Schwartz is writing about something he knows about and is well prepared to write about. This is backed up by his online presence which includes his Google+ posts, his Open Home Automation blog, his YouTube videos, and his Twitter handle. He has authored Home Automation with Arduino (self-published with CreateSpace) and Internet of Things with the Arduino Yún (also Packt Publishing) in addition to Arduino Home Automation Projects.
Conclusion

Author Marco Schwartz combines high-definition photographs, clear diagrams, and sufficiently sharp screen snapshots with easy-to-follow text to describe seven projects in seven chapters of Arduino Home Automation Projects that apply Arduino to build home automation projects. Although a background in electronics and basic programming will certainly make it easier to read and understand this book, the full code is made available and the detailed instructions and helpful visual aids lead me to be believe that most teenagers and adults could follow along with the book and get the described projects working. I'm happy that I accepted Packt Publishing's offer to review this book as I found Schwartz's writing style to be engaging and easy to follow and thought that the visual aids were outstanding.