Showing posts from December, 2009

Singleton Pattern with Lazy Initialization On Demand Holder

This is one of the better ways to implementing a singleton class (class which lets user create one and only one instance).It uses Initialization on demand holder pattern.

public class Singleton {
private static class LazyHolder {
private static final Singleton instance = new Singleton();

public static Singleton getInstance() {
return LazyHolder.instance;

private Singleton() {
//initialize singleton instance

Using Apache Thrift for RPC over TCP socket

Apache thrift is a framework for cross language RPC service/client implementation. Thrift has a small IDL (interface definition language) and comes with an IDL compiler that generates code to be used to easily build RPC clients and servers that communicate seamlessly across programming languages. First you need to download thrift and then install Cygwin (i used 1.7 version of Cygwin).Since the thrift is distributed in source form so we first need to build the thrift.exe IDL compiler. Follow this document to generate the compiler exe on windows (mine is Windows 7 home premium x64).Following is an example usage. We intend to write a C# service and a Java client that implement the following interface (hello.thrift):namespace java hello
namespace csharp hello

enum Operation {
ADD = 1,

struct Work {
1: i32 num1 = 0,
2: i32 num2,
3: Operation op,
4: string comment,

exception InvalidOperation {
1: i32 what,
2: string why

service HelloService {


JSON-RPC between .NET service and Java client

Recently i experimented with Jayrock .NET library for JSON-RPC and following its tutorial it was very simple to run a .NET JSON-RPC service on my Win7 Home Premium (IIS 7.5). From my earlier experience with JSON-RPC in Java, i wrote a Java client using json-rpc-client library and could get the RPC communication happening without any issue.

The ease with which the code samples worked for the below helloworld-ish sample, motivated me to write about it. JSON-RPC is simpler to debug and implement than SOAP and can be secured by implementing it over https. Most of the time in systems integration all we need is getting the .NET software to communicate with the Java software and in such simpler scenarios where RPC mechanism which is firewall friendly like SOAP is needed then thats where JSON-RPC is a good option.

helloworld.ashx - the JSONRPCHandler

using System;
using System.Web;
using Jayrock.Json;
using Jayrock.JsonRpc;
using Jayrock.JsonRpc.Web;

namespace jayrockserv
public class HelloWorl…