Real time logging using JMS, log4j and websocket

In our daily life, we may sometime want to take the server log into the browser.  We have a lot of solutions to get this working. I am going to explain some interesting solution for real-time logging using log4j, JMS and websocket.

We all are familiar with log4j, so we don’t need much more explanation.  Normally, we use fileAppender to write logs into file.  Apache log4j has a feature to send the log messages to a JMS borker, which is called JMSAppender.

Here, in this example, I am using ActiveMQ as message broker. It is an open source message broker written in java together with a full Java Message Service client. We can use activeMQ by embedding into tomcat or by installing and running as a different server. Normally activeMQ start listening to a default TCP port 61616.

To use ActiveMQ as a destination of our messages, we need to configure JMS appender properly. The code sample...

WebSocket

There are many technologies that the server will send data to the client in the very moment it knows that the new data is available such as push, comet ,etc. These all are making an illusion that the server initiated the connection called long polling. With the long polling, client opens a HTTP connection to the server and keeps it open until it receives a response. These techniques work quite well, we use them daily in application such as Gtalk.

All of these work-around has one problem such as they carry the overhead of HTTP and will not be suited for low latency applications.

WebSocket: Sockets into the web

The websocket defines an API establishing socket connection between a browser and a server. Simply, it is a persistent connection between the client and the server and both parties can send data at any time. It is an independent TCP-based protocol. Its only relationship to HTTP is that the handshake is handled using HTTP servers as an Upgrade...

Always use length() instead of equals() to check empty string in java

If we need to check, If the string is Empty, Then do not use this way :  string1.equals(“”)

Best way to check if string is empty or not is to use length() method. This method simply return the count of characters inside char array which constitutes the string. If the count or length is 0; you can safely conclude that string is empty.

public boolean isEmpty(String str)
{
return str.equals(“”); //NEVER do this
}

public boolean isEmpty(String str)
{
return str.length()==0; //Correct way to check empty
}
PS : From JAVA 6 onwards, isEmpty() function is available in String class itself. Please use this function directly.