NoSuchObjectException: No such object in table
Hi,
I have written this simple client server application using RMI.
Server
Code:
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;
import java.util.Iterator;
import java.util.Properties;
public class Server {
private static final long WAIT_TIME = 3000;
static String regServerHost;
static int regServerPort;
static FileWriter fWriter;
static String hostId;
static TransactionManager manager;
static void init() {
try {
fWriter = new FileWriter("server-"+hostId+".log");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void registerRemoteObject() {
Registry registry;
// Bind the remote object's stub in the registry
try {
log("Server: regserverport "+regServerPort);
log("Server: remote object name "+Globals.remoteObjectName);
manager = new TransactionManagerImpl(hostId);
// manager = (TransactionManager) UnicastRemoteObject.exportObject(new TransactionManagerImpl(hostId), 0);
registry = LocateRegistry.getRegistry(regServerPort);
registry.rebind(Globals.remoteObjectName, manager);
log("server: bound complete");
} catch (RemoteException e) {
log("server: exception");
e.printStackTrace();
} catch (Exception e) {
log("server: exception");
e.printStackTrace();
}
}
private static boolean lookupobj() {
Registry registry;
TransactionManager manager = null;
try {
registry = LocateRegistry.getRegistry(regServerPort);
manager = (TransactionManager) registry.lookup(Globals.remoteObjectName);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (manager == null)
return false;
return true;
}
private static void startServer() {
try {
registerRemoteObject();
log("successfully registered remote object");
int counter = 0;
while (counter < Globals.numberOfNodes) {
synchronized(Globals.threadsSet) {
Globals.threadsSet.add(Thread.currentThread());
}
try {
Thread.sleep(WAIT_TIME);
} catch (Exception e) {}
synchronized(Globals.accessCount) {
counter = Globals.accessCount;
}
// log(Globals.remoteObjectName+" " +lookupobj());
}
//Utils.writeToServerLogFile(Globals.serverLogFile);
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
public static void main(String args[]) {
hostId = args[0];
regServerHost = args[2];
regServerPort = Integer.parseInt(args[1]);
init();
loadSystemProperties();
log(hostId+" started itself ");
startServer();
}
private static void loadSystemProperties() {
Properties properties = new Properties();
try {
properties.load(new FileInputStream("system.properties"));
} catch (IOException e) {
}
Globals.serverLogFile = properties.getProperty("serverLogFile");
Globals.remoteObjectName = properties.getProperty("remoteObjectName");
Globals.numberOfNodes = Integer.parseInt(properties.getProperty("numberOfNodes"));
}
private static void log(String msg) {
try {
System.out.println(new Date()+" "+msg);
fWriter.write(msg+"\n");
fWriter.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Client
Code:
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Properties;
import java.util.Random;
public class Client {
private static int numberOfRuns = 1;
private static String remoteObjectName;
private static FileWriter fWriter;
private static String hostId;
static void init() {
try {
fWriter = new FileWriter("client-"+hostId+".log");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
String host = args[2];
int serverPort = Integer.parseInt(args[1]);
hostId = args[0];
Globals.hostId = hostId;
init();
host = "sun114-12.cise.ufl.edu";
log("host is "+host);
log("client "+hostId+" started");
loadSystemProperties();
Transaction ts = new Transaction(host, serverPort, remoteObjectName);
try {
int st = Utils.getInitialSleepTime();
Thread.sleep(st);
int i;
log(ts.sayHello());
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
private static void log(String output) {
System.out.println("Client: "+output);
}
private static int getInteger() {
Random ran = new Random();
int rNum = 1 + ran.nextInt(1000);
return rNum;
}
private static void loadSystemProperties() {
Properties properties = new Properties();
try {
properties.load(new FileInputStream("system.properties"));
} catch (IOException e) {
}
numberOfRuns = Integer.parseInt(properties.getProperty("numberOfRuns"));
remoteObjectName = properties.getProperty("remoteObjectName");
Globals.numberOfNodes = Integer.parseInt(properties.getProperty("numberOfNodes"));
Globals.remoteHosts = new String[Globals.numberOfNodes];
for (int i = 1;i <= Globals.numberOfNodes;i++) {
Globals.remoteHosts[i-1] = properties.getProperty("node"+i);
}
}
}
[U]Transaction[/U]
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Transaction {
TransactionManager manager;
String remoteObjectName;
PrintWriter fWriter;
private void lookupRemoteObject(int i, int serverPort) {
Registry registry;
boolean flag = false;
String host = Globals.remoteHosts[i];
System.out.println("host is "+host);
while (!flag) {
try {
registry = LocateRegistry.getRegistry(host, serverPort);
manager = (TransactionManager) registry.lookup(remoteObjectName);
if (manager != null) {
flag = true;
} else {
Utils.pause(3000);
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace(fWriter);
//fWriter.flush();
} catch (NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace(fWriter);
//fWriter.flush();
} catch (Exception e) {
//e.printStackTrace();
//fWriter.flush();
}
}
}
public Transaction(String host, int serverPort, String remoteObjectName) {
try {
fWriter = new PrintWriter("transaction-"+Globals.hostId+".log");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.remoteObjectName = remoteObjectName;
int i = 1;
lookupRemoteObject(i, serverPort);
}
/*Tuple in(Tuple inTouple) {
Tuple matchedTuple = null;
try {
matchedTuple = manager.getTuple(inTouple);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return matchedTuple;
}*/
/* Tuple out(Tuple t) {
Tuple outTuple = new Tuple("",null);
try {
System.out.println("calling tuplemanager");
// String name = manager.putTuple(t);
System.out.println("put tuple returned ");
} catch (Exception e) {
e.printStackTrace();
}
return outTuple;
} */
/*Tuple out(Tuple t) throws RemoteException {
return manager.putTuple(t);
}
Tuple in(Tuple t) throws RemoteException {
return manager.getTuple(t);
}*/
public String sayHello() {
try {
return manager.sayHello();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace(fWriter);
fWriter.flush();
}
return "";
}
public void acquireLock() {
try {
manager.acquireLock();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
After registering the remote object the server shows
"Successfully registered remote object"
But when the client executes ts.sayHello() the following exception is shwon:
NoSuchObjectException: No Such Object in Table.
This is driving me crazy because there is obviously no reason for this. And I am at a loss how to debug and find out the problem.
Please help me with this.
Send mail without SMTP Server/Protocal
How can i send mail without using SMTP protocal can anyone send me code or way i can do ..
thanks
vikram