java原生RMI测试

package rmi;
import java.rmi.Remote;


public interface RMITest extends Remote{

	public String sayHello(String hello) throws Exception;
}
package rmi;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;


public class RMITestImpl extends UnicastRemoteObject implements RMITest {

	protected RMITestImpl() throws RemoteException {
		super();
	}

	private static final long serialVersionUID = 1L;

	public String sayHello(String hello) throws Exception {
		System.out.println(new Date()+"调用一次");
		return "RMI……"+hello;
	}

	public static void main(String[] args) {
		try {
			LocateRegistry.createRegistry(1099);
			RMITest rmiTest =  new RMITestImpl();
			
			Naming.rebind("hello", rmiTest);
			
			System.err.println("hello was started.....");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
package rmi;
import java.rmi.Naming;


public class RMIClient {

	public static void main(String[] args) {
		try {
			RMITest rmiTest = (RMITest) Naming.lookup("//localhost:1099/hello");
			System.out.println(rmiTest.sayHello("Hello World!!"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 

猜你喜欢

转载自hamber.iteye.com/blog/1580732
今日推荐