Posting for AkshanthaKumar:
=====================
Hi
How do I cluster a TCP server. I am not sure how to go about it. Lets say I have a code below, how would I enable clustering on it using Terracotta and what benefits would I get by clustering using Terracotta like would the threads be moved to workers.
Code:
import java.net.*;
import java.io.*;
class TestServer extends Thread {
public TestServer() {
super("Demo");
start();
}
public void run() {
ServerSocket acceptSocket=null;
try {
acceptSocket = new ServerSocket(123);
while(true) {
final Socket s = acceptSocket.accept();
Thread t = new Thread() {
public void run() {
try {
doRequest(s);
} catch(Exception err) {
System.out.println("Error at client :"+err);
}
}
};
t.start();
}
} catch (IOException e) {
System.out.println("Error at server :"+e);
} finally {
try {
acceptSocket.close();
}catch(Exception re){
throw new RuntimeException(re);
}
}
}
public final void doRequest(Socket s) throws IOException {
InputStream in = s.getInputStream();
OutputStream out=s.getOutputStream();
PrintWriter rout=new PrintWriter(
new BufferedOutputStream(out));
rout.println("Hello");
rout.flush();
try {
Thread.sleep(1000);
} catch (InterruptedException e){
System.out.println("Do request thread interrupted : "+e);
} finally {
out.close();
}
s.close();
}
public static void main(String args[]) {
new TestServer();
}
}
=====================
Regards, Akshathkumar Shetty
Tech Lead
enStage Software Pvt. Ltd.
Email: ashetty@enstage.com Off: +91-80-25204351, 80-32722080 Telefax: +91-8025204350 Bangalore, India