Java’s Functional and Declarative Evolution

Java 8 is the largest change in the Java platform to date. One of the primary goals was to move the language from an imperative model to a declarative model. Before Java 8, software engineers wrote code that explicitly modified the state of the program and provided instruction on how all operations should be performed. Let’s discuss the evolution of Java development. Consider this example:

We have an Array collection with a bunch of numbers {1, 2, 2, 3, 5, 8} and need to find the “sum of all numbers.” Using Java 4 or lower (imperative) you have to loop through the array like this:

int sum=0;
for(int x=0; x<array.size(); x++){
// do the summation
 sum+=array[x];
}

This code explicitly loops through the array, one element at a time. Java 5 simplified things a bit, with the introduction of the more concise For loop:

int sum=0;
for(int x:array){
 sum+=x;
}

So no longer care about the length, we just loop through the collection. But if you compare this to the previous code, it’s still the same approach – we tell Java to explicitly loop through each and every element in the array. We still need the same amount of code to do it (minus some < and ++ operators) and readability is more or less the same. We wrote more logic just looping the array than our actual business logic.

We can also use the Iterator pattern like this:

int sum=0;
Iterator i = array.iterator();
while(i.hasNext()){
sum+=i.next();
}

If we compare all three options, they’re all similar amount of code and complexity. The iterator pattern actually introduces even more objects for the JVM to manage.

Onto Functional Style
With the introduction of Java 8, we get two things: Lambadas and the ‘forEach’ method on all Collections. ForEach provides us with a reference to all items in the collection. The implementation is specific to the type of collection. We just have to call the ForEach method. Java will execute and provide us the implementation. This is a classic example of polymorphism. Here’s how we use it:

int sum=0;
array.forEach(n -> {
//implementation here
sum+=n;
});

With this approach, we don’t care how the array is looped — there is no need to manage iteration values or indexes. ForEach will handle it for us and call the body of our function, for every element in the array (commented area). The code that is in the forEach: (n-> { … }) is lambda notation.

This lets us focus on implementing the solution to our problem (summing the contents of the array), rather than worrying about how to access our Array. It’s the job of the Java API to handle things it *already* knows how to do.

Compared to our Java 5 implementation, the functional style is easier to read, is less complex, and helps us focus on code that represents our business logic.

More Features
This is just an example of how Java 8 will change the way developers write code. I’ll talk about further improvements including the Stream / Map / Reduce / Predicate APIs in my next article.

Advertisements

Comparsion: Struts 2 vs Spring 3 MVC

Apache Struts 2 and Spring MVC are two of the most popular Java web frameworks today. I’ve used both APIs extensively and wanted to share a quick comparison.

In Struts, the object that handles a request and routes it for processing is called an Action. In Spring MVC, that object is referred to as a Controller. Actions and Controllers are pretty much the same thing – they take input, process it, and return some sort of response.

The one major design difference is that by default, Struts 2 Actions are newly instantiated every time a request is made, whereas in Spring MVC the default behavior is to act as a Singleton. Spring MVC Controllers are created once and held in memory/shared across all requests. Note, you can change this behavior (scope) to request or session but we’ll talk about that later. This is a major difference to keep in mind when designing applications that need to be thread-safe, database oriented, or other share-able transactions.

Struts 2 Actions:
The basic Struts 2 Action looks very similar to any normal Pojo but with an execute method:


public class TestAction{
   public String execute() {
       return "success";
   }
}

This method just returns the name of the result as defined in the struts.xml configuration file.
The struts framework will check the XML for the configured return path and handle forwarding to the proper view (usually a JSP page).

Spring MVC Controllers:
The basic Spring MVC controller only requires that you tag it with the @Controller annotation. Then you should use the @RequestMapping annotation to specify what URL patterns the controller will respond to. You can use this annotation at the class level or method level or a combination to achieve your needs.


@Controller	// Controllers are singletons by default
@RequestMapping(value = "/actions")
//@Scope("request")	// This will make spring instantiate a new object for every request
public class ControllerTest {
  
