Sunday, December 7, 2008

Java 5 Concurrency: The Executor framework

The Java platform has always provided support for multi-threaded/concurrent programming. However, prior to Java 5, the support was in the form of primitive constructs in the programming language itself. Java 5 steps up and provides concurrency utility frameworks and data structures in the java.util.concurrent package. One of the utilities provided is the task scheduling framework better known as the Executor framework. The JVM runs as a process and our application is one of the threads in the JVM. There are various other "system" threads running to do tasks like garbage collection, memory management etc. But from the application's perspective, there is the single "main" thread to begin with. The application, in itself, can spawn a number of threads to perform various helper tasks for various reasons like performance etc. Prior to Java 5 , spawning a new thread to perform a task was most commonly done as follows, although you could also do by extending the Thread class, but it is not highly recommended:

public void mainMethod() {
 HelperTask task = new HelperTask(); // Step 1: Create an object representing the task
 Thread t = new HelperThread(task);  // Step 2: Create a new thread for executing the task
 t.start();                        //  Step 3: Start the new thread
}

public class HelperTask implements Runnable {
public void run() {
      doHelperTask();
}
}
We have the following issues:
  1. Most of the code related to thread creation and task delegation to the thread is a part of the application itself. We need a way to abstract the above steps away from the application.
  2. Also what if we have multiple helper tasks or a scenario where every single user action requires a new Thread to be spawned to process? Creating a lot many threads with no bounds to the maximum threshold can cause out application to run out of memory.
  3. Secondly although threads are light-weight (but only as compared to the process) , creating them utilizes a lot of resources. In such a situation, having a ThreadPool is a better solution so that only fixed number of Threads are created and re-used later.
  4. Another short-coming of the Runnable interface's void run() method is that the task executed within run() has no way of returning any result back to the main thread. So work-arounds designed around that would be that the asynchronous task either updates certain database table(s) or some file(s) or some such external data structure(s) to communicate the result to the main thread.
The Executor framework addresses all the above issues and in addition also provides additional life-cycle management features for the threads. The Executor framework consists of the following important interfaces:

  1.  Callable: This interface is similar in concept to Runnable interface, ie it represents the asynchronous task to be executed. The only difference is that its call() method returns a value, ie the asynchronous task will be able to return a value once it is done executing. 
  2. Executor, ExecutorService and ScheduledExecutorService - Each of these interfaces adds more functionality to the previous one in thread and their life-cycle management. The Executor abstracts the Thread creation (as seen in Step 1 above) and executes all Runnable tasks. The ExecutorService extends the Executor and is able to execute Callable tasks in addition to Runnable tasks. It also contains life cycle management methods. The ScheduledExecutorService allows us to schedule the asynchronous tasks thereby adding support for delayed and periodic task execution. 
  3. Future: This interface represents the result of the asynchronous task which itself could be represented as Callable. The ExecutorService which can execute Callable tasks returns a Future object to return the result of the Callable task.

Thursday, November 13, 2008

Essentials of a Software Engineer

Sites like Digg usually have highest number of "diggs" on stuff like "Top 5/10/n" things on varied topics. Recently a co-worker sent me one such popular article about "Top 10 concepts that every software engineer should know". This was just in time as I was preparing to start this blog about the various techniques that would enable a Java engineer to be more pragmatic regardless of the kind of applications being developed and the myriad of technologies/frameworks being used . In my opinion, it is more essential to know these principles thoroughly and put them in practice before jumping to learning any new frameworks. Most of the frameworks - open source or otherwise - are usually the usage of the design principles in practice to solve commonly encountered problems. Good Object-Oriented(OO) design is the key principle here. As Rod Johnson points out here, "It's possible to design a [J2EE] application so badly that, even if it contains beautifully written Java code at an individual object level, it will still be deemed a failure. A [J2EE] application with an excellent overall design but poor implementation code will be an equally miserable failure. ... adherence to good OO principles brings real benefits. OO design is more important than any particular implementation technology (such as [J2EE], or even Java). Good programming practices and sound OO design underpin good [Java/J2EE] applications. Bad Java code is bad J2EE code."

Sunday, September 28, 2008

Overview of Web Service Stacks

