Wednesday, November 28, 2012

JDeveloper 11gR2 and Java 7

Hello all. For an ADF Essentials application I'm working on I wanted to use the great set of new features introduced by Java 7, however JDeveloper 11gR2 (11.1.2.3) is not certified for JDK 7 so I had to look for help and found some tricks that let you use the new JDK.

This is what you need to do:
  1. Download and Install the new JDK. You can find the latest release using this link.
  2. Add the new JDK to JDeveloper's libraries: Tools -> Manage Libraries.
  3. Configure JDeveloper to run using the new JDK.
  4. Configure the Integrated Weblogic server to run using the new JDK.

The first step is pretty simple. Just go to that location, download the latest release of Java 7 and install it on your machine. In my case, I didn't have to create any environment variable for the JAVA_HOME or PATH, but your case may be different.

Open JDeveloper and go to the menu Tools and then open Manage Libraries:



The Manage Libraries dialog appears, select the title Java SE Definitions and then click on the button New... 



The Create Java SE dialog appears. For me, the Browse... button didn't work when trying to get in the Program Files directory, so I opened Windows Explorer and went to new JDK installation directory (for me it was: C:\Program Files\java\jdk1.7.0_09) then I copied the path from Windows Explorer and pasted it on the Java SE Executable inputText. I also added  \bin\java.exe





Notice that after you complete the path to the Java SE executable (\bin\java.exe), Jdeveloper will load the rest of the information. Hit the OK button to finish.

Now, JDeveloper comes with its own JDK and we need to change it for the new one you just installed. Close JDeveloper, open Windows Explorer and go to {ORACLE_HOME}\jdeveloper\jdev\bin where {ORACLE_HOME} is the directory you used when installing JDeveloper. There you will find a file called jdev.conf open it with a text editor and look for the entry SetJavaHome on line 31 (line numbers may differ in other versions of Jdeveloper), comment it and create a new SetJavaHome entry below that one, but this time use the new JDK installation folder:




What we just did tells JDeveloper to start with the new JDK. Now we need to configure the Integrated Weblogic server so it starts with the new JDK as well.

We need to find the value of the -Dide.pref.dir.base entry as described here:
http://www.java-n-me.com/2012/11/jdeveloper-11gr2-wont-start.html

In my case the value of the entry is: C:\Users\{MY_USER}\AppData\Roaming, where {MY_USER} is my user name, you may need to enable the "show hidden files" property of the Windows Explorer in order to be able to locate that directory. There,  look for the JDeveloper\system11.x.x.x.. directory where the x's are numbers. Open DefaultDomain\bin directory and edit setDomainEnv.cmd using a text editor:



Comment the line 45 and below that line set the SUN_JAVA_HOME property to point to the new JDK. Repeat the same procedure for the JAVA_HOME  property found at line 55 (line numbers may differ in other versions of Jdeveloper):



That's it, now JDeveloper and its Integrated Weblogic Server will use the new JDK. Open JDeveloper and go to Help->About->Version and you will notice that it is using the new JDK:



As I didn't want to keep using the old JDK I removed it from the Tools->Manage Libraries menu option so my new JDK become the Default JDK and all my new projects will be using Java 7:



I hope this workaround works for you as it did for me.

see ya!


References:

Using WebLogic Server with JDK 7. Oracle [online].
Available on Internet: http://docs.oracle.com/cd/E23943_01/doc.1111/e14142/jdk7.htm
[accessed on Novemeber 25th 2012].

Nick Aiva's blog! [online].
Available on Internet: http://nickaiva.blogspot.com/2011/04/jdeveloper-11g-setting-up-newest-java.html
[accessed on Novemeber 25th 2012].

Sunday, November 25, 2012

ADF Essentials Hackathon at RightHandCorp

Hello all, last saturday (24-Nov-2012) we had a lot of fun with the RightHandCorp's development team in their first ever ADF Essentials hackathon. We built a proof of concept for one of their in-house web application and the result was more than satisfactory. They were surprised about how easy it is to build great good looking enterprise applications using this framework. Used JDeveloper 11gR2, ADF Business components,  ADF Faces, Oracle Database and Glassfish server.



We had a great time, lot of networking and knowledge sharing. Looking forward for the next one.

see ya!

Sunday, November 11, 2012

