Tuesday, September 28, 2010

Eclipse Memory Analyzer

http://wiki.eclipse.org/index.php/MemoryAnalyzer

I will post the details soon

XLogger

Last few days I was looking to improve the logger. Main issue with the logger that we need to make sure we are using log.isXXXEnabled() for all log calls. So we are making sure that in the log message we are not making unnecessary string if logger is not going to print that. Also sometime we do perfLogger with System.currentTimeInMillis but we actually not using in prod server, but this system call are very time consuming. I was thinking of how to improve it so i have come up with customize logger called XLogger.

XLogger.java

import java.text.MessageFormat;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
/**
*
* XLogger is wrapper around the Logger provided by apache.
* This framework will make sure that you are not building a string for
* log until it is not candidate for logging. Logger has provided isXXXEnable method
* to do this but for every logging statement, using this is tedious.
*
* It is also helpful for performance logging.
* @author Viren.Balaut
*
*/
public class XLogger
{
private Logger logger = null;
private boolean isPerfLogger = false;
private long startTime = -1;
private long endTime = -1;
public static XLogger getLogger()
{
return new XLogger(false);
}
public static XLogger getLogger(String name)
{
return new XLogger(name, false);
}
public static XLogger getLogger(Class clazz)
{
return new XLogger(clazz, false);
}
public static XLogger getPerfLogger()
{
return new XLogger(true);
}
public static XLogger getPerfLogger(String name)
{
return new XLogger(name, true);
}
public static XLogger getPerfLogger(Class clazz)
{
return new XLogger(clazz, true);
}
private XLogger(boolean isPerfLogger)
{
logger = LogManager.getRootLogger();
this.isPerfLogger = isPerfLogger;
}
private XLogger(String name, boolean isPerfLogger)
{
logger = LogManager.getLogger(name);
this.isPerfLogger = isPerfLogger;
}
private XLogger(Class clazz, boolean isPerfLogger)
{
logger = LogManager.getLogger(clazz);
this.isPerfLogger = isPerfLogger;
}
public void start()
{
if (logger.isDebugEnabled() && isPerfLogger)
{
startTime = System.currentTimeMillis();
}
}
public void stop()
{
if (logger.isDebugEnabled() && isPerfLogger)
{
if (startTime <= -1)
{
throw new IllegalStateException(
"Please call start() before calling end.");
}
endTime = System.currentTimeMillis();
}
}
public void perf(String message, String methodName)
{
if (logger.isDebugEnabled() && isPerfLogger)
{
if (startTime <= -1 endTime <= -1)
{
throw new IllegalStateException(
"Please call start() and end() both before calling logPerf.");
}
debug(
message + ", Time taken({0}) in MS : {1}",
methodName,
(endTime - startTime));
resetTimer();
}
}
public void console(String message, Object... objects)
{
System.err.println(getMessage(message, objects));
}
public void debug(String message, Object... objects)
{
if (logger.isDebugEnabled())
{
logger.debug(getMessage(message, objects));
}
}
public void info(String message, Object... objects)
{
if (logger.isInfoEnabled())
{
logger.info(getMessage(message, objects));
}
}
public void warn(String message, Object... objects)
{
if (logger.isEnabledFor(Priority.WARN))
{
logger.warn(getMessage(message, objects));
}
}
public void error(String message, Object... objects)
{
if (logger.isEnabledFor(Priority.ERROR))
{
logger.error(getMessage(message, objects));
}
}
public void error(String message, Throwable e, Object... objects)
{
if (logger.isEnabledFor(Priority.ERROR))
{
logger.error(getMessage(message, objects), e);
}
}
public void fatel(String message, Throwable e, Object... objects)
{
if (logger.isEnabledFor(Priority.FATAL))
{
logger.fatal(getMessage(message, objects), e);
}
}
private String getMessage(String message, Object... objects)
{
try
{
MessageFormat form = new MessageFormat(message);
String msg = form.format(objects);
return msg;
}
catch (Exception e)
{
System.err.println("Error occurred while parsing message for "
+ message);
return message;
}
}
private void resetTimer()
{
startTime = -1;
endTime = -1;
}
}

Monday, September 27, 2010

Resource Handling

Resource Handling
In Java, Most of the time we open some resource and after using we forget to close the resource and that make a lots of open connection for the resouse if we are not using a good ConnectionPool for resource. Still if we are using the connectino pool, then also we need to release the resopuse after use, so other can use that resource for their operation.
here is sample framework which basically takes care of these part in your java program.

Here I have two classes, one is TransactionGuard and other is Command interface.

TransactionGuard.java

/**
* TransactionGuard is make sure you release the particular resource after use.
* By using any resource, it will make sure that you are closing the resource
* after using. To use this one need to implement getConnection and
* releaseConnection method based on the need.
*
* @author Virendra.Balaut
*
* @param
* an element/resource for which you want to implement
* TransactionGuard
*/
public abstract class TransactionGuard
{
/**
* This is the main execute method which takes care of getting a connection of T type,
* do some operation and after use close the connection.
*
* @param command
* @throws Exception
*/
public void execute(Command command) throws Exception
{
T connection = null;
try
{
connection = getConnection();
command.execute(connection);
}
finally
{
releaseConnection(connection);
}

}

/**
* Abstract method to get Connection for a dedicated Type.
*
* @return
*/
protected abstract T getConnection() throws Exception;

/**
* Abstract method to release connection for a dedicated type.
*
* @param connection
*/
protected abstract void releaseConnection(T connection) throws Exception;
}


Command.java


public abstract class Command
{
public abstract void execute();
}

Using these framework will ensure that resouce will automatically closed after use.

Sample Example :-

Here I have sample program which will do database operation. So I need to open a database connection and after use it I need to release that connection.

new TransactionGuard()
{

@Override
protected Connection getConnection() throws Exception
{
Connection connection = createConnection();
return connection;
}

@Override
protected void releaseConnection(Connection connection)
throws Exception
{
if(connection != null) {
connection.close();
}

}
}.execute(new Command()
{

public void execute(Connection connection) throws Exception
{
// Do some operation with connection.

}
});


Let me know your suggestions/views.