Java Native Interface (JNI)

JNI is a programming framework that enables the java code running in a java virtual machine to call native applications specific to operating system and hardware.  JNI enables one to write native methods to handle situations when an application cannot be written entirely in the Java programming language.

Here I am describing how to do programming with JNI with an example. JNI is difficult, as it involves two languages and runtimes. I shall assume that you are familiar with:

  1. Java
  2. C/C++ and the GCC Compiler
  3. (For Windows) Gygwin or MinGW.
  4. (For IDE) Eclipse C/C++ Development Tool (CDT)

Here, I am giving an example with C programming language.

Step 1: Write a Java class that uses native methods

public class HelloJNI {
   static {
      System.loadLibrary("hello"); /* hello.dll (Windows) or libhello.so (Unixes)*/
   }
   /* A native method that receives nothing...

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...