JDeveloper 11gR2 won't start

Hello all, I just wanted to share with you a workaround to "restart" your JDeveloper 11gR2 (11.1.2.3) if it is not starting. I struggle with this for several hours, until I found some documentation that helped me out.

My case
  • JDeveloepr 11gR2 (11.1.2.3)
  • OS = Win Vista Sp2 
  • RAM= 4GB
  • Installation by default (C:\Oracle\Middleware).

I'm developing an ADF Essentials application and after updating some extensions my JDeveloper installation stopped working. I did several reinstallations and registry cleaning but nothing worked, JDeveloper just didn't start and it freezed when "Restoring Editors":



Fortunately, I found this blog: http://adfhowto.blogspot.in/2011/03/troubleshooting-jdeveloper-will-not.html which gives a tip about the JDeveloper preferences directory. What it says is that you can delete or modify the name of the JDeveloper preferences directory, and JDeveloper will create another one with default preferences. Something you should be aware of is that this procedure deletes all your preferences: SVN, extension preferences, general preferences...etc. So it's up to you.

The Solution
I decided that this procedure fitted my needs and started to search for that directory. There is an easy way to find the preferences directory:
  • Open a DOS terminal (cmd).
  • Go to your JDeveloper installation directory (in my case C:\Oracle\Middleware\jdeveloper\).
  • We need to execute the file jdev.exe which is in {JDEV_INSTALLATION_DIR}\jdev\bin
  • Type this command: jdev.exe -verbose
The previous procedure will try to start JDeveloper and will show all the steps required to do so. Look for the -Dide.pref.dir.base entry:



The final step is: go to that location and rename or delete (rename is better) the directory system11.x.x.x... for something else. Restart JDeveloper and it will start and create a new directory with default preferences.

I hope this post helps so you don't have to spend several hours trying to put JDeveloper to work.

see ya!

Friday, November 9, 2012

Java 7: File Filtering using NIO.2 - Part 3

Hello all. This is Part 3 of the File Filtering using NIO.2 series. For those of you who haven't read Part 1 or Part 2, here's a recap.

NIO.2 is a new API for I/O operations included in the JDK since Java 7. With this new API, you can perform the same operations performed with java.io plus a lot of great functionalities such as: Accessing file metadata and watching for directory changes, among others. Obviously, the java.io package is not going to disappear because of backward compatibility, but we are encouraged to start using NIO.2 for our new I/O requirements. In this post, we are going to see how easy it is to filter the contents of a directory using this API. There are 3 ways in order to do so, we already reviewed two similar ways in Part 1 and Part 2, but now we are going to see a more powerful approach.

What you need
NetBeans 7+ or any other IDE that supports Java 7

Filtering content of a directory is a common task in some applications and NIO.2 makes it really easy. The classes and Interfaces we are going to use are described next:
  • java.nio.file.Path: Interface whose objects may represent files or directories in a file system. It's like the java.io.File but in NIO.2. Whatever I/O operation you want to perform, you need an instance of this interface.
  • java.nio.file.DirectoryStream: Interface whose objects iterate over the content of a directory.
  • java.nio.file.DirectoryStream.filter<T>: A nested interface whose objects decide whether an element in a directory should be filtered or not.
  • java.nio.file.Files: Class with static methods that operates on files, directories, etc.

The way we are going to filter the contents of a directory is by using objects that implement the java.nio.file.DirectoryStream.filter<T> interface. This interface declares only one method +accept(T):boolean which as the JavaDoc says: "returns true if the directory entry should be accepted". So it's up to you to implement this method and decide whether a directory entry should be accepted based on whatever attribute you want to use: by hidden, by size, by owner, by creation date, etc. This is important to remember, using this method you are no longer tied to filter only by name, you can use any other attribute.

If you only want directories, you can use the java.nio.file.Files class and its +isDirectory(Path, LinkOption...):boolean method when creating the filter:

//in a class...

    /**
     * Creates a filter for directories only
     * @return Object which implements DirectoryStream.Filter
     * interface and that accepts directories only.
     */
    public static DirectoryStream.Filter<Path> 
                                 getDirectoriesFilter() {

        DirectoryStream.Filter<Path> filter = 
                           new DirectoryStream.Filter<Path>() {

            @Override
            public boolean accept(Path entry) throws IOException 
            {
                return Files.isDirectory(entry);
            }
        };

        return filter;
    }