There are currently the following open-source Java Web Service stacks in the market today.There could be more that I am unaware of, but at least the following contribute more significantly to the market share.
  1. Apache Axis1 and Apache Axis2
  2. Apache CXF
  3. Spring-WS
  4. Sun’s Metro available via Java EE 5 GlassFish container
  5. JbossWS
  6. XFire - which is now merged and transformed to being CXF.
This article is a comparative interview with each of the web service stack’s Principal Engineers and their vision for their product. http://www.infoq.com/articles/os-ws-stacks-background The second article is a comparison between Apache’s 3 products – Axis1, Axis2 and CXF. http://www.theserverside.com/tt/articles/article.tss?l=AxisAxis2andCXF Both of these articles are non-biased articles with no claims of one being better than the other. They leave it up to users to pick the one most matching their needs and demands. Sun has now published the standards for Web Services and it has now become imperative for all the Web Service stacks to provide implementations of those for conformance. Every web service stack provides the core functionality of:
  1. Providing an easy-to-use deployment option. 
  2. Correct Web service/operation invocation for the particular message(SOAP/REST). This further involves in the following order -
  • Receiving the message over the correct transport endpoint(Endpoint is an abstraction to represent URL and port),
  • Figure out the operation intended to perform based on the message, and then
  • Invoke the correct Java method mapping to the operation.
    3.  Pre-processing the messages via handlers to perform various functions like authentication, logging, custom processing etc.
    4. Marshaling/Unmarshaling mechanism of the request and response structures to/from Java<=>XML.
   5.  In addition, today's web service stacks are expected to provide implementations for the various WS-* standards that exist.

