Java RMI简单例子

rmi调用一般过程:

接口:

 

package com.zhang.rmi;

import java.rmi.Remote;
import java.rmi.RemoteException;

/**
 * 实现Remote接口,定义的方法都需要抛出RemoteException
 * <p />
 * 
 * @author Administrator
 */
public interface TestRmiInterface extends Remote {

    String concat(String a, String b) throws RemoteException;

}

实现类:

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

/**
 * 继承UnicastRemoteObject类,实现TestRmiInterface接口
 * <p />
 * 
 * @author Administrator
 */
public class TestRmiImpl extends UnicastRemoteObject implements TestRmiInterface {
    
    private static final long serialVersionUID = 7060916499195401961L;

    /**
     * 显式定义构造方法,需要抛出RemoteExcepiton
     * 
     * @throws RemoteException
     */
    protected TestRmiImpl() throws RemoteException {
        super();
    }

   

    /**
     * {@inheritDoc}
     */
    @Override
    public String concat(String a, String b) {
        //System.out.println(a+b);
        return a + b;
    }

}

服务器端:

 

package com.zhang.rmi;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;


public class Server {
    
    public Server(){
        TestRmiInterface testRmi = null;
        Registry registry = null;
        try {
            registry = LocateRegistry.createRegistry(1099);
            
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            testRmi = new TestRmiImpl();
            registry.rebind("rmi://localhost:1099/testServer", testRmi);
        } catch (RemoteException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } 
    }
    
    public static void main(String[] args) {
//        if(System.getSecurityManager() == null){
//            System.out.println("SecurityManager is null");
//            System.setSecurityManager(new RMISecurityManager());
//        }
        
        new Server();
        System.out.println("server ready");
    }

}

 客户端:

 

package com.zhang.rmi;

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;


public class Client {
    
    public static void main(String[] args) {
        try {
            Registry registry = LocateRegistry.getRegistry(1099);
            TestRmiInterface testRmi = (TestRmiInterface) registry.lookup("rmi://localhost:1099/testServer");
            String test = testRmi.concat("hello", "rmi");
            System.out.println(test);
        }  catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NotBoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值