The Artima Developer Community
Sponsored Link

Java Answers Forum
Help with RMI

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
steve heidel

Posts: 1
Nickname: steveatcp
Registered: Apr, 2007

Help with RMI Posted: Apr 17, 2007 12:24 PM
Reply to this message Reply
Advertisement
I have a need to build a client application that will sit on different personal PC's and be able to be run offline in a stand alone environment. When it is online I want to be able to connect to a server that runs some methods against a central database for the client application (these methods can only be run when the client is connected to the server).
To do this I thought I would build a client application and a server application. The client application can stand alone if not connected to the internet. If it is connected to the internet I would use RMI to be able to call some methods that the server application has server code.
First question is will this work? Is it the best way to do this?
I have build a quick little test client and server application to test how RMI works. The client does nothing more then call the server application and had it two numbers which the server application adds together and returns the sum.
If I compile all of the code in a default package (using Eclipse) things seem to work (at least running both applicaitons on my local PC). But if I put the client in one package and the server in a seperate package I can't get anything to work.
Here is the clinet application code:

package clientSide;
import java.rmi.*;
public class AddClient {
/**
* @param args
*/
public static void main(String[] args){

//Lets try setting up a security manager..
if (System.getSecurityManager() == null) {
System.out.println("Security Manager was null,
set one up");
System.setSecurityManager(new SecurityManager());
System.out.println("Security Manager is setup");
}

try{
System.out.println("");
System.out.println("");
String addServerURL = "rmi://" + args[0]
+ "/AddServer";
System.out.println("Using " + addServerURL + " to
execute addServerIntf naming.lookup");
AddServerIntf addServerIntf = (AddServerIntf)
Naming.lookup(addServerURL);
System.out.println("This is the addition of two
numbers");
System.out.println("The first number is:" + args[1]);
double d1 = Double.valueOf(args[1]).doubleValue();
System.out.println("The second number is:" + args[2]);
double d2 = Double.valueOf(args[2]).doubleValue();
System.out.println("The sum is: " +
addServerIntf.add(d1, d2));
System.out.println("");
System.out.println("");

//Adding a second call to a different file
String addServerURLTwo = "rmi://" +
args[0] + "/AddServerTwo";
System.out.println("Using " + addServerURLTwo +
" to execute the following");
AddServerIntfTwo addServerIntfTwo = (AddServerIntfTwo)
Naming.lookup(addServerURLTwo);
System.out.println("This is the addition of three
numbers");
System.out.println("The first number is: " + d1);
System.out.println("The second number is: " + d2);
double d3 = 3;
System.out.println("The third number is: " + d3);
System.out.println("The sum is: " +
addServerIntfTwo.addThree(d1, d2, d3));
System.out.println("");
System.out.println("");

//Just to make sure I can make the first call again
//Recalling the first connection.
System.out.println("Using " + addServerURL +
" to execute the following");
System.out.println("This is the addition of two numbers
again");
System.out.println("The sum of " + d1 + " and " + d2 + "
is " + addServerIntf.add(d1, d2));
System.out.println("");
System.out.println("");
}catch (Exception e){
System.out.println("Exception in AddClient is as
follows " + e);
}
}
}

The two interfaces for the server calls are...

package clientSide;
import java.rmi.*;
public interface AddServerIntf extends Remote{
double add(double d1, double d2) throws RemoteException;
}


package clientSide;
import java.rmi.*;
public interface AddServerIntfTwo extends Remote{
double addThree(double d1, double d2, double d3) throws RemoteException;
}

And the client.policy looks like this. The directory
is to the location of my serverSide.jar and I have a....


grant codeBase "file:c:/Docume~1/steveh~1/WORKSP~1/simple~1"{
permission java.security.AllPermission;
};


The server application looks like this....

package serverSide;
import java.net.*;
import java.rmi.*;
public class AddServer {

/**
* @param args
*/
public static void main(String[] args) {
//Lets try setting up a security manager..
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}

try{
//The original code
AddServerImpl addServerImpl = new AddServerImpl();
Naming.rebind("AddServer", addServerImpl);

//Adding a second lookup
AddServerImplTwo addServerImplTwo = new AddServerImplTwo();
Naming.rebind("AddServerTwo", addServerImplTwo);
}catch (Exception e){
System.out.println("Exception in addServer: " + e);
}
}
}


The java generated stub code looks like this...
package serverSide;

// Stub class generated by rmic, do not edit.
// Contents subject to change without notice.

public final class AddServerImpl_Stub
extends java.rmi.server.RemoteStub
implements AddServerIntf, java.rmi.Remote
{
private static final long serialVersionUID = 2;

private static java.lang.reflect.Method $method_add_0;

static {
try {
$method_add_0 = AddServerIntf.class.getMethod("add", new java.lang.Class[] {double.class, double.class});
} catch (java.lang.NoSuchMethodException e) {
throw new java.lang.NoSuchMethodError(
"stub class initialization failed");
}
}

// constructors
public AddServerImpl_Stub(java.rmi.server.RemoteRef ref) {
super(ref);
}

// methods from remote interfaces

// implementation of add(double, double)
public double add(double $param_double_1, double $param_double_2)
throws java.rmi.RemoteException
{
try {
Object $result = ref.invoke(this, $method_add_0, new java.lang.Object[] {new java.lang.Double($param_double_1), new java.lang.Double($param_double_2)}, 864055858262779977L);
return ((java.lang.Double) $result).doubleValue();
} catch (java.lang.RuntimeException e) {
throw e;
} catch (java.rmi.RemoteException e) {
throw e;
} catch (java.lang.Exception e) {
throw new java.rmi.UnexpectedException("undeclared checked exception", e);
}
}
}

package serverSide;

// Stub class generated by rmic, do not edit.
// Contents subject to change without notice.

public final class AddServerImplTwo_Stub
extends java.rmi.server.RemoteStub
implements AddServerIntfTwo, java.rmi.Remote
{
private static final long serialVersionUID = 2;

private static java.lang.reflect.Method $method_addThree_0;

static {
try {
$method_addThree_0 = AddServerIntfTwo.class.getMethod("addThree", new java.lang.Class[] {double.class, double.class, double.class});
} catch (java.lang.NoSuchMethodException e) {
throw new java.lang.NoSuchMethodError(
"stub class initialization failed");
}
}

// constructors
public AddServerImplTwo_Stub(java.rmi.server.RemoteRef ref) {
super(ref);
}

// methods from remote interfaces

// implementation of addThree(double, double, double)
public double addThree(double $param_double_1, double $param_double_2, double $param_double_3)
throws java.rmi.RemoteException
{
try {
Object $result = ref.invoke(this, $method_addThree_0, new java.lang.Object[] {new java.lang.Double($param_double_1), new java.lang.Double($param_double_2), new java.lang.Double($param_double_3)}, 4923686196154390282L);
return ((java.lang.Double) $result).doubleValue();
} catch (java.lang.RuntimeException e) {
throw e;
} catch (java.rmi.RemoteException e) {
throw e;
} catch (java.lang.Exception e) {
throw new java.rmi.UnexpectedException("undeclared checked exception", e);
}
}
}


The implemented methods on the server side for adding the numbers looks like this....

package serverSide;



import java.rmi.*;
import java.rmi.server.*;
public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf{

/**
*
*/
private static final long serialVersionUID = 1L;
public AddServerImpl() throws RemoteException{
}
public double add(double d1, double d2) throws RemoteException{
System.out.println("We are now running AddServerImpl.add");
return d1 + d2;
}
}


package serverSide;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;


public class AddServerImplTwo extends UnicastRemoteObject implements AddServerIntfTwo{

/**
*
*/
private static final long serialVersionUID = 1L;
public AddServerImplTwo() throws RemoteException{ }

public double addThree(double d1, double d2, double d3) throws RemoteException{
System.out.println("We are now running AddServerImplTwo.addThree");
return d1 + d2 + d3;
}
}


The server side interfaces look the same as the client side but with a package change....

package serverSide;
import java.rmi.*;
public interface AddServerIntf extends Remote{
double add(double d1, double d2) throws RemoteException;
}

package serverSide;
import java.rmi.*;
public interface AddServerIntfTwo extends Remote{
double addThree(double d1, double d2, double d3) throws RemoteException;
}

And lastly the serverside server.policy looks like this....

grant codeBase "file:C:/Docume~1/steveh~1/worksp~1/simple~1/"{
permission java.security.AllPermission;
};



I then created 2 jar files with the class files and security.policy in the jar.

I then tried to run things as follows in two different batch files. The server is run first.....

java rmiregistry
java -Djava.security.policy=server.policy -jar serverSide.jar

This gives me a permissions error. I don't understand how to get aound the error and have the server code load up. I get the same permissions error when I try and run the client code.

But if I take the code out of packages and put everything into one file I can get it to load up and run on the same computer. But I can't take the code to a seperate server and get it to load up. Not sure why.

Anyone have any suggestions?

Topic: Help with RMI Previous Topic   Next Topic Topic: java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use