Or if you only want hidden files, you can use the java.nio.file.Files class and its +isHidden(Path):boolean method when creating the filter:

//in a class...

    /**
     * Creates a filter for hidden files only
     * @return Object which implements DirectoryStream.Filter
     * interface and that accepts hidden files only.
     */
    public static DirectoryStream.Filter<Path> 
                                 getHiddenFilesFilter() {

        DirectoryStream.Filter<Path> filter = 
                           new DirectoryStream.Filter<Path>() {

            @Override
            public boolean accept(Path entry) throws IOException 
            {
                return Files.isHidden(entry);
            }
        };

        return filter;
    }


Or if you want files belonging to a specific user, you have to ask for a user and compare it with the owner of the directory entry. To obtain the owner of a directory entry, you can use the java.nio.file.Files class and its +getOwner(Path, LinkOption...):UserPrincipal method (watch out, not all OS support this).
To obtain a specific user on the filesystem use the java.nio.file.FileSystem class and its +getUserPrincipalLookupService():

//in a class...

    /**
     * Creates a filter for owners
     * @return Object which implements DirectoryStream.Filter
     * interface and that accepts files that belongs to the 
     * owner passed as parameter.
     */
    public static DirectoryStream.Filter<Path> 
          getOwnersFilter(String ownerName) throws IOException{

        UserPrincipalLookupService lookup = FileSystems.getDefault().getUserPrincipalLookupService();

        final UserPrincipal me = 
                    lookup.lookupPrincipalByName(ownerName);

        DirectoryStream.Filter<Path> filter = 
                           new DirectoryStream.Filter<Path>() {

            @Override
            public boolean accept(Path entry) throws IOException 
            {
                return Files.getOwner(entry).equals(me);
            }
        };

        return filter;
    }


The following piece of code defines a method which scans a directory using any of the previous filters:

