Thursday, May 15, 2014

System.out.println



System           

final class
out      

static member of final class and of type printStream
println 
method  of class printStream


System

 System is a Final class from java.lang package

public final class System extends object

Since it is a final class it  can’t be instantiated and inherited by other classes
err,in and out are some static fields in System class.


Out [public static final PrintStream out]
Of type printStream,  already open and ready to accept output data and print data to outputstream [console]
In  [public static final InputStream in]
Of type inputStream, open and ready to supply input data ,generally keyboard
err  [public static final PrintStream err]
Of type printStream, already open and ready to accept output data, and print data to standard error output stream[error console in eclipse]


Out

  A static member of  final System class  and type of PrintStream (standard output stream)

public static final PrintStream out

This stream is already opened by JVM during start-up and ready to accept output data. It is mapped to standard O/P console host.

When JVM is initialized it will call initializeSystemClass() and will initialize the system class and the out variable.  This will use the setout() to set the output to console.

println

A method of class PrintStream . Used to print on standard output. It will print the data to console and a newline. Internally it will call print()and then write() + newLine()

User can customize the out object setout() method. By default the O/P the data to console,  we can redirect it to any file using this setout()

      System.setOut(new PrintStream(new FileOutputStream("print.txt")));
      System.out.println(" output from console ");

In the above example, all the contents of the println will be written to the file “print.txt”




Disadvantage:

Use System.out.println only for our learning and internal debugging purpose. It will degrade performance . Since just println will call a sequence of print + write+ newline. Also system.out is not synchronized , implementing synchronization is very complex and that will degrade performance.
Use  Log4J for debugging purpose in your code , it has multiple levels of debugging options. Also Loggers are synchronized.




Wednesday, May 14, 2014

Developing simple Webservices using Jersey


Jersey is an open source RESTful webservice framework in Java. It provides lots of utilities and features to simplify the RESTful service development. Here, I will explain how to develop a simple hello world program using Jersey 2.x. 

1. Download the Jersey JAX-RS 2.0 RI bundle from https://jersey.java.net/download.html

2. Create a dynamic web project in Eclipse. Keep all the JAR files downloaded in step 1 in the WebContent/WEB-INF/lib folder.

3. Create a web.xml file inside WebContent/WEB-INF directory as follows.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  
<servlet>
    <servlet-name>myAction</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.test.jersey.action</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet> 
<servlet-mapping>
    <servlet-name>myAction</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>


In the above web.xml, any requests coming to this application will be routed to the servlet class org.glassfish.jersey.servlet.ServletContainer which will redirect the request to appropriate Java class present inside the package com.test.jersey.action. 

If you are using Jersey 1.x version, the servlet class name and init param to be used is,


<servlet-name>myAction</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.test.jersey.action</param-value>
 </init-param>



4. Create a Java file inside the package com.test.jersey.action (or in its subpackage) which will map the request to corresponding action


package com.test.jersey.action;

import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;

@Path("/hello")
public class HelloWorldProgram {

@GET
@Produces("text/html")
public String getHelloMessage() {
return "<html><body>Hello World</body></html>";
}
}




5. Build the application and deploy it in any server. Now if you hit the url http://localhost:8080/hello you will get the message "Hello World". (The portnumber may differ based on your server setup).


If you need to accept input parameters from client, you can do so by using @PathParam annotation as below.


@GET
@Path("/user/{id}")
@Produces("text/html")
public String getHelloMessage(@PathParam("id") String name) {
return "<html><body>Hello "+name+"</body></html>";
}


If you hit the service using the url  http://localhost:8080/hello/user/ABC, it will return a text "Hello ABC".

Jersey is one of the best choice to develop simple light weight Webservices in Java.

Tuesday, May 13, 2014

Writing to multiple log files in Java

   In Java, normally we use log4j or java.util.Logger to create log files. Log4j uses the attributes mentioned in log4j.properties file for this. By default, log4j will look in to the WebContent/WEB-INF/classes directory for the properties file. A sample log4j property file is given below.

log.dir=/mylogs

rrd.dir=${log.dir}/rrd
datestamp=yyyy-MM-dd HH:mm:ss.SSS
roll.pattern.hourly=.yyyy-MM-dd.HH
roll.pattern.daily=.yyyy-MM-dd


log4j.rootLogger=DEBUG,testlogger
log4j.appender.testlogger=org.apache.log4j.DailyRollingFileAppender
log4j.appender.testlogger.File=${log.dir}/test.log
log4j.appender.testlogger.DatePattern=${roll.pattern.daily}
log4j.appender.testlogger.layout=org.apache.log4j.PatternLayout



Here, the logs will be generated in a log file named test.log inside the directory mylogs.


Using log4j, it is possible to write logs to different files based on the package name. For that, we need to mention different log handler based on the package name. For eg: suppose you want to print all the logs generated by the java classes present under the package com.test. For this, you need to define a handler as follows.


log4j.logger.com.test=DEBUG, testpackageLogger

Now you can mention the file where the logs from com.test package needs to be printed as below.

log4j.appender.testpackageLogger.File=${log.dir}/testpackage.log


Similarly, for different package named com.temp, you can define a new handler similar to above in the same log4j.properties file


log4j.logger.com.temp=DEBUG, temppackageLogger

log4j.appender.temppackageLogger.File=${log.dir}/temppackage.log


The logs generated by the classes which are not present in these packages will get printed in the log file mentioned under rootLogger configuration mentioned above.


Please note that the logs generated by the classes present under com.test and com.temp will also get generated inside the log file mentioned in rootLogger. To avoid that, you can use log4j additivity property


log4j.additivity.com.test=false

By setting its value as false, the logs will get printed only inside the package specific log file.