1. First write the client and the server and the code of the algorithm. code show as below:
(1) Interface code
Package com.wonder.rmi;
Import java.rmi.remote; import java.rmi.RemoteException; import java.util.list;
public interface RMIOperate extends Remote {public double add (double x, double y) throws RemoteException; public double minus (double x, double y) throws RemoteException; public double add (List l) throws RemoteException;}
(2) an algorithm for the interface
Package com.wonder.rmi.server;
Import java.rmi.remoteException; import java.rmi.server.UnicastRemoteObject; import java.util.list;
Import com.wonder.rmi.rmioperate;
Public Class OperateImpl Extends UnicastRemoteObject IMPLEments RMIOPERATE {
Public OPERATEIMPL () THROWS RemoteException {Super ();
/ ** * * / private static final long service = 1L;
Public Double Add (Double X, Double Y) throws remoteException {double z = x y; return z;}
Public Double Minus (Double X, Double Y) THROWS RemoteException {Double Z = x-Y; Return Z;}
Public Double Add (List L) THROWS RemoteException {Return Double.Parsedouble (String.Valueof ((Double) L.Get (0) (Double) L.Get (1)));}
} (3) Server end programs
Package com.wonder.rmi.server;
Import java.net.malformedurlexception; import java.rmi.naming; import java.rmi.remoteException; import java.rmi.registry.locateRegistry;
Public class rmiserver {
/ ** * @Param args * / public static void main (string [] args) {Try {OPERATEIMPL o = new operateImpl (); system.out.println ("Server is start ..."); locateregistry.createRegistry ( 7099); // This port should be the same as the bounded port, if not specified, the default port is 1099 naming.rebind ("RMI: //192.168.71.214: 7099 / rmioperate", o); // This port and The above port is like System.out.Println ("Waiting RMI Client Invoke ...");} catch (RemoteException E) {// Todo auto-generated catch block E.PrintStackTrace ();} catch (mALFORMEDUREXCEPTION E) {/ / TODO Auto-generated catch block e.printStackTrace ();}}
}
2. Client program
Package com.Wonder.rmi.Client;
import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.util Import java.util.list;
Import com.wonder.rmi.rmioperate;
Public class rmiclient {
/ ** * @Param args * / @suppresswarnings ({"unchecked", "unchecked"}) public static void main (string "args) {string host =" 192.168.71.214 "; try {// call mode 1 Registry reg = null; reg = LocateRegistry.getRegistry ( "192.168.71.214", 7099); RMIOperate rmiObj = (RMIOperate) reg.lookup ( "RMIOperate"); // called by 2 RMIOperate rmiObj1 = (RMIOperate) Naming.lookup ( " RMI: //192.168.71.214: 7099 / rmioperate "); // To find the port of the remote server
Double X = 1; Double Y = 2; List L = New ArrayList (2); L.Add (0, Double.Valueof ("123")); L.Add (1, Double.Valueof ("456")) Double z = rmiobj.add (x, y); system.out.println ("add =" z); double w = rmiobj.minus (x, y); system.out.println ("minus =" w); double _a = rmiobj.add (l); system.out.println ("add list =" _ a);} catch (remoteExcection e) {// Todo auto-generated catch block e.printstackTrace ();} Catch (notboundexception e) {// Todo auto-generated catch block E.PrintStackTrace ();} catch (mAlformedurlexception e) {// Todo auto-generated catch block E.PrintStackTrace ();}}
}
3. Generate stub
C: /> java weblogic.rmic com.wonder.rmi.server.OperateImpl
In order to facilitate testing, it is a JAR package to put it into the path to startweblogic.cmd.
4. If you call LocateRegistry in your code, you don't have to start the RMREGISTRY command in the command prompt window.
5. Set the startup class of WebLogic, com.wonder.rmi.server.rmiserver. Starting WebLogic can't make an error.
two. Common problem
1. If it is a problem with the connection rejection, check if the RMI registration is started normally. And the bounded address and the port is correct.
2. If there is a case where the Stub class is not found, please check if the corresponding Stub class is generated using the RMIC. You can also check if the port is set correctly.