//in a class...
    
    /**
     * Scans the directory using the filter passed as parameter.
     * @param folder directory to scan
     * @param filter Object which decides whether a 
     * directory entry should be accepted
     */
    private static void scan(String folder
                           , DirectoryStream.Filter<Path> filter) 
    {
        //obtains the Images directory in the app directory
        Path dir = Paths.get(folder);
        //the Files class offers methods for validation
        if (!Files.exists(dir) || !Files.isDirectory(dir)) {
            System.out.println("No such directory!");
            return;
        }
        //validate the filter
        if (filter == null) {
            System.out.println("Please provide a filter.");
            return;
        }

        //Try with resources... so nice!
        try (DirectoryStream<Path> ds = 
                      Files.newDirectoryStream(dir, filter)) {
            //iterate over the filtered content of the directory 
            int count = 0;
            for (Path path : ds) {
                System.out.println(path.getFileName());
                count++;
            }
            System.out.println();
            System.out.printf(
                     "%d entries were accepted\n", count);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

We can execute the previous code passing the following parameters to the main method (check the source code at the end of this post):

  • Directory to scan: C:\ or / dependening on your OS. 
  • Filter: hidden

When executing the code we get the following:




In a windows machine, you can obtain the hidden files using the command: dir /AAH Notice that we are getting the same result:




And on my Linux virtual machine:




Using the command ls -ald .* we get similar results:



Again, Write once, run everywhere!

I hope you enjoyed the File Filtering using NIO.2 series. One last word, all the filtering methods we reviewed worked on one directory only, if you want to scan a complete tree of directories, you'll have to make use of the java.nio.file.SimpleFileVisitor class.


Click here to download the source code of this post.


See ya!

References:

Reese Richard and Reese Jennifer (2012). Java 7 New Features Cookbook. United Kingdom: Packt Publishing Ltd.

Thursday, November 1, 2012

Duke's Choice Awards 2012 LAD nomination extended

Just to let you know, the nominations for the Duke's Choice Awards 2012 LAD has been extended, the new final date is Novemeber 7th, 2012.

The program looks for the most compelling implementations of Java technology. Innovation is the only criteria. The nomination form can be found on the Duke's Choice Java.net site


Winners will be announced on stage during JavaOne LAD (Brasil) December 4th through 6th, and will also be featured in the January/February issue of Java Magazine. Winners will receive a free JavaOne LAD full conference pass and a Duke's Choice Award Statue.

So, you are in Latin America (LAD) and have an innovative Java project? what are you waiting for? Remember the end date for nominations is November 07th, 2012.

Don't you know if you got what it takes? Check the 2011 winners (when there was only one global program).


see ya!

Wednesday, October 31, 2012

Java 7: File Filtering using NIO.2 - Part 2

Hello all. This is Part 2 of the File Filtering using NIO.2 series. For those of you who haven't read Part 1, here's a recap.

NIO.2 is a new API for I/O operations included in the JDK since Java 7. With this new API, you can perform the same operations performed with java.io plus a lot of great functionalities such as: Accessing file metadata and watching for directory changes, among others. Obviously, the java.io package is not going to disappear because of backward compatibility, but we are encouraged to start using NIO.2 for our new I/O requirements. In this post, we are going to see how easy it is to filter the contents of a directory using this API. There are 3 ways in order to do so, we already review one way in Part 1 and now we are going to see  another approach.

What you need
NetBeans 7+ or any other IDE that supports Java 7

Filtering content of a directory is a common task in some applications and NIO.2 makes it really easy. The classes and Interfaces we are going to use are described next:
  • java.nio.file.Path: Interface whose objects may represent files or directories in a file system. It's like the java.io.File but in NIO.2. Whatever I/O operation you want to perform, you need an instance of this interface.
  • java.nio.file.PathMatcher: Interface that allows objects to perform match operations on paths.
  • java.nio.file.DirectoryStream: Interface whose objects iterate over the content of a directory.
  • java.nio.file.Files: Class with static methods that operates on files, directories, etc.

The way we are going to filter the contents of a directory is by using objects that implement the java.nio.file.PathMatcher interface. We can get one of these objects with the help of the java.nio.file.Files class, using the method +getPathMatcher(String):PathMatcher. This method supports both "glob" and "regex" patterns. You can check Part 1 of File Filtering using NIO.2 for more information about "glob" and for "regex" visit the java.util.regex.Pattern class. The pattern is matched against the name of the files, directories, etc. That live inside the directory. This is important to remember, using this method you can only filter by the name of the file, directory, etc.

For example, if you want to filter .png and .jpg images, you should use one of the following syntax and pattern (notice the colon between the syntax and the pattern):
  • "glob:*.{png,jpg}"
  • "regex:([^\s]+(\.(?i)(png|jpg))$)"

Of course, "glob" syntax is much simpler, but you have the option of using regular expressions for a more detailed match. Anyway, you may be wondering why you should use this approach if the java.nio.files.DirectoryStream interface allows you to filter directly using "glob"... Well, let's suppose that you already have a filter, but you need to perform more than one filtering operation, that's when you need to use this approach.

The following piece of code defines a method which scans a directory using different patterns:

//in a class...
    
    /**
     * Scans the directory using the patterns passed 
     * as parameters. 
     * Only 3 patterns will be used.
     * @param folder directory to scan
     * @param patterns The first pattern will be used
     * as the glob pattern for the DirectoryStream.     
     */
    private static void scan(String folder, String... patterns) {
        //obtains the Images directory in the app directory
        Path dir = Paths.get(folder);
        //the Files class offers methods for validation
        if (!Files.exists(dir) || !Files.isDirectory(dir)) {
            System.out.println("No such directory!");
            return;
        }
        //validate at least the glob pattern
        if (patterns == null || patterns.length < 1) {
            System.out.println(
                "Please provide at least the glob pattern.");
            return;
        }

        //obtain the objects that implements PathMatcher
        PathMatcher extraFilterOne = null;
        PathMatcher extraFilterTwo = null;
        if (patterns.length > 1 && patterns[1] != null) {
            extraFilterOne = FileSystems.getDefault().
                                 getPathMatcher(patterns[1]);
        }
        if (patterns.length > 2 && patterns[2] != null) {
            extraFilterTwo = FileSystems.getDefault().
                                 getPathMatcher(patterns[2]);
        }

        //Try with resources... so nice!
        try (DirectoryStream ds = 
                  Files.newDirectoryStream(dir, patterns[0])) {
            //iterate over the content of the directory and apply 
            //any other extra pattern
            int count = 0;
            for (Path path : ds) {
                System.out.println(
                          "Evaluating " + path.getFileName());

                if (extraFilterOne != null && 
                    extraFilterOne.matches(path.getFileName())) {
                    System.out.println(
                                  "Match found Do something!");
                }

                if (extraFilterTwo != null && 
                    extraFilterTwo.matches(path.getFileName())) {
                    System.out.println(
                             "Match found Do something else!");
                }

                count++;
            }
            System.out.println();
            System.out.printf(
                 "%d Files match the global pattern\n", count);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

You can try invoking the last method with the following parameters:

  • C:\Images or /Images depending on your OS.
  • ?_*.jpg This pattern specifies that you want all .jpg images whose name starts with one digit followed by an underscore.
  • glob:2_* Specifies another filter (using glob syntax) where you only want items whose name starts with the number two followed by an underscore.
  • glob:3_* Specifies another filter (using glob syntax) where you only want items whose name starts with the number three followed by an underscore.

Having several filters allows you to take different actions for matched items.
Following is the result of the execution on my windows machine:



And on my Linux virtual machine:



Again, Write once, run everywhere! However, notice that the ordering of the items is system dependent, so do not ever hardcode the position of a file or directory.

I hope you enjoyed this post, there is another more powerful way to filter the content of a directory and we'll explore it in Part 3.

Click here to download the source code.


See ya!

References:

Reese Richard and Reese Jennifer (2012). Java 7 New Features Cookbook. United Kingdom: Packt Publishing Ltd.

Saturday, October 27, 2012

Book Review: Java EE 6 Cookbook for Securing, Tuning, and Extending Enterprise Applications

Book cover from
http://www.packtpub.com
Java EE 6 is the latest specification for building Enterprise Applications with Java. It is a set of APIs that will simplify your development and if you come from Java EE 5, then you will notice the difference. Fortunately, Java EE 6 is been around for almost three years now and is widely adopted, which improves the quality of the documentation and training you may find about it.

In this post, I'll be reviewing the book "Java EE 6 Cookbook for Securing, Tuning, and Extending Enterprise Applications" written by Mick Knutson and published by PACKT. I have to say that this book is not for begginers in the Java EE field, you need some Java EE background in order to better understand the recipes. It is aimed at Java developers and programmers who want to secure, tune and, extend their Java EE applications

I liked the way chapter one updates you not only with the new features added to the specification, but also with the outgoing ones. It explains why some JSRs are pruned from this release and then it starts with the new: Context Dependency Injection (CDI), EJB 3.1, JPA 2.0, JAX-RS 1.1, Servlets 3.0, JSF 2.0, etc. Again, this is just a small review as cited on the book "... This chapter is not a tutorial or primer on the various specifications..." But is good enough to see what's new in Java EE 6. This chapter contains no recipes.

Chapter two dives into the implementation of some of the new features of the JPA 2.0 spec. The recipes are written in the form "Getting ready - How to do it - How it works - There's more..." which allows you to start using the new features very fast and avoid pitfalls. There's a tutorial in this chapter that helps you in profiling and testing JPA Operations, really useful.

Chapter three is about security using the Java EE built-in features and it also explains how to use Spring Security for a more fine-grained security implementation. This chapter is all about security, I wasn't expecting recipes about configuring Linux firewall rules or obfuscating Java byte-code... well done!

So far, so good. I haven't finished the book, but looking at the table of contents I can see really interesting chapters ahead:
  • Chapter 4: Enterprise Testing Strategies. Inlcuding Testing JPA with DBUnit,  Testing JAX-WS and JAX-RS with soupUI, among others.
  • Chapter 5: Extending Enterprise Applications. Inlcudes integrating Scala, Groovy and Jython into Enterprise Applications. I think this is an important chapter, specially because JSR 292 Invoke Dynamics is now part of the JDK 7.
  • Chapter 6: Enterprise Mobile Device Integration. Inlcudes an evaluation of Mobile-Web frameworks: jQuery Mobile, Sencha Touch and Modernizer. Really interesting and updated. There is also information about development and distribution considerations for iOS and Android.
  • Chapter 7: Deployment and Configuration. Inlcudes recipes that cover issues and solutions to application configuration with the use of Java Management Extensions (JMX).This chapter also cover tools to aid in  rapid and hot-deployment of Java EE applications such as Apache Ant and Apache Maven.
  • Chapter 8: Performance and Debugging. Inlcudes recipes that will help you in improving  the performance of your Enterprise applications with: jVisualVM, jstatd, among others.
There are so many recipes about topics I wasn't expecting that it really surprised me. After reviewing this book I realize that many of the topics will show you tools that you can leverage when tuning and extending your Java EE applications, so you can make the difference in your team. This is a must read if you are a architect/leadDevelper/consultant.

One more thing, the book is published in a lot of formats: printed book, kindle, PDF, ePub, so you have many options to read it. I have the kindle edition and it looks great on my kindle, paragraphs are well formatted and the source code is easy to read.

For more information about this book go to:
http://www.packtpub.com/java-ee6-securing-tuning-extending-enterprise-applications-cookbook/book

See ya!

Friday, October 19, 2012

Java 7: File Filtering using NIO.2 - Part 1

Hello all. NIO.2 is a new API for I/O operations included in the JDK since Java 7. With this new API, you can perform the same operations performed with java.io plus a lot of great functionalities such as: Accessing file metadata and watching for directory changes, among others. Obviously, the java.io package is not going to disappear because of backward compatibility, but we are encouraged to start using NIO.2 for our new I/O requirements. In this post, we are going to see how easy it is to filter the contents of a directory using this API. There are 3 ways in order to do so, that's why this post is Part 1.

What you need
NetBeans 7+ or any other IDE that supports Java 7
JDK 7+

Filtering content of a directory is a common task in some applications and NIO.2 makes it really easy. The classes and Interfaces we are going to use are described next:
  • java.nio.file.Path: Interface whose objects may represent files or directories in a file system. It's like the java.io.File but in NIO.2. Whatever I/O operation you want to perform, you need an instance of this interface.
  • java.nio.file.DirectoryStream: Interface whose objects iterate over the content of a directory.
  • java.nio.file.Files: Class with static methods that operates on files, directories, etc.

The way we are going to filter the contents of a directory is by using glob patterns, which are like regular expressions but simpler. The pattern is matched against the name of the files, directories, etc. That live inside the directory. This is important to remember, using this method you can only filter by the name of the file, directory, etc.
For more information about globbing, check this wiki. Also, there is some documentation about globbing patterns in the Java Docs.

So, let's suppose that we have a directory called Images, and we need to iterate over the files inside this directory, but we only need the .png files. We have to follow this steps in order to do so:
  1. Obtain a java.nio.file.Path instance which points to the directory Images.
  2. Open a new java.nio.file.DirectoryStream using the java.nio.file.Files class and passing the directory and the pattern (*.png) as parameters.
  3. Iterate over the content of the directory using the java.nio.file.DirectoryStream instance.
Next is the source code of a method that scans a directory using the pattern passed as parameter:

//in a class...
    
    /**
     * Scans the directory using the glob pattern passed 
     * as parameter. 
     * @param folder directory to scan
     * @param pattern glob pattern (filter)
     */
    private static void scan(String folder, String pattern) {
        //obtains the Images directory in the app directory
        Path dir = Paths.get(folder);
        //the Files class offers methods for validation
        if (!Files.exists(dir) || !Files.isDirectory(dir)) {
            System.out.println("No such directory!");
        }
        //Try with resources... so nice!
        try (DirectoryStream<path> ds = 
                    Files.newDirectoryStream(dir, pattern)) {
            //iterate over the content of the directory
            int count = 0;
            for (Path path : ds) {
                System.out.println(path.getFileName());
                count++;
            }
            System.out.println();
            System.out.printf("%d Files match the pattern"
                                                     , count);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }


Following is the result of the execution on my windows machine:



And on my Linux virtual machine:


That's something I love from Java, Write once, run everywhere! I hope you enjoyed this post, there are more ways to filter the content of a directory and we'll explore them in future posts.

Click here to download the complete source code.

See ya!

References:

Reese Richard and Reese Jennifer (2012). Java 7 New Features Cookbook. United Kingdom: Packt Publishing Ltd.

Sunday, October 14, 2012

Java 7: Meet the Fork/Join Framework

Free clip art from
http://www.freepik.com
JSR-166(y) is the official name of this new feature which is included in Java 7. If you notice there is a "y" in the name, this is because JSR-166 (Concurrency Utilities) is being added since Java 5, but it wont stop here as there are already plans to add new classes in Java 8 under the JSR-166(e). Check this page maintained by Doug Lea, the creator of JSR-166, for more information.

According to Wikipedia, Parallelism is the "simultaneous execution of some combination of multiple instances of programmed instructions and data on multiple processors" and Java has classes and interfaces to achieve this (sort of...) since DAY 1. You may know them as: java.lang.Thread, java.lang.Runnable, etc... What Concurrency Utilities (java.util.concurrent package) does is simplify the way we code concurrent tasks, so our code is much simpler and cleaner. As developers we haven't had to do anything when running our applications in machines with higher processing resources, obviously, the performance of our applications will improve, but are we really using the processing resources to the maximum? The answer is big NO.

This post will show you how the Fork/Join framework will help us in using the processing resources to the maximum when dealing with problems that can be divided into small problems and all the solutions to each one of those small problems produce the solution of the big problem (like recursion, divide and conquer).

What you need
NetBeans 7+ or any other IDE that supports Java 7
JDK 7+
Blur on an image, example from Oracle

The Basics
The Fork/Join framework focuses on using all the processing resources available in the machine to improve the performance of the applications. It was designed to simplify parallelism in Divide and Conquer algorithms. The magic behind the Fork/Join framework is its work-stealing algorithm in which work threads that are free steal tasks from other busy threads, so all threads are working at all times. Following are the basics you should know in order to start using the framework:
  • Fork means splitting the task into subtasks and work on them.
  • Join means merging the solution of every subtask into one general solution.
  • java.lang.Runtime use this class in order to obtain the number of processors available to the Java virtual machine. Use the method +availableProcessors():int in order to do so.
  • java.util.concurrent.ForkJoinPool Main class of the framework, is the one that implements the work-stealing algorithm and is responsible for running the tasks.
  • java.util.concurrent.ForkJoinTask Abstract class for the tasks that run in a java.util.concurrent.ForkJoinPool. Understand a task as a portion of the whole work, for example, if you need to to do something on an array, one task can work on positions 0 to n/2 and another task can work on positions (n/2) +1 to n-1, where n is the length of the array.
    • java.util.concurrent.RecursiveAction Subclass of the abstract task class, use it when you don't need the task to return a result, for example, when the task works on positions of an array, it doesn't return anything because it worked on the array. The method you should implement in order to do the job is compute():void, notice the void return.
    • java.util.concurrent.RecursiveTask Subclass of the abstract task class, use it when your tasks return a result. For example, when computing Fibonacci numbers, each task must return the number it computed in order to join them and obtain the general solution. The method you should implement in order to do the job is compute():V, where V is the type of return; for the Fibonacci example, V may be java.lang.Integer.

When using the framework, you should define a flag that indicates whether it is necessary to fork/join the tasks or whether you should compute the work directly. For example, when working on an array, you may specify that if the length of the array is bigger than 500_000_000 you should fork/join the tasks, otherwise, the array is small enough to compute directly. In essence, the algorithm you should follow is shown next:

if(the job is small enough)
{
   compute directly
}
else
{
   split the work in two pieces (fork)
   invoke the pieces and join the results (join)
}

OK, too much theory for now, let's review an example.

The Example
Blurring an image requires to work on every pixel of the image. If the image is big enough we are going to have a big array of pixels to work on and so we can use fork/join to work on them and use the processing resources to the maximum. You can download the source code from the Java™ Tutorials site.

Once you download the source code, open NetBeans IDE 7.x and create a new project:



Then select Java Project with Existing Sources from the Java category in the displayed pop-up window:



Select a name and a project folder and click Next >



Now, select the folder where you downloaded the source code for the Blur on an image example:



And select the file ForkBlur.java then click finish:



The source code will be imported and a new project will be created. Notice that the new project is shown with erros, this is because Java 7 is not enable for default:



To fix this, right click on the project name and select the option Properties. On the pop-up dialog, go to Libraries and select JDK 1.7 from the Java Platform ComboBox:



Now, go to the option Sources and select JDK 7 from the Source/Binary Format ComboBox:



Last but not least, increase the memory assigned to the virtual machine when running this application as we'll be accessing a 5 million positions array (or more). Go to the option Run and insert -Xms1024m -Xmx1024m on the VM Options TextBox:


Click OK and your project should be compiling with no errors. Now, we need to find an image bigger enough so we can have a large array to work on. After a while, I found some great images (around 150 MB) from planet Mars, thanks to the curiosity robot, you can download yours from here. Once you download the image, past it on the project's folder.

Before we run the example, we need to modify the source code in order to control when to run it using the Fork/Join framework. In the ForkBlur.java file, go to line 104 in order to change the name of the image that we are going to use:

//Change for the name of the image you pasted 
//on the project's folder.
String filename = "red-tulips.jpg";

Then, replace lines 130 to 136 with the following piece of code:

ForkBlur fb = new ForkBlur(src, 0, src.length, dst);
        boolean computeDirectly = true;

        long startTime = System.currentTimeMillis();
        if (computeDirectly) {
            fb.computeDirectly();
        } else {
            ForkJoinPool pool = new ForkJoinPool();
            pool.invoke(fb);
        }
        long endTime = System.currentTimeMillis();

Notice the computeDirectly flag. When true, we'll NOT be using the fork/Join Framework, instead we will compute the task directly. When false, the fork/join framework will be used.

The compute():void method in the ForkBlur class implements the fork/join algorithm. It's based on the length of the array, when the length of the array is bigger than 10_000, the task will be forked, otherwise, the task will be computed directly.

Following you can see my 2 processors when executing the Blur on an image example without using the Fork/Join framework (computeDirectly = true), it took about 14s to finish the work:



You can see that the processors are working, but not to the maximum. When using the Fork/Join framework (computeDirectly = false) you can see them working at 100% and it took almost 50% less time to finish the work:



This video shows the complete process:




I hope you can see how useful this framework is. Of course, you cannot use it all around your code, but whenever you have a task that can be divided into small tasks then you know who to call.

For more tutorials and information about concurrency in Java, check the Java™ Tutorials site.

see ya!


References:

The Java™ Tutorials. Oracle [online].
Available on Internet: http://docs.oracle.com/javase/tutorial/essential/concurrency/
[accessed on October 10 2012].

PHOTOJOURNAL. NASA Jet Propulsion Laboratory [online].
Available on Internet: http://photojournal.jpl.nasa.gov/catalog/PIA16101
[accessed on October 10 2012].

Monday, October 8, 2012

ADF Essentials

Hello all. Today I'm going to present you a great framework for bulding Rich Web applications with Java. It is called ADF Essentials and for those of you who haven't heard about it, let me tell you its benefits:
  • 150 Ajax-enabled JSF components.
  • Support for Skinning.
  • Visual and declarative development.
  • Free to develop and deploy.
  • Glassfish server officially certified, check the ADF Essentials product certification page.
  • Includes ADF Faces, ADF Data Visualization Tools (DVT), ADF Controller, ADF Model, ADF Share, and ADF Business Components. Check this page for a complete list of components included.
  • For more information and other benefits visit the ADF Essentials product home page.

The framework is built over JSF 2 so if you have previous experience in building Web applications using JSF you are likely to start developing with ADF Essentials pretty fast. On the contrary, if you are just beggining with Web development, the visual and declarative development this framework offers will let you build the application in a fast pace. Either case, there are tons of documentation online: books, tutorials and  videos to help you.

What you will need:

I've been using ADF Faces (which is the complete version of the framework) for the last 3 years and I can tell how easy it is to develop RIA (Rich Internet Applications) with it. In future posts I will show you how to deploy a Web application developed with ADF Essentials on Glassfish server and even on Tomcat (which is not yet officially supported).

Two things before we finish:
  • ADF Faces is the complete framework (which is free to develop but not free to deploy) and ADF Essentials is a subset of ADF Faces and is free to develop and deploy.
  • Check the following video from ADF Insider Essentials's channel on youtube so you can see the framework in action:




see ya!


References:

Oracle ADF Essentials. Oracle [online].
Available on Internet: http://www.oracle.com/technetwork/developer-tools/adf/overview/adfessentials-1719844.html
[accessed on October 06 2012].