  @RequestMapping(value="/index", method=RequestMethod.GET)
  public ModelAndView index(ModelMap model) {
    ModelAndView mv = new ModelAndView("index", "model", model);
  }
}

The above example shows the index method that would respond to a GET request in this URL Format: /actions/index. We could easily change the RequestMethod to POST and it would ONLY respond to POST requests.

Struts 2 Configs:
As you can see, the MVC concept is pretty similar between both frameworks. Now, lets compare the configurations required.

Struts 2 only requires a single ‘struts.xml’ config file to configure the framework. A typical action/result routing configuration looks like this:

<action name="TestAction" class="com.TestAction">  
            <result name="success">/pages/success.jsp</result>
            <result name="input">/pages/input.jsp</result>
            <result name="fail">/pages/failure.jsp</result>
</action>

In the example above, we returned the “success” result. So this Struts 2 would forward us to success.jsp as the view.

Spring MVC Config:
Spring MVC requires a ‘servlet-context.xml’ file. As seen above, the actual result routing/configuration is entirely completed within the Controller classes via annotations. Here’s snippetts from the file:


<!-- Configures the @Controller and annotation programming model -->
	<mvc:annotation-driven />

<!-- Forwards requests to the "/" resource to the "welcome" view. Use this to setup a default action -->
	<mvc:view-controller path="/" view-name="welcome"/>

<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"/>
		<property name="suffix" value=".jsp"/>
	</bean>

The last bean entry is the most important. In our above example we returned the “index” model and view. Spring will automatically prefix and suffix the view resulting in “/WEB-INF/views/index.jsp” as the JSP that handles the request. Of course, you can configure the path to whatever you desire.

You can find a fully working servlet-context file anywhere on the internet or you can contact me and I am happy to provide you an example.

Note, most development teams will then choose to deploy the full Spring architecture and have the Core Application Context enabled as well. For example, this would be necessary if you wanted to have Spring manage your Data Layer via JPA / EntityManager. Or if you wanted to have Spring manage and inject your Beans across your application.

Summary:

Either framework is a good choice. I suggest reviewing the exact needs of your application before choosing a Framework. I’m available to help if there is any need!

Thanks for reading.

Netbeans Rant

Netbeans is an awesome IDE (my fav one), but one thing that annoys me is the lack of support for JAR files that depend on Native Libraries. Java Native Access allows Java APIs access to functionality bundled in a DLL. With JNA you can call directly into a DLL and access its methods. Most often used for integrating with C/C++ code.

One way to set this up is to include a MANIFEST.MF file in your JAR. You add a line like this:

Bundle-NativeCode: [Directory to the DLLs] 

Now you can distribute the JAR and required DLLs easily. With Eclipse this is easy – you simply add the JAR to your Project Build Library. Then expand the node and configure the “Native Library location” option to point to your DLL(s).

However, in Netbeans if you try to add a JAR that has DLLs specified, the IDE immediately crashes and throws a “Class not found” error. As far as I can tell – there is no way at all to setup a Native Library JAR file in Netbeans. If anyone has done this please let me know how! 🙂

Your options are:
1. Use Eclipse
2. Add the DLL path to the Java executable like so:

java -Djava.library.path=[path to native lib folder]

Usually adding the DLL(s) path to the PATH environment variable does the trick. Then go into the MANIFEST.MF file and remove the line for “Bundle-NativeCode”. Finally, add the JAR in Netbeans and it will work!

Create a MD5 Hash with no 3rd Party Libraries

I generally like to stay with built in Java libraries unless a 3rd party API offers significantly better functionality. An example I recently came across is creating MD5 Hashes in Java. There are many APIs available (Fast, Apache Commons) and they are probably a little faster than Java’s own libraries. However – staying with the Java 6 API was my goal.

I used the java.security.MessageDigest class and some helper methods. Check out the code below to generate a MD5 hash and represent it in Hex/String format.

It’s simple, just pass in the string you want to Hash. SALT is a secret string saved somewhere else:

public String generateMD5Hash(String function){

        MessageDigest digest= null;
        try {
            digest = java.security.MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }
        digest.update((SALT+function).getBytes());

        byte[] messageDigest = digest.digest();

        String md5 = toHex(messageDigest).toString();

        System.out.println(md5);

        return md5;       
    }

