Showing posts with label apache. Show all posts
Showing posts with label apache. Show all posts

Thursday, November 19, 2009

SpringSource Tool Suite

I have started using STS for the past 3-4 months. The tool is powered by the latest Eclipse version Galileo and comes bundled with useful plugins for Spring based development. The tool also comes with the maven plugin bundled in. I always use Maven and Spring for all my Java projects so I am enjoying this tool.

The suite also comes with Apache Tomcat server called TC Server. The server comes with a web application named "Insight". This web application tracks performance of the web applications deployed in this server. The application have a nice UI to drill down the statistics to pin point the code hampering the performance. No doubt the tool is very helpful to debug the code and find the performance bottlenecks. The beauty is no configurations are required at all.

Friday, October 30, 2009

Configure Log4j for a web application on Apache Tomcat

Today I stumbled upon a problem of configuring log4j for a web application on Apache Tomcat. The problem is where to keep the log4j.xml and how initialize the logging system. One way is to keep the log4j.xml anywhere and change the Tomcat startup script to add -Dlog4j.configuration=<log4j.xml file path> but I do not want to depend on a change in Tomcat startup script.

I tried all the possible places but it did not work. I realized unless I define the log4j.configuration system parameter it will not work. I then looked at log4j api and found DOMConfigurator class which initializes the log4j system from log4j.xml.

Here is what I done to keep all configurations inside my application.

Create a listener class as shown below to initialize the log4j using the log4j.xml deployed with your application.


import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class Log4jInitializationListener implements ServletContextListener {
public Log4jInitializationListener() {
}
public void contextInitialized(ServletContextEvent servletContextEvent) {
DOMConfigurator.configureAndWatch(servletContextEvent.getServletContext().getInitParameter("log4jXMLFilePath"));
Logger logger = Logger.getLogger(Log4jInitializationListener.class);
logger.debug("Log4j working");
}

public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}


Add these entries to your web.xml. Here we are defining a parameter to configure the log4j.xml file path and registering the above created listener.

<pre>
<context-param>
<param-name>log4jXMLFilePath</param-name>
<param-value>WEB-INF/classes/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>Log4jInitializationListener</listener-class>
</listener>
</pre>

Start the application to confirm log4j is working. I have used the following log4j configuration so here it creates a server.log file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="RollingFileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="server.log"/>
<param name="MaxFileSize" value="100MB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %l - %m%n" />
</layout>
</appender>
<root>
<priority value="debug"/>
<appender-ref ref="RollingFileAppender"/>
</root>
</log4j:configuration>

Thursday, May 21, 2009

Apache cxf using annotations and integration with Spring

Today I will show you how you can create a webservice using Apache CXF version 2.2.1. I will be building this webservice on top of my previous application.

Create a interface AirlineManager to expose methods over webservice. The create method takes airline code and name to create a Airline record in database. Annotation @WebService tells that this is a interface for webservice.

package com.travel;

import javax.jws.WebService;

@WebService
public interface AirlineManager {
public void create(String code, String name);
}

Implement the above webservice interface. We are using AirlineDao created in earlier post. Notice the @WebService(endpointInterface = "com.travel.AirlineManager") which tells that this implementation is for AirlineManager interface.

package com.travel;

import com.travel.dao.AirlineDao;
import javax.jws.WebService;

@WebService(endpointInterface = "com.travel.AirlineManager")
public class AirlineManagerImpl implements AirlineManager {
private AirlineDao airlineDao;

public AirlineManagerImpl(AirlineDao aAirlineDao) {
airlineDao = aAirlineDao;
}

@Override
public void create(String code, String name) {
airlineDao.create(code, name);
}
}

Add this in the spring-context.xml. Here we are importing some cxf beans, defining bean AirlineManagerImpl and creating airlineManagerService webservice.

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-http-jetty.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<bean id="airlineManager" class="com.kaleconsultants.travel.AirlineManagerImpl">
<constructor-arg><ref bean="airlineDao"/></constructor-arg>
</bean>

<jaxws:endpoint id="airlineManagerService" implementor="#airlineManager" address="/AirlineManagerService" />

Add this in your web.xml.

<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

Deploy the application in tomcat and your webservice is ready. You can check the wsdl http://host:port/travel/AirlineManagerService?wsdl.

To test the webservice, you need to create a client. You can use wsdl2java available in the bin directory of cxf.

Execute this command, this will generate the wsdl stubs.
wsdl2java http://host:port/travel/AirlineManagerService?wsdl

Write a client like this and there you go.......

package com.travel;

public class ClientTest {
public static void main(String[] args) {
AirlineManagerImplService service = new AirlineManagerImplService();
AirlineManager airlineManager = service.getAirlineManagerImplPort();
airlineManager.create("IC", "Indian");
}
}