With respect to points # 2 and # 4 above, Mark Hansen has correctly pointed out:
The key technology for efficient SOA is efficient and accurate Java/XML mapping or more generically known as the OXM (Object-XML Mapping)as pointed out by Spring-WS. At the SOA level, system standards are specified using platform independent XML messages (SOAP/REST) and WSDL operations (which themselves are in XML). But at the language level (Java/C#/VB etc), the systems that are the real engines behind the functionality of SOA are implemented using objects and methods. The more seamless effortless and accurate OXM solution that a web service engine provides the more popular it will be.

Programming Java Web Services

For someone who is a total newbie to the world of Java Web Services(JWS), here's a list of the different JSRs/standards within the realm of JWS that are available with Java EE 5 and Java SE 6. They all have a particular purpose in the whole orchestration of JWS.
  1. JAX-WS 2.0 (Java API for Xml-Web Services) - specified by JSR 224.
  2. This was formerly JAX-RPC 1.1. JAX-RPC 1.1 was a standards-based implementation, but the binding and parsing layers underneath it were proprietary. When JAX-RPC 1.1 needed a major overhaul, the next JAX-RPC version would have been JAX-RPC 2.0 But the industry evolved more than just doing RPC-style web services. So to accommodate the message-style web services as well, which are becoming more and more common, "RPC" was dropped to become a more general JAX-WS 2.0
  3. JAXB 2.0 (Java API for Xml Binding) - specified by JSR 222.
  4. This specification defines and nominates JAXB as the default mechanism for serializing and de-serializing the XML messages contained within the SOAP message and Java objects. There are sure other ways to do this job via other Java-XML binding mechanisms - like XMLBeans, JiBX, Castor etc. But the JWS specs (defined by Sun) decided to make JAXB 2.0 as the "default" standard.
  5. WS-Metadata 2.0 (Web Services Metadata) - specified by JSR 181.
  6. Deploying Web services is quite a feat. One would normally require a set of deployment descriptors a la typical J2EE applications. But from Java EE 5 onwards, we now have web services-specific metadata annotations to achieve the deployment.
  7. WSEE 1.2 - Web Services for Java EE - specified by JSR 109.
  8. This defines the program model and run-time beahvior of Web Services in the Java EE container.
All these standards can be complex to understand at first, and after surfing a lot on the web for an in-depth perspective, I have finally landed on the book "SOA Using Java Web Services" by Mark D. Hansen.

Tuesday, August 12, 2008

Why do J2EE applications need Spring? The core of Spring explained...

What is the core of spring? The real purpose of why it came to being and is so popular. Most Java/J2EE application code tend to become monolithic (read a single large code base) over time - i.e. the application code slowly diminishes to maintain its modularized form with the result that the dependency relation graph between the classes/objects gets really ugly with no standard form. Secondly the applications also need a lot of infrastructure services - like Life Cycle Management of their objects and important resources like database connections etc, Database Access, Transaction Management and many more.Even simple Java applications would at least require efficient management of their objects and its dependencies. Although standard J2EE API and the application servers implementing those API claim to provide all the infrastructure services, applications still tend to become monolithic and unwieldy. So we need simplicity and freedom in the following areas:

1. Externally configurable objects - It is always better to be able to configure objects externally, i.e. set their values externally without recompilation of Java code or involving developers. The whole beauty of the general concept of configuration is that any change made to it does not require a rebuild or redeployment.

2. Simple POJO based programming - If we are just able to concentrate on programming business logic and not spend time of writing infrastructural code, we speed up the time to deliver the project by at least 40%. Simplicity and speed usually are natural if we adopt the POJO-based programming model. But for a real application, we have to add code to the POJOs to access databases, coordinate transactions, locate services provided by the container/environment, and so on. We would like these tasks to be performed declaratively via configuration (and via annotations), without disturbing the POJO code.

3. Dependency Resolution - Java interfaces do provide a clear separation between a contract and implementation. Ideally we would want the client class to be dependent only on the interface class. But we still have to look up the implementation class that we want to use. If it is hard-coded in Java code, we lose the advantage of interfaces, because the client class now depends on the interface class and the implementation class. Eg. Let's say we have an interface I and 2 concrete implementations of it - say C1 and C2. The whole idea of having interface is to shield client from the concrete classes at compile-time. But the client class still had to know about C1 at compile-time. But the client class still ends up having something like this:
I iobj1 = new C1();
iobj1.executeMethod();
We do have to be aware of C1, otherwise we would never be able to work without real implementations. But it is better to have awareness outside of Java code.

4. Lookup of Resources - A Java object A, say, depending on another Java resource B or a resource like Database C, should not be concerned with looking up for them. We again need a way to externally configure lookup code. So how does Spring help us?
  • Spring helps us achieve the above by wiring the dependencies between objects, all external to Java code.
  • Spring is essentially a technology dedicated to enabling you to build applications using POJOs.The components/objects are simple POJOs providing simple getters and setters. The container wires all the objects passing the dependent objects to POJO properties or constructors.Spring achieves the above using the Dependency Injection mechanism.
  • Most importantly, Spring itself is modular and has a layered architecture. This means we can use just the core JavaBeans configuration management without using the MVC framework or AOP support or DAO support. This means we can create modularized applications of any kind; so you are not restricted to creating web-based enterprise Java applications.
A more detailed discussion on what Spring does and does not can be found here.

Sunday, August 3, 2008

Program to an interface, not an implementation

In the introduction of the GoF book, the GoF mention an important principle of reusable object-oriented design. The principle is: "Program to an interface, not an implementation". The basic reason behind it is to achieve the flexibility in choosing different implementations at run-time as many times via the interface. This is achieved because of the polymorphism feature of the OO languages. The power of interfaces is best understood in design patterns. Most of the design patterns such as Strategy, State, Command, Abstract Factory Method are actually the above principle in practice. Now how do we try to abide by the principle? The best way is to strive for having method signatures with more generic, interface or parent class type method parameters and return types. Also it is advisable to declare class member variables to be of a more generic, interface or parent class type rather than a concrete implementation. The advantages of using interfaces (pure interfaces and/or abstract base classes) are as follows:
  • It is always painless to depend on interfaces. Client classes depending on the interfaces remain unaware of the concrete classes they use indirectly via the interface , as long as the concrete classes adhere to the interface that clients expect. This makes the clients more resilient to changes in implementation details.
  • The above aspect makes unit testing also very easy. In order to test a client class that depends on a complex computation exposed by an interface, we can create a mock stub object that implements the interface to replace the complex computation object.
  • Depending on interfaces has benefits even beyond just programming. The more we depend on concrete classes, the more complex dependency structures become, as a result our build scripts and build processes also tend to be more unwieldy.
Erich Gamma, one of the GoF, explains in an interview: "This principle is really about dependency relationships which have to be carefully managed in a large app. It's easy to add a dependency on a class. It's almost too easy; just add an import statement and modern Java IDEs like Eclipse even write this statement for you.Interestingly the inverse isn't that easy and getting rid of an unwanted dependency can be real refactoring work or even worse, block you from reusing the code in another context. For this reason you have to develop with open eyes when it comes to introducing dependencies. This principle tells us that depending on an interface is often beneficial."