RMI

java RMI 线程池的配置

System.setProperty("sun.rmi.transport.tcp.maxConnectionThreads", "10");

属性配置
-Dsun.rmi.transport.tcp.maxConnectionThreads=10
-Dsun.rmi.transport.tcp.threadKeepAliveTime=0 默认1分钟

System.setProperty("sun.rmi.transport.tcp.responseTimeout", "2");  

System.setProperty("sun.rmi.transport.tcp.readTimeout", "2");  

System.setProperty("sun.rmi.transport.connectionTimeout", "2");  

System.setProperty("sun.rmi.transport.proxy.connectTimeout", "2");  

System.setProperty("sun.rmi.transport.tcp.handshakeTimeout", "2"); 

https://docs.oracle.com/javase/6/docs/technotes/guides/rmi/sunrmiproperties.html

https://docs.oracle.com/javase/8/docs/technotes/guides/rmi/sunrmiproperties.html

https://docs.oracle.com/javase/8/docs/technotes/guides/rmi/

RMI使用方法

方法1

//必须要在这里继承Remote接口,否则后面报网络异常,严重误导人。
public interface Calculator extends Remote { 
	public String calculate(String expr) throws RemoteException;
}

public class CalculatorServer implements Calculator {
    @Override
    public String calculate(String expr) throws RemoteException {  
        return "hello, " + expr;  
    }  
 
    public static void main(String[] args) throws RemoteException, AlreadyBoundException{
        Calculator calc = new CalculatorServer();
        UnicastRemoteObject.exportObject(calc, 0);  
        
        // 注意这里必须是createRegistry,不能写getRegistry
        Registry registry = LocateRegistry.createRegistry(1888);  
        registry.rebind("calculator", calc);  
    }
}

public class CalculatorClient {
	public static void main(String[] args) throws Exception {
		Registry registry = LocateRegistry.getRegistry("localhost",1888);  
		Calculator calculator = (Calculator) registry.lookup("calculator");  
		String result = calculator.calculate("expr");  
		System.out.println(result);  
	}
}

方法2和方法3

public interface Const {
	// 有时用localhost会报错,可以用真实IP地址
	public static final String Host = "192.168.56.1";
}

public interface UserHandler extends Remote {
	public String getUserName(int id) throws RemoteException;
	public int getUserCount() throws RemoteException;
	public User getUserByName(String name) throws RemoteException;
}

public class User implements Serializable{
	private static final long serialVersionUID = 1L;
    // setter和getter可以没有
    String name;
    int id;

    public User(String name, int id) {
        this.name = name;
        this.id = id;
    }
    @Override
    public String toString() {
        return "User [name=" + name + ", id=" + id + "]";
    }
}

public class UserHandlerImpl extends UnicastRemoteObject implements UserHandler {
	private static final long serialVersionUID = 1L;
	// 该构造期必须存在,因为集继承了UnicastRemoteObject类,其构造器要抛出RemoteException
    public UserHandlerImpl() throws RemoteException {
        super();
    }

    @Override
    public String getUserName(int id) throws RemoteException {
        return "lmy86263";
    }
    @Override
    public int getUserCount() throws RemoteException{
        return 1;
    }
    @Override
    public User getUserByName(String name) throws RemoteException{
        return new User("lmy86263", 1);
    }
}

public class Server {

	public static void main(String[] args) throws Exception{
		UserHandler userHandler = new UserHandlerImpl();
		Server.rebind2(userHandler);

		System.out.println(" rmi server is ready ...");
	}
	private static void rebind1(UserHandler userHandler) throws Exception {
		Context namingContext = new InitialContext();
        namingContext.rebind("rmi://"+Const.Host+":1099/111",userHandler);
	}
	private static void rebind2(UserHandler userHandler) throws Exception {
		Registry registry = LocateRegistry.createRegistry(1099);
		registry.rebind("user1", userHandler);
	}
}

public class Client {

	public static void main(String[] args) throws Exception{
		UserHandler handler = Client.getUserHandler1();
		
	    int  count = handler.getUserCount();
	    String name = handler.getUserName(1);
	    System.out.println("name: " + name);
	    System.out.println("count: " + count);
	    System.out.println("user: " + handler.getUserByName("lmy86263"));
	    
		handler = Client.getUserHandler2();
		
	    count = handler.getUserCount();
	    name = handler.getUserName(1);
	    System.out.println("name: " + name);
	    System.out.println("count: " + count);
	    System.out.println("user: " + handler.getUserByName("lmy86263"));
	}
	
	private static UserHandler getUserHandler1() throws Exception {
		String url = "rmi://"+Const.Host+":1099/";
		Context namingContext = new InitialContext();
		UserHandler handler = (UserHandler) namingContext.lookup(url + "user1");
		return handler;
	}
	private static UserHandler getUserHandler2() throws Exception {
		Registry registry = LocateRegistry.getRegistry(Const.Host, 1099);
		UserHandler handler = (UserHandler) registry.lookup("user1");
		return handler;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值