Thursday, December 24, 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

Tuesday, December 22, 2009

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 {

void ping(),

i32 add(1:i32 num1, 2:i32 num2),

i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch),

string greeting(),


Compile the above thrift file with the thrift compiler to generate both java and c# code.

thrift.exe -r --gen java hello.thrift

thrift.exe –r –gen csharp hello.thrift

Following is the service (Program.cs):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Thrift;
using Thrift.Transport;
using Thrift.Protocol;
using Thrift.Server;
using hello;

namespace thriftcsserv
class Program
static void Main(string[] args)
HelloServiceHandler handler = new HelloServiceHandler();
HelloService.Processor processor = new HelloService.Processor(handler);
TServerTransport transport = new TServerSocket(34568);
TServer server = new TSimpleServer(processor, transport);

// Use this for a multithreaded server
// server = new TThreadPoolServer(processor, serverTransport);

Console.WriteLine("Starting the server...");
catch (Exception e)

public class HelloServiceHandler : HelloService.Iface
public void ping()
{ }
public int add(int num1, int num2)
return (num1 + num2);
public int calculate(int logid, Work w)
int val = 0;
switch (w.Op)
case Operation.ADD:
val = w.Num1 + w.Num2;
case Operation.SUBTRACT:
val = w.Num1 - w.Num2;
case Operation.MULTIPLY:
val = w.Num1 * w.Num2;
case Operation.DIVIDE:
if (w.Num2 == 0)
InvalidOperation io = new InvalidOperation();
io.What = (int)w.Op.GetTypeCode();
io.Why = "Cannot divide by 0";
throw io;
val = w.Num1 / w.Num2;
InvalidOperation io1 = new InvalidOperation();
io1.What = (int)w.Op.GetTypeCode();
io1.Why = "Unknown operation";
throw io1;
return val;
public string greeting()
return "Hello world of RPC using thrift from c# service";

And the is below:

package hello.client;

import hello.HelloService;
import hello.InvalidOperation;
import hello.Operation;
import hello.Work;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class HelloClient {
public static void main(String[] args) {
try {

TTransport transport = new TSocket("localhost", 34568);
TProtocol protocol = new TBinaryProtocol(transport);
HelloService.Client client = new HelloService.Client(protocol);;;

String str = client.greeting();

int sum = client.add(1, 1);
System.out.println("1+1=" + sum);

Work work = new Work();

work.op = Operation.DIVIDE;
work.num1 = 1;
work.num2 = 0;
try {
int quotient = client.calculate(1, work);
System.out.println("Whoa we can divide by 0");
} catch (InvalidOperation io) {
System.out.println("Invalid operation: " + io.why);

work.op = Operation.SUBTRACT;
work.num1 = 15;
work.num2 = 10;
try {
int diff = client.calculate(1, work);
System.out.println("15-10=" + diff);
} catch (InvalidOperation io) {
System.out.println("Invalid operation: " + io.why);


} catch (TException x) {

Now run the service and then the client. The o/p is as follows:


Hello world of RPC using thrift from c# service


Invalid operation: Cannot divide by 0


Hope you find this useful. I did not find a direct C# service example but i could easily translate the Java server example to C#.

Thursday, December 10, 2009

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 HelloWorld : JsonRpcHandler
public string Greetings()
return "Welcome to Jayrock!";

public int sum(int a, int b)
return (a+b);


The Java JSON-RPC client:

import org.apache.commons.httpclient.HttpState;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codebistro.jsonrpc.Client;
import org.codebistro.jsonrpc.HTTPSession;
import org.codebistro.jsonrpc.TransportRegistry;

public class Main {
private static final Log log = LogFactory.getLog(Main.class);
private static final String rootURL= "http://localhost/jayrock/helloworld.ashx";
* @param args
public static void main(String[] args) {
// Register HTTP
HTTPSession httpSession= (HTTPSession)TransportRegistry.i().createSession(rootURL);
HttpState state= new HttpState();
Client client= new Client(httpSession);
RpcIF rpc = client.openProxy(null, RpcIF.class);
String str = rpc.greetings();
log.debug("Output:" + str);

public interface RpcIF {
String greetings();

O/P on client side:

JSON RPC REQUEST ==> {"id":1,"method":"greetings","params":[]}
JSON RPC RESPONSE ==> {"id":1,"result":"Welcome to Jayrock!"}

15 sorting algorithms visualized in 5 minutes, with awesome arcade sounds

15 sorting algorithms visualized in 5 minutes, with awesome arcade sounds from r/programming