And the toHex function here:

public static String toHex(byte[] bytes) {
        BigInteger bi = new BigInteger(1, bytes);
        return String.format("%0" + (bytes.length << 1) + "x", bi);
    }

Forrester Says Java is a Dead-End – Um, what??

I just read a post by Mike on Forrester’s Blog titled “Java Is A Dead-End For Enterprise App Development”.  While some of his points are valid, I disagree with his reasons. Here’s why:

  1. Mike says “Java was at the right place at the right time.”
    • I don’t see how this leads to dead ends. Having the right product at the right time is how successful businesses are made.
  2. Then he goes on to point out various deficiencies in the Java platform and why the industry should “move forward”
    • Java is a 20 year old language based on C++” – I fail to see how this is relevant to his argument.  Java has evolved through 6 major revisions, approaching 7 very soon.  Its true the focus has moved all over the place but I think most people will agree that the API is in the best state yet. Think about  Spanish and French, which are hundreds of years old, based on Latin. Does that make them obsolete? Additionally, his facts aren’t correct. Java was released in 1995 making it 15 years old.
  3. “Java frameworks prove complexity” – Mike says that popular frameworks (Hibernate, Spring, etc) cause complexity. 3rd party APIs do add complexity – thats true for any language. But take a look at the purpose of these frameworks. They solve problems that are common to all n-tiered projects by providing a common framework. The community has proven and accepted them and developers can use the same framework across projects. Imagine if these frameworks didn’t exist. Should Oracle buy out these technologies and bundle them into the JDK – Thats another discussion.

Don’t get me wrong- Java is not a perfect language/platform and I do agree with this point:

  1. “Java bungled the presentation layer” – I don’t think its so much as bungled, but Java never really focused on providing pretty presentation. I agree that the desktop APIs have really sucked. But on the web, Java provides a way to send data to the presentation and leaves the actual pretty stuff up to 3rd party tools.

Its pretty clear to me that Mike wants Java to be one big bundled PRODUCT. Why? Because thats what Forrester does – they review products. They compare Java to BPM and other COTS products. That’s not even a valid comparison.

They want the Java platform to do everything and be a one stop shop for development. On the contrary, that’s not what Java is about. The Java concept is: Openness and Flexibility for framework choice.   Not everyone needs or wants to be tied into an entire platform when they only need a small piece of it …

Google Translate Recap

I’ve been implementing a Java API that can interact with Google Translate for integration into a 3rd party application. I wanted to share some thoughts on the process and see if anyone can provide some feedback.

  1. As far as I can tell, Google does not provide a prebuilt API for interacting with the service
  2. Google expects you to query the service via simple URL parameters and parse a JSON response
  3. All you need to do is Post a request to the following URL: https://ajax.googleapis.com/ajax/services/language/translate?v=1.0
  4. Then attach 2 parameters with their values: q (The text to translate) and langpair (The languages to and from, separated by a | character)

An example URL looks like this: https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=hello&langpair=en|fr. Building the Java API was actually pretty easy.

  1. We used the Java class: HttpURLConnection to handle the actual URL query and response
  2. Google responds with a JSON encoded string, so we needed an API to parse and create Java Objects out of the string. I chose GSON because I figured it would work best to use Google’s own products.
  3. The JSON returned by Google looks something like this:

         "responseData": {
              "translatedText": "Hola, mi amigo!"
         },
         "responseDetails": null,
         "responseStatus": 200
    }
    
  4. Next, you need to turn the JSON string into GSON / Java objects. This process is called deserialization. A pretty simple example from my API is below:
  5. JSONResponse data = new Gson().fromJson(rawJSONString, JSONResponse .class);
    

    The JSONResponse object is a custom POJO that will hold the data from the JSON string. Gson is the Google implementation of JSON and you simply call the fromJson() function to deserialize the JSON string, rawJSONString, and construct the object.

  6. Once you have the final GSON object, you can treat it like any Java POJO and ask it for properties. Note, we could have simply proceeded without the String -> GSON object step and parsed the String. However, that approach can get complex and can be very tedious.

