Simple RMI

m6s

Joined
Mar 19, 2010
Messages
3
Reaction score
0
This is a simple server class for RMI :

public class SmallAgent implements IExecute {

private transient static int port = 7147;

public SmallAgent() throws RemoteException {
super();
}

@Override
public Object execute(int a, int b) throws RemoteException {
Object s = new String("EXECUTING ON AGENT SOMETHING: a+b=" + (a + b));
return s;
}

@Override
public String getVersion() {
return "SmallAgent-Version()";
}

static void startStandalone(int port) throws IOException, ClassCastException {
IExecute smallAgent = new SmallAgent();
RemoteStub stub = RMI.getRMI().exportObject(smallAgent);

ServerSocket serverSocket = new ServerSocket(port);

for (;;) {
Socket socket = serverSocket.accept();
OutputStream out = socket.getOutputStream();
RMI.getRMI().writeReference(stub, out);
//Because multiple connections are needed, threading is used on InputStreamRunnable
Runnable run = new InputStreamRunnable(socket.getInputStream());
new Thread(run).start();
}
}

public static void main(String[] args) throws RemoteException, IOException, ClassCastException {
Logger logger = Logger.getLogger("gr.sentinel.agent");
logger.info("Starting Standalone");
startStandalone(port);
}
}



The RMI handling is done with this :
public class RMI {

private static RMI rmi = null;

public static RMI getRMI() {
if (rmi == null) {
return new RMI();
} else {
return rmi;
}
}

public RMI() {
Logger logger = Logger.getLogger("gr.RMI");
}

public RemoteStub readObject(InputStream in)
throws IOException, ClassNotFoundException {
ObjectInputStream objectStream = new ObjectInputStream(in);
return (RemoteStub) objectStream.readObject();
}

/**
* Writes the stub to an output stream
*
* @param stub
* @param out
* @throws java.rmi.RemoteException
* @throws java.io.IOException
*/
public void writeReference(RemoteStub stub, OutputStream out)
throws RemoteException, IOException {
ObjectOutputStream objectStream = new ObjectOutputStream(out);
objectStream.writeObject(stub);
}

/** Export an RMI object, using the rmi.port system property, if set. */
public RemoteStub exportObject(Remote remote) throws RemoteException, ClassCastException {
String property = "7147";//Test
int rmiPort = 0;
if (property != null) {
rmiPort = Integer.parseInt(property);
}
if (rmiPort != 0) {
return (RemoteStub) UnicastRemoteObject.exportObject(remote, rmiPort);
} else {
return UnicastRemoteObject.exportObject(remote);
}
}
}



It drives me mad! The fact that IExecute overrides Remote interface, I use Java 6, and it breaks with ClassCastException about RemoteStub in line :
RemoteStub stub = RMI.getRMI().exportObject(smallAgent);
in void standAlone() method in SimpleAgent class.

IF I extend the UnicasteRemoteObject, and to dome modifications in order not to throw exception because it "binds" the port, the class listens on the port, smoothly!!!!!!!!!!!!
I am in Java6, I think I don't need any stubs. BUT If I try to put an rmic task in netbeans, netbeans BREAKS! HELL!

Has anyone any idea on this? Thank you in advance!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top