Saturday, 26 May 2012

Service Loader, a new feature in Java

What is Service loader in Java?

Service Loader is a new class added in Java 1.6 and it provides good means of developing loosely coupled application.
It allows us to link an interface to a particular implementation at run time based on configuration thus providing us with loose coupling
It also provides a sort of Dependency Injection (DI) though not as powerful as Spring framework but does provide a good facility in case you don’t require a full blown DI feature.
Also various design patterns like Strategy can be easily done with the help of Service Loader.

What is the Logic behind Service Loader?

Each interfaces present in the application is called service since they define what can be done but not how it can be done.
A single interface can have multiple implementations and each implementation is called service provider
Service Loader makes use of both the above concepts and to link a particular implementation with interface i.e. link a particular service with a service provider makes use of provider configuration file

Provider configuration file:

This file contains mapping of interface to implementation and is present in the resource directory META-INF/services
Within this file, contains a list of fully-qualified binary names of concrete provider classes, one per line and is compliant with UTF -8

Not clear yet, See example to make things crisp and clear

Let consider Account Interface as shown, We can see it has following methods
  • getType()
  • setType(String type)
  • getBalance()
  • getBranch()
  • setBalance(int balance)
  • setBranch(String branch)
Let say we have two implementing classes SavingAccount and CurrentAccount

We will create this example using Eclipse IDE. Following are the steps: -

1) Create a Java Project as shown. Give project name as ServiceLoaderEg

Figure: -

Figure: -

2) Create new package

Figure: -

3) Create interface Account within the above created package with discussed method signatures

Figure: -

4) Create class SavingAccount which implements Account. The implementation is shown

Figure: -

5) Similarly CurrentAccount

Figure: -

6) Create a folder META-INF within the project and within it create services folder. Within this create a file and name of the file should match fully qualified name of Account interface i.e.

Figure: -

7) Within the file write the name of implementation you want to use . In the above example we have used the implementation as CurrentAccount. Again fully qualified name of CurrentAccount is needed to be put within the file. Same is shown in above figure

8) Create a main class called Main and within it write the following code

Figure: -

As we see in the screen shot we are making use of static load method of ServiceLoader and we have passed the class as interface type i.e. Account. The act of mapping this interface to corresponding implementation is done via the file which we created with META-INF/services folder

9) On running the above code we get following output

Figure: -

As we see from the screen shot the CurrentAccount implementation is used which is evident by the Sys out method printed and the type of Account

Note: Before running the example please copy META-INF folder completely within bin folder of eclipse
10) From the above example we see that the invoking class is not dependant directly on the Implementation and just depends on interface and the mapping between them is done by using ServiceLoader. Thus we achieve loosely coupled application

Application areas:

Many times when development happens cross team it is quite possible the team may have dependency on the code each other develop. So some classes if not completely developed can affect the dead lines of project as well testing. To do up with this we can create Mock objects corresponding to each classes which has not been fully developed and the mapping can be done by using ServiceLoader. Once the class is completely developed we can replace the Mock object with actual implementation via again Service Loader Thus we achieve Loosely coupled application as well Dependency Injection.

Download Source Code for Service Loader in Java.

Video on Service Loader:

Also see the following video which will clear your picture more on Service Loader.

More articles and videos coming:

I am going to write more articles as well as record more videos on
Java interview questions and new rich features like concurrent collections. Semaphores, Executors and so on in coming time. So stay tuned

Thursday, 17 May 2012

Java Training: -Show JDBC (Java Database Connectivity) and its different section?

JAVA interacts with database using a common database application programming interface called as JDBC. JDBC allows a developer to write applications that is database independent. So the developer will be interacting with JDBC API rather than getting in to complications of the database.JDBC allows you to write Java code, and leave the platform (database) specific code to the driver. JAVA was designed to be platform independent and JDBC takes java one step ahead making java database code database independent. That means if you use JDBC for connectivity for SQL Server you can run the same code without changing for ORACLE.

Figure: - JDBC as a layer
Different sections in JDBC: -
There are four major components in JDBC. Below is the diagram which shows the four major sections and the way they interact to achieve the final “resultset”.

Figure: - JDBC in detail

Driver manager section creates the connection object. Connection object create the statement object with the required SQL which is then executed against the database. And finally database gives the result back in the resultset.

See the following starter video on Servlet: -

Click to get Java Training


Get more Java training stuffs from author's blog

Saturday, 5 May 2012

Java Training: -How will you explain taglib directives?

Taglib are also termed as JSP tag extensions. They provide a way of encapsulating reusable functionality on JSP pages. One of the biggest drawbacks of scripting environments such as JSP is that it's easy to get carried away without thinking about how it will be maintained and grown in the future. For example, the ability to generate dynamic content by using Java code embedded in the page is a very powerful feature of the JSP specification. Custom tags allow such functionality to be encapsulated into reusable components. You can make write your reusable class in JAVA and call the same using XML tag.

There are four files which play an important role:-
  • Main class file which encapsulates the logic.
  • Tag library descriptor file.
  • Web.xml file which has the tag library descriptor file location.
  • Finally the JSP file which calls it.
Below is the image which shows the four files in one go.

Figure: - taglib directive in action
The first file is the class file which will has the reusable code which will be called in the JSP file. The above class is also called as tag handler class. One of the important things to note is doStartTag() and doEndTag(). doStartTag is called when the JSP engine encounters an open tag and doEndTag is called when it encounters a closed tag.

The second important file is the tag descriptor file. This file maps the class name with a name which will be used to call this class.

The third file is the web.xml file. We need to define the tag library descriptor file location in web.xml file.
Finally is the JSP file which calls the class. There are two things to be noted in the JSP file first is the taglib which refers to the URI. Second is the custom tag which calls the datetime class.

See the following video on Java which describes introduction to EJB (Enterprise Java Beans): -

Click to get Java Training


Get more Java training stuffs from author's blog