Tuesday, January 27, 2015

How to use Future and FutureTask in Java Concurrency with Example

Future and FutureTask in Java allows you to write asynchronous code. Future is a general concurrency abstraction, also known as promise, which promises to return a result in future. In asynchronous programming, main thread doesn't wait for any task to finished, rather it hand over the task to workers and move on. One way of aynchronous processing is using callback methods. Future is another way to write asynchronous code. By using Future and FutureTask, you can write method which does long computation but return immediately. Those method, instead of returning result, return a Future object. You can later get result by calling Future.get() method, which will return object of type T, where T is what Future object is holding . One example of Future is submit() method of ExecutorService, which immediately return a Future object. By the way, Future and FutureTask are available in java.util.concurreent package from Java 1.5. Also, Future is and interface and FutureTask is an implementation or RunnableFuture, which can be used as Runnable interface, thus, can be passed to ExecutorService. In this Java concurrency tutorial, we will learn how to use Future and FutureTask in Java.

Monday, January 26, 2015

What is difference between Maven, ANT, Jenkins and Hudson?

In short, though Maven and ANT are build tool but main difference is that maven also provides dependency management, standard project layout and project management. On difference between Maven, ANT and Jenkins, later is a continuous integration tool which is much more than build tool. You can setup your CI environment using Jenkins or Hudson and automatically build, test and deploy your Java project. Now last, main difference between Jenkins and Hudson, both are originate from same source code but one is closed source while other is open source. You can read the details in this article. Now let's start long story, what is difference between Maven and ANT or difference between Maven and Jenkins, or Maven vs Hudson are some of the frequently discussed questions among Java when developers. Well all four e.g.  ANT, Maven, Jenkins and Hudson are tools to help Java developers on build, unit testing, continues integration (CI) and project management. In this Java article we will explore each of Maven, ANT, Jenkins and Hudson to get basic idea of what they are, what benefit they offer and how they are used in Java JEE projects. Just to give you basic idea, ANT is a well known build tool, probably oldest among all. A build tool is used to create deliverable like JAR file or WAR file from Java source and resources for deployment.

Maven came after ANT and offers much more than a build tool. Main difference between ANT and Maven is that In ANT you need to define every thing i.e. source directory, build directory, target directory etc while Maven adopts principle of Convention over configuration. Which means Maven has predefined project structure i.e. standard directory for source files, test files and resources. On the other hand, Jenkins and Hudson are Continues Integration tool, which gives you power to automate your build and deployment process. By using Jenkins or Hudson you can trigger build whenever developer commit code, to see if project is compiling fine, to run unit tests, to create build or even deploy in QA or production environment. Similarly you can have daily build, nightly build or weekly build process established in Jenkins or Hudson. In next section we will understand Maven, ANT, Jenkins and Hudson in more detail and understand difference between them.

Friday, January 23, 2015

Solaris Command to Show Long argument list of a Running Process

One of the problem with ps command, which is a popular tool to find any processes along with grep command in Solaris operating system is that it doesn't show full command line argument of process. This means if you are doing grep on any text which appear at tail end of long command line, you will likely not able to capture that process by using ps and grep. This is dangerous because it may lead you to assume that certain process is not running and you may restart it again, despite its being running and only because you didn't find the process. This is quite common while running Java process in any UNIX operating system e.g. Solaris, Linux, IBM AIX etc because Java process usually has long list of arguments e.g. system properties, JVM options,  and name of main class. I have faced this problem quite few time even after using ps -ef  option. Until now I have not found any compelling solution for this problem which will work on all UNIX systems, except for Solaris, which  I am going to share with you guys. So, my only advice is to put the keyword in head of long command line argument and try to keep Java command line as small as possible. Also prefer using ps -auxww over ps -ef,  former better display long command line than later and you have higher chance of capturing the process by using grep with tail keywords.