Overall, I found the process really easy and quick to implement. I’m not sure why Google doesn’t just provide a downloadable API. Maybe its because they want to push the lightweight REST and JSON formats or remain language agnostic? Most likely, I think they meant the service to be used in a Javascript/Ajax environment and embedded into a Web application.

Contact me if you would like to know more or have ideas on a better way to do this 🙂

You can check out their online documentation for more information.

Improving Web Application Performance

Regardless of scale or number of users – it is important to design your application with Performance in mind.

In general, there are 3 tiers on any Web Application:  the Presentation, the Middle, and the Backend layer. Requests on your web application can flow through 1 or more of the layers.  The more layers that are involved in a request, the slower your application response time becomes.

There are only 2 ways to speed up web applications: Throw more Hardware at it or  Design your Software better. Both are viable solutions and probably should be used in conjunction. You can simply buy more hardware, load balance the servers, and give yourself more load capability. However, its not cheap and has limited effectiveness. I’ll focus on software solutions here.

Lets consider this scenario: I access a web application; pretend the page simply has 2 dynamic dropdowns populated via database records. The server accepts your request, processes it in the middle layer, determines that 2 dynamic dropdowns need to be populated, issues 2 separate Database queries, returns the results, and renders the entire page to the user. Perfect right? Lets break this down:

  • Number of layers hit:  all 3
  • Number of database accesses: 2
  • Total response time =  Cost of 2 DB accesses + Cost to progress through all 3 layers
  • Now add some business logic at the middle tier that asks some data source (maybe a webservice) for a restaurant’s menu items. Throw a few more dynamic dropdowns, images, and dynamic content on the page. Finally, multiply your server load by 100 to simulate a production environment. We’re looking at exponential increases in response time. How do we improve on this?

    The strategy is to minimize the number of layers that are involved in processing each request.

    The first option to consider is to Cache content that comes from the database. You can’t cache everything but anything that has some degree of consistency can be stored in memory. You still cannot avoid the first request for the item, but you can use the Cache to return data for subsequent requests

    • In our example, I would consider our dropdown values to be a good candidate to Cache. Assuming we cached both sets of dropdowns in memory during the first request, any subsequent request for the page would result in this breakdown:
      • Number of layers hit:  Presentation & Middle (2)
      • Number of database accesses: 0
      • Total response time =  Cost to progress through 2 layers
    • In general, a database transaction is the slowest point during request processing – so we have effectively taken out the “weakest link”
    • You can implement your own Cache if you require a high degree of customization, but I would suggest using an industry proven framework. Two Java based frameworks I have used and highly recommend are JCS Cache and OSCache. They are extremely simple to use and you can even Cache entire pages that are static.

    Your second option is to separate your Content and Assets from your Application. By this I mean your Application Server should not be serving your media or content assets. It is not tuned for large file distribution or media streaming.

    • Host your Web Application on its standard Server (JBoss, Tomcat, IIS, etc)
    • Host your media and large files seperately on a content server (Apache or a CDN network like Akamai)

    This will allow the Application server to do its job – deliver application logic not stream files.

    Your third option is to access the Database Properly, when necessary.

    • All accesses to the Database should be via Stored/Compiled procedures
    • This serves a similar purpose as Application Caching. It allows the DB Server to cache and pre-compile the SQL Query

    Finally, design your HTML so it is compact. Avoid repetitive inline CSS. For example:

    <div><font color=”red”>First Name</font></div>
    <div><font color=”red”>Last Name</font></div>
    <div><font color=”red”>Phone</font></div>

    This should be consolidated in single CSS class and HTML written as such

    <div class=”red”>First Name</div>
    <div class=”red”>Last Name</div>
    <div class=”red”>Phone</div>

    There are many other common tweaks such as using web friendly graphics, smaller/minified CSS, and pre-compiling any server side pages (JSPs or ASPXs). You can even be proactive and use Firebug to track the slowest / largest assets on your pages:

    If you need help tuning your applications or have questions with things discussed here please contact me!

    How to Interview Candidates for Software Jobs

    Everyone has been to Interviews. You can expect to be grilled on various topics – thats a given.
    Most interviews I’ve seen tend to send you through 4-6 different people – 1 by 1, as a panel, or paired up. There’s the Technical interview, the Culture fit, and some Q&A time at the end (sometimes the beginning).

    However, have you ever thought how effective your interview team actually was at determining good fit for the job? How many times have you answered the same question? Does the team actually have a plan or do they just do their own “mini interviews”? If you are interviewing a candidate for a Software related position, heres some things that you really need to consider:

    First, the most important thing you have to determine is:  How well the candidate can actually write code.

    The best way to do that is to make them WRITE code. Give them a simple question that any Software Engineer can solve quickly. Ask them to write the code on paper. It doesn’t need to be perfect. You aren’t looking for syntax – thats why IDEs exist.  For example:

    • Count the number of a ‘*’ characters in a string
    • Reverse a string
    • Find the max value in an array

    If they can’t write simple code, move onto the next candidate. That simple.

    The second thing you need to determine is: Do you want to work with this person, everyday?

    • This is the culture fit most people encounter and is the hardest part of your job. Everyone does this differently, whether its taking them out to lunch or discussing interests outside of the job
    • Software engineers encounter new APIs or Products every single day. Its your job to determine how a candidate deals with learning new technology. Ask them how they learned something on their resume.

    The last thing you want is a candidate who can’t do their own research or needs constant guidance.

    Third, Do not ask brain teasers or pointless questions. Why? Because they really are useless & wastes time.

    • Anything that involves knowing a formula or some weird piece of information to solve the problem is not going to get you any useful information.
    • Brain teasers can be answered if you’ve seen the question before, which reveals nothing about your candidate.

    Lastly, you need a way to separate the GOOD candidates from the BEST.

    • For experienced Software Engineers, I like to present the candidate with a small code snippet and have them walk me through it. I’m looking to hear how the code works, what the output is, and the execution order on the code.

    Being able to explain OTHER people’s code is a great indicator of a good Software Engineer.

    I would love to hear your thoughts on interview tactics – what works? what doesn’t?

    What is Service Oriented Architcture? SOA?

    The term Service Oriented Architecture has become one of the hottest “buzzwords” in the past couple of years. Yet, it’s astonishing the number of people I meet that cannot properly define or describe SOA. In the Government Software field, everyone is talking about integrating data sources and collaboration between agencies. The term “SOA” is practically used on every engagement.

    There are so many different definitions for SOA and I think most of them are much too specific and rely too much on Web Service/XML technology. For example, JavaWorld defines SOA based services as “self-describing interfaces in platform-independent XML documents” specifically using WSDL technology. This a well written article but I don’t agree with their specific definition of SOA. While Web Services are the most common example of a SOA platform, I do not believe that XML is a requirement to be classified as SOA.

    Personally, I think in order for an application to be classified as having a Service Oriented Architecture, it must meet 3 conditions:

    – The application must provide a service that is designed to be integrated into more than a single larger application and by itself, the application does not provide significant value

    – The application must provide a means of integrating with it that is Platform & Language independent

    – The application should not require a 3rd party consumer to install any additional software to use its services

    I think most people will agree that a XML Based Web Service that provides Stock Quotes can probably be classified as SOA.

    On the other hand a Flash based Application, requires a client to install a Platform specific plugin should disqualify it from SOA classification.

    Now, consider a Java Servlet that provides Stock Quotes that’s accessible via simple HTTP POST.  I believe that this can be classified as SOA — but many IT professionals will disagree with me. It meets all 3 criteria that I mentioned above.  I can easily build an application that consumes this service, regardless of Platform or Language.  There is no 3rd party software required to use make a simple HTTP POST or GET request.  Sure – the data that is sent is not in XML format, but HTTP Post is still a universal method of sending data across the web.

    I’m not arguing that something like this should be implemented in this fashion, but I think that the SOAP & XML Wrapper around many things is not necessary to provide a “Service.”

    What do you consider a SOA? What are some requirements for being “Service Oriented”?