Monday, December 15, 2008

Using Log4J in Spring: Effective Logging

Spring supports logging in its usual plug and use format. To use Apache's log4j in a Spring-Maven environments:

  1. Add the log4j dependency to the pom
  2. In web.xml of the application define spring listener for log4j and its config location
  3. In the config location put in the appropriate properties file defining your loggers



The log4j.properties at the above link is fully commented to help you(and me) understand how to use a properties file to split your logs at a finer level.

I have realized that in order to utilize the full power of log4j it is important to understand and use log4j.properties. This can help you to isolate logs from different part of of your application- something that can be really handy when you are trying to pin-point the source of trouble in a large system. The bigger and more distributed your system becomes, more important and useful logging appears.
As usual, I have focussed only on the practical aspects. For knowing more about log4j, see:





Tuesday, December 9, 2008

PostgreSQL Vs MySQL

My team decided to use PostgreSQL instead of MySQL. This got me interested in finding out why. Found these articles that helped me understand:

(a recent comparison, subtly votes in favor of PostgreSQL)
(this is the most concise comparison but is a little old, very nicely tabulated)
(a very old comparison to give some prespective)

In a nutshell, both are equally good in the current scenario; though before the 8th release of PostgreSQL,  MySQL held a distinct advantage. Currently PostgreSQL offers a wider range of features and on the whole MySQL is a bit faster. However with every release MySQL is inching towards more features and PostGreSQL towards more speed. MySQL is currently more widespread, perhaps because it is older and is preferred by the widespread PHP community. Another difference is the licensing. Both are open-source but MySQL is under GNU while PostgreSQL is under BSD.  

Monday, December 8, 2008

Using JAXB in a Spring-Maven Environment for WebServices

In a Spring application you can consume a webservice using JaxRpcPortProxyFactorybean or XFireClientFactoryBean (in conjunction with XFire). However, there is another way which can prove to be extremely useful and convenient if you are using Spring and Maven: by using Spring and Maven in conjunction with JAXB. As usual, I will focus on using JAXB and not about JAXB. To learn more about JAXB see:


In this article we will cover
1. Setting up JAXB in Spring environment to generate stubs for the webservice
2. Using it in an external project or the same project.

As per the default maven build structure, you need to make a package called "wsdl" under src/main and place a copy of wsdl of the webservice you wish to use. Then configure your pom file with the right dependencies:



Two things that you should notice in the pom besides JAXB dependencies are the build plugins maven-source-plugin and maven-jaxb2-plugin. They will generate stubs for your webservice in the "target" directory under "generated-sources" when you compile the project/ generate resources using maven. After that your webservice should be ready to used. Almost :D.

The generated source will contain stubs that correspond to exposed functions, responses and the various objects used. The name of the stubs will correspond to name in wsdl. In the same project you can use it by importing stubs from the package specified in com.saveenkumar.myApp.someWebService.types For an external project, you just need to import the project as a dependency.

Let us now see how to use these stubs. You will be able to use the webservice in your Spring app with the help of org.springframework.ws.client.core.WebServiceTemplate. You can see the bean config that you can use to inject it here:


Note that the "contextPaths" property of the JAXB marshaller is same as the  property above. If you want to dig still deeper, have a look about using WebServiceTemplate  at:


and



It may be a good idea to hide the WebServiceTemplate behind a nice interface to abstract away the user from the generated JAXB classes.

The generated classes will always have a class called "ObjectFactory". There will also be objects corresponding to every operation, every response and every object used in these transactions. Let us say our webservice exposes the following operations:

List getAllStockInfo();
List getStocksByVolatilityRange(float low, float high);
StockInfo getStockInfo(String ticker);

May generate following objects:

ObjectFactory
ArrayOfStockInfo
StockInfo
GetAllStockInfo
GetAllStockInfoResponse
GetStockInfo
GetStockInfoResponse
GetStocksByVolatilityRange
GetStocksByVolatilityRangeResponse

Let us say we want to use GetStockInfo. The code would look something like:



I would also recommend using SoapUI to test your webservices. 

Tuesday, December 2, 2008

@Resource and @Autowired

In September I wrote an article on using @Autowired for testing RMI services in Spring. Recently when trying to test a webservice consumer, I ran into trouble with @Autowired. Instead using @Resource (javax.annotation.Resource in Java 1.6) seemed to work fine. Digging I found this article in Spring forum:


which pointed to this in spring reference:

Seems the two are pretty similar. Using 
@Qualifier (org.springframework.beans.factory.annotation.Qualifier) with @Autowired solved the problem and in